support latest macro syntax

master
Sameer Puri 5 years ago
parent 24318598bd
commit b74291c6b0

@ -48,7 +48,7 @@ macro_rules! command {
} }
macro_rules! commands { macro_rules! commands {
($($(#[$outer:meta])* $commandName: ident {$letter: pat, $number: pat, $fraction: pat, {$($(#[$inner:meta])* $argument: ident), *} },)*) => { ($($(#[$outer:meta])* $commandName: ident {$letter: expr, $number: expr, $fraction: path, {$($(#[$inner:meta])* $argument: ident), *} },)*) => {
/// Commands are the operational unit of GCode /// Commands are the operational unit of GCode
/// They consist of an identifying word followed by arguments /// They consist of an identifying word followed by arguments
@ -58,72 +58,66 @@ macro_rules! commands {
arguments: Vec<Word> arguments: Vec<Word>
} }
paste::item! { impl Command {
impl Command { pub fn new(command_word: CommandWord, mut arguments: Vec<Word>) -> Self {
pub fn new(command_word: CommandWord, mut arguments: Vec<Word>) -> Self { Self {
Self { command_word: command_word.clone(),
command_word: command_word.clone(), arguments: arguments.drain(..).filter(|w| {
arguments: arguments.drain(..).filter(|w| { match command_word {
match command_word { $(CommandWord::$commandName => match w.letter.to_lowercase() {
$(CommandWord::$commandName => match w.letter.to_lowercase() { $($argument => true,)*
$($argument => true,)*
_ => false
},)*
_ => false _ => false
} },)*
}).collect() _ => false
} }
}).collect()
} }
}
pub fn push(&mut self, argument: Word) { pub fn push(&mut self, argument: Word) {
match self.command_word { match self.command_word {
$(CommandWord::$commandName => match argument.letter.to_lowercase() { $(CommandWord::$commandName => match argument.letter.to_lowercase() {
$($argument => { $($argument => {
self.arguments.push(argument); self.arguments.push(argument);
})* })*
_ => {}
},)*
_ => {} _ => {}
} },)*
_ => {}
} }
} }
} }
paste::item! { impl Into<Vec<Word>> for Command {
impl Into<Vec<Word>> for Command { fn into(self) -> Vec<Word> {
fn into(self) -> Vec<Word> { let mut args = self.arguments;
let mut args = self.arguments; args.insert(0, self.command_word.into());
args.insert(0, self.command_word.into()); args
args
}
} }
} }
paste::item! { impl TryFrom<&[Word]> for Command {
impl TryFrom<&[Word]> for Command { type Error = ();
type Error = (); fn try_from(words: &[Word]) -> Result<Self, ()> {
fn try_from(words: &[Word]) -> Result<Self, ()> { if words.len() == 0 {
if words.len() == 0 { return Err(());
return Err(()); }
} let command_word = CommandWord::try_from(&words[0])?;
let command_word = CommandWord::try_from(&words[0])?; let mut arguments = Vec::with_capacity(words.len() - 1);
let mut arguments = Vec::with_capacity(words.len() - 1); for i in 1..words.len() {
for i in 1..words.len() { match command_word {
match command_word { $(CommandWord::$commandName => match words[i].letter.to_lowercase() {
$(CommandWord::$commandName => match words[i].letter.to_lowercase() { $($argument => {
$($argument => { arguments.push(words[i].clone());
arguments.push(words[i].clone()); })*
})*
_ => {}
},)*
_ => {} _ => {}
} },)*
_ => {}
} }
Ok(Self {
command_word,
arguments
})
} }
Ok(Self {
command_word,
arguments
})
} }
} }
@ -141,62 +135,58 @@ macro_rules! commands {
Checksum(u8), Checksum(u8),
} }
paste::item! { impl CommandWord {
impl CommandWord { pub fn is_command(word: &Word) -> bool {
pub fn is_command(word: &Word) -> bool { let (number, fraction) = match &word.value {
let (number, fraction) = match &word.value { Value::Fractional(number, fraction) => (number, fraction),
Value::Fractional(number, fraction) => (number, fraction), _other => return false
_other => return false };
}; match (word.letter, number, fraction) {
match (word.letter, number, fraction) { $(($letter, $number, $fraction) => true,)*
$(($letter, $number, $fraction) => true,)* ('*', _checksum, None) => true,
('*', _checksum, None) => true, ('N', _line_number, None) => true,
('N', _line_number, None) => true, (_, _, _) => false
(_, _, _) => false
}
} }
} }
} }
paste::item! {
impl TryFrom<&Word> for CommandWord { impl TryFrom<&Word> for CommandWord {
type Error = (); type Error = ();
fn try_from(word: &Word) -> Result<Self, ()> { fn try_from(word: &Word) -> Result<Self, ()> {
let (number, fraction) = match &word.value { let (number, fraction) = match &word.value {
Value::Fractional(number, fraction) => (number, fraction), Value::Fractional(number, fraction) => (number, fraction),
_other => return Err(()) _other => return Err(())
}; };
match (word.letter, number, fraction) { match (word.letter, number, fraction) {
$(($letter, $number, $fraction) => Ok(Self::$commandName),)* $(($letter, $number, $fraction) => Ok(Self::$commandName),)*
('*', checksum, None) => Ok(Self::Checksum(*checksum as u8)), ('*', checksum, None) => Ok(Self::Checksum(*checksum as u8)),
('N', line_number, None) => Ok(Self::LineNumber(*line_number as u16)), ('N', line_number, None) => Ok(Self::LineNumber(*line_number as u16)),
(_, _, _) => Err(()) (_, _, _) => Err(())
}
} }
} }
} }
paste::item!{
impl Into<Word> for CommandWord { impl Into<Word> for CommandWord {
fn into(self) -> Word { fn into(self) -> Word {
match self { match self {
$( $(
Self::$commandName {} => Word { Self::$commandName {} => Word {
letter: $letter, letter: $letter,
// TODO: fix fraction // TODO: fix fraction
value: Value::Fractional($number, $fraction) value: Value::Fractional($number, $fraction)
},
)*
Self::Checksum(value) => Word {
letter: '*',
value: Value::Fractional(value as u32, None)
},
Self::LineNumber(value) => Word {
letter: 'N',
value: Value::Fractional(value as u32, None)
}, },
Self::Comment(string) => Word { )*
letter: ';', Self::Checksum(value) => Word {
value: Value::String(string) letter: '*',
} value: Value::Fractional(value as u32, None)
},
Self::LineNumber(value) => Word {
letter: 'N',
value: Value::Fractional(value as u32, None)
},
Self::Comment(string) => Word {
letter: ';',
value: Value::String(string)
} }
} }
} }

Loading…
Cancel
Save