summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/main.rs17
-rw-r--r--src/translate.rs28
2 files changed, 30 insertions, 15 deletions
diff --git a/src/main.rs b/src/main.rs
index 47262c6..71d79f0 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -7,21 +7,8 @@ use std::io::prelude::*;
use clap::{App, Arg, SubCommand};
use gcode::*;
-fn translate(mut gcode: Gcode, letter: char, offset: f32) -> Gcode {
- for arg in gcode.args_mut() {
- if arg.letter == letter {
- arg.value += offset;
- }
- }
- gcode
-}
-
-fn translate_optional(gcode: Gcode, letter: char, offset: Option<f32>) -> Gcode {
- match offset {
- Some(offset) => translate(gcode, letter, offset),
- None => gcode
- }
-}
+mod translate;
+use translate::*;
fn read_stdin() -> io::Result<String> {
let mut buffer = String::new();
diff --git a/src/translate.rs b/src/translate.rs
new file mode 100644
index 0000000..ecf6698
--- /dev/null
+++ b/src/translate.rs
@@ -0,0 +1,28 @@
+extern crate gcode;
+
+use gcode::Gcode;
+
+pub fn translate(mut gcode: Gcode, letter: char, offset: f32) -> Gcode {
+ for arg in gcode.args_mut() {
+ if arg.letter == letter {
+ arg.value += offset;
+ }
+ }
+ gcode
+}
+
+pub fn translate_optional(gcode: Gcode, letter: char, offset: Option<f32>) -> Gcode {
+ match offset {
+ Some(offset) => translate(gcode, letter, offset),
+ None => gcode
+ }
+}
+
+#[test]
+fn test_translate() {
+ let test = gcode::parse("G0 X1 Y2").next().unwrap();
+ let result = translate(test, 'X', 42.0);
+
+ assert_eq!(43.0, result.value_for('X').unwrap());
+ assert_eq!(2.0, result.value_for('Y').unwrap());
+}