diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml new file mode 100644 index 0000000..b5e8257 --- /dev/null +++ b/.github/workflows/rust.yml @@ -0,0 +1,33 @@ +name: svg2gcode + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + +env: + CARGO_TERM_COLOR: always + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v1 + - name: Build + run: cargo build + - name: Run cargo-tarpaulin + uses: actions-rs/tarpaulin@v0.1 + with: + version: '0.14.2' + args: '-- --nocapture --test-threads 1' + - name: Upload to codecov.io + uses: codecov/codecov-action@v1 + with: + token: ${{secrets.CODECOV_TOKEN}} + + - name: Archive code coverage results + uses: actions/upload-artifact@v1 + with: + name: code-coverage-report + path: cobertura.xml diff --git a/src/converter.rs b/src/converter.rs index 7c10515..3659895 100644 --- a/src/converter.rs +++ b/src/converter.rs @@ -26,6 +26,16 @@ pub struct ProgramOptions { pub dpi: f64, } +impl Default for ProgramOptions { + fn default() -> Self { + Self { + tolerance: 0.002, + feedrate: 300.0, + dpi: 96.0, + } + } +} + pub fn svg2program(doc: &Document, options: ProgramOptions, mach: Machine) -> Vec { let mut turtle = Turtle::new(mach); diff --git a/src/machine.rs b/src/machine.rs index f82a413..825b946 100644 --- a/src/machine.rs +++ b/src/machine.rs @@ -23,7 +23,7 @@ pub enum Distance { /// Generic machine state simulation, assuming nothing is known about the machine when initialized. /// This is used to reduce output GCode verbosity and run repetitive actions. -#[derive(Debug)] +#[derive(Debug, Default)] pub struct Machine { tool_state: Option, distance_mode: Option, diff --git a/src/main.rs b/src/main.rs index 2be2b34..ffc6906 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,6 +21,9 @@ mod postprocess; /// This concept is referred to as [Turtle graphics](https://en.wikipedia.org/wiki/Turtle_graphics). mod turtle; +use converter::ProgramOptions; +use machine::Machine; + fn main() -> io::Result<()> { if env::var("RUST_LOG").is_err() { env::set_var("RUST_LOG", "svg2gcode=info") @@ -112,3 +115,24 @@ fn main() -> io::Result<()> { gcode::program2gcode(program, std::io::stdout()) } } + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn square_produces_expected_gcode() { + let shapes = include_str!("../tests/square.svg"); + let options = ProgramOptions::default(); + let machine = Machine::default(); + let document = roxmltree::Document::parse(&shapes).unwrap(); + + let program = converter::svg2program(&document, options, machine); + let mut actual = vec![]; + assert!(gcode::program2gcode(program, &mut actual).is_ok()); + assert_eq!( + String::from_utf8(actual).unwrap(), + include_str!("../tests/square.gcode") + ) + } +} diff --git a/tests/square.svg b/tests/square.svg new file mode 100644 index 0000000..55bfc25 --- /dev/null +++ b/tests/square.svg @@ -0,0 +1,67 @@ + + + + + + + + + + image/svg+xml + + + + + + + + +