From 3fd1b2265dff12ef9010cb1b6e18a030e8505526 Mon Sep 17 00:00:00 2001 From: Astro Date: Fri, 13 Sep 2019 01:04:59 +0200 Subject: [PATCH] command_parser: channel setup --- firmware/src/command_parser.rs | 35 ++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/firmware/src/command_parser.rs b/firmware/src/command_parser.rs index f44f305..2f763fa 100644 --- a/firmware/src/command_parser.rs +++ b/firmware/src/command_parser.rs @@ -28,6 +28,8 @@ enum Token { Enable, #[token = "disable"] Disable, + #[token = "setup"] + Setup, #[regex = "[0-9]+"] Number, @@ -39,6 +41,7 @@ pub enum Error { UnexpectedEnd, UnexpectedToken(Token), NoSuchChannel, + NoSuchSetup, } #[derive(Debug)] @@ -51,6 +54,7 @@ pub enum ShowCommand { pub enum ChannelCommand { Enable, Disable, + Setup(u8), } #[derive(Debug)] @@ -64,11 +68,13 @@ pub enum Command { const CHANNEL_IDS: &'static [&'static str] = &[ "0", "1", "2", "3", ]; +const SETUP_IDS: &'static [&'static str] = CHANNEL_IDS; impl Command { pub fn parse(input: &str) -> Result { let mut lexer = Token::lexer(input); + /// Match against a set of expected tokens macro_rules! choice { [$($token: tt => $block: stmt,)*] => { match lexer.token { @@ -81,14 +87,24 @@ impl Command { Token::End => Err(Error::UnexpectedEnd), _ => Err(Error::UnexpectedToken(lexer.token)) } - } + }; + } + /// Expecting no further tokens + macro_rules! end { + ($result: expr) => { + match lexer.token { + Token::End => Ok($result), + _ => Err(Error::UnexpectedToken(lexer.token)), + } + }; } + // Command grammar choice![ Quit => Ok(Command::Quit), Report => choice![ Mode => choice![ - End => Ok(Command::Show(ShowCommand::ReportMode)), + End => end!(Command::Show(ShowCommand::ReportMode)), Off => Ok(Command::Report(ReportMode::Off)), Once => Ok(Command::Report(ReportMode::Once)), Continuous => Ok(Command::Report(ReportMode::Continuous)), @@ -110,6 +126,21 @@ impl Command { channel as u8, ChannelCommand::Enable )), + Setup => choice![ + Number => { + let setup = SETUP_IDS.iter() + .position(|id| *id == lexer.slice()); + match setup { + Some(setup) => + end!(Command::Channel( + channel as u8, + ChannelCommand::Setup(setup as u8) + )), + None => + Err(Error::NoSuchSetup) + } + }, + ], ] } None => Err(Error::NoSuchChannel)