1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
use crate::{Span, Word};
use core::fmt::{self, Display, Formatter};

cfg_if::cfg_if! {
    if #[cfg(feature = "std")] {
        type Arguments = Vec<Word>;
    } else {
        use arrayvec::ArrayVec;
        type Arguments = ArrayVec<[Word; GCode::MAX_ARGUMENT_LEN]>;
    }
}

/// The general category for a [`GCode`].
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(
    feature = "serde-1",
    derive(serde_derive::Serialize, serde_derive::Deserialize)
)]
#[repr(C)]
pub enum Mnemonic {
    General,
    Miscellaneous,
    ProgramNumber,
    ToolChange,
}

impl Mnemonic {
    pub fn for_letter(letter: char) -> Option<Mnemonic> {
        match letter.to_ascii_lowercase() {
            'g' => Some(Mnemonic::General),
            'm' => Some(Mnemonic::Miscellaneous),
            'o' => Some(Mnemonic::ProgramNumber),
            't' => Some(Mnemonic::ToolChange),
            _ => None,
        }
    }
}

impl Display for Mnemonic {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            Mnemonic::General => write!(f, "G"),
            Mnemonic::Miscellaneous => write!(f, "M"),
            Mnemonic::ProgramNumber => write!(f, "O"),
            Mnemonic::ToolChange => write!(f, "T"),
        }
    }
}

/// A single gcode command.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(
    feature = "serde-1",
    derive(serde_derive::Serialize, serde_derive::Deserialize)
)]
pub struct GCode {
    mnemonic: Mnemonic,
    number: f32,
    arguments: Arguments,
    span: Span,
}

impl GCode {
    /// The maximum number of [`Word`]s when compiled without the `std`
    /// feature.
    pub const MAX_ARGUMENT_LEN: usize = 5;

    pub fn new(mnemonic: Mnemonic, number: f32, span: Span) -> Self {
        GCode {
            mnemonic,
            number,
            span,
            arguments: Arguments::default(),
        }
    }

    pub fn mnemonic(&self) -> Mnemonic { self.mnemonic }

    pub fn major_number(&self) -> u32 {
        debug_assert!(self.number >= 0.0);

        self.number.floor() as u32
    }

    pub fn minor_number(&self) -> u32 {
        let fract = self.number - self.number.floor();
        let digit = (fract * 10.0).round();
        digit as u32
    }

    pub fn arguments(&self) -> &[Word] { &self.arguments }

    pub fn span(&self) -> Span { self.span }

    /// Add an argument to the list of arguments attached to this [`GCode`].
    pub fn push_argument(&mut self, arg: Word) { self.arguments.push(arg); }

    /// The builder equivalent of [`GCode::push_argument()`].
    pub fn with_argument(mut self, arg: Word) -> Self {
        self.push_argument(arg);
        self
    }

    /// Get the value for a particular argument.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use gcode::{GCode, Mnemonic, Span, Word};
    /// let gcode = GCode::new(Mnemonic::General, 1.0, Span::PLACEHOLDER)
    ///     .with_argument(Word::new('X', 30.0, Span::PLACEHOLDER))
    ///     .with_argument(Word::new('Y', -3.14, Span::PLACEHOLDER));
    ///
    /// assert_eq!(gcode.value_for('Y'), Some(-3.14));
    /// ```
    pub fn value_for(&self, letter: char) -> Option<f32> {
        let letter = letter.to_ascii_lowercase();

        for arg in self.arguments() {
            if arg.letter.to_ascii_lowercase() == letter {
                return Some(arg.value);
            }
        }

        None
    }
}

impl Extend<Word> for GCode {
    fn extend<I: IntoIterator<Item = Word>>(&mut self, words: I) {
        for word in words {
            self.push_argument(word);
        }
    }
}

impl Display for GCode {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "{}{}", self.mnemonic(), self.major_number())?;

        if self.minor_number() != 0 {
            write!(f, ".{}", self.minor_number())?;
        }

        for arg in self.arguments() {
            write!(f, " {}", arg)?;
        }

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn correct_major_number() {
        let code = GCode {
            mnemonic: Mnemonic::General,
            number: 90.5,
            arguments: Arguments::default(),
            span: Span::default(),
        };

        assert_eq!(code.major_number(), 90);
    }

    #[test]
    fn correct_minor_number() {
        for i in 0..=9 {
            let code = GCode {
                mnemonic: Mnemonic::General,
                number: 10.0 + (i as f32) / 10.0,
                arguments: Arguments::default(),
                span: Span::default(),
            };
            println!("{:?}", code);

            assert_eq!(code.minor_number(), i);
        }
    }

    #[test]
    fn get_argument_values() {
        let mut code = GCode::new(Mnemonic::General, 90.0, Span::default());
        code.extend(vec![
            Word {
                letter: 'X',
                value: 10.0,
                span: Span::default(),
            },
            Word {
                letter: 'y',
                value: -3.14,
                span: Span::default(),
            },
        ]);

        assert_eq!(code.value_for('X'), Some(10.0));
        assert_eq!(code.value_for('x'), Some(10.0));
        assert_eq!(code.value_for('Y'), Some(-3.14));
        assert_eq!(code.value_for('Z'), None);
    }
}