g_win/
emit.rs

1use crate::{Command, GCodeLine, GCodeModel, G1};
2
3/// Trait objects that can be emitted to valid gcode, with an optional debug line appended
4pub trait Emit {
5    fn emit(&self, debug: bool) -> String;
6}
7
8impl Emit for Command {
9    fn emit(&self, debug: bool) -> String {
10        match self {
11            Command::G1(g1) => g1.emit(debug),
12            Command::G90 => "G90".to_string(),
13            Command::G91 => "G91".to_string(),
14            Command::M82 => "M82".to_string(),
15            Command::M83 => "M83".to_string(),
16            Command::Raw(s) => s.clone(),
17        }
18    }
19}
20
21impl Emit for GCodeLine {
22    fn emit(&self, debug: bool) -> String {
23        let comments = if self.comments.is_empty() {
24            String::from("")
25        } else {
26            format!(";{}", self.comments)
27        };
28        self.command.emit(debug) + comments.as_str()
29    }
30}
31
32impl Emit for G1 {
33    fn emit(&self, _debug: bool) -> String {
34        let mut out = String::from("G1 ");
35        let G1 { x, y, z, e, f, .. } = self;
36        let params = vec![('X', x), ('Y', y), ('Z', z), ('E', e), ('F', f)];
37        for (letter, param) in params {
38            if let Some(param) = param {
39                out += format!("{}{} ", letter, f32::from(*param)).as_str();
40            }
41        }
42        out
43    }
44}
45
46impl Emit for GCodeModel {
47    fn emit(&self, debug: bool) -> String {
48        self.lines
49            .iter()
50            .map(|line| line.emit(debug) + "\n")
51            .collect()
52    }
53}