From f2dcb8b08dbcdedffd27d0063ad47c06ee986a4e Mon Sep 17 00:00:00 2001 From: Astro Date: Fri, 13 Sep 2019 01:10:31 +0200 Subject: [PATCH] command_parser: unnest the grammar definition --- firmware/src/command_parser.rs | 67 ++++++++++++++-------------------- 1 file changed, 28 insertions(+), 39 deletions(-) diff --git a/firmware/src/command_parser.rs b/firmware/src/command_parser.rs index 2f763fa..6db8362 100644 --- a/firmware/src/command_parser.rs +++ b/firmware/src/command_parser.rs @@ -84,8 +84,8 @@ impl Command { $block } )* - Token::End => Err(Error::UnexpectedEnd), - _ => Err(Error::UnexpectedToken(lexer.token)) + Token::End => return Err(Error::UnexpectedEnd), + _ => return Err(Error::UnexpectedToken(lexer.token)) } }; } @@ -94,7 +94,7 @@ impl Command { ($result: expr) => { match lexer.token { Token::End => Ok($result), - _ => Err(Error::UnexpectedToken(lexer.token)), + _ => return Err(Error::UnexpectedToken(lexer.token)), } }; } @@ -111,42 +111,31 @@ impl Command { ], End => Ok(Command::Report(ReportMode::Once)), ], - Channel => choice![ - Number => { - let channel = CHANNEL_IDS.iter() - .position(|id| *id == lexer.slice()); - match channel { - Some(channel) => { - choice![ - Enable => Ok(Command::Channel( - channel as u8, - ChannelCommand::Enable - )), - Disable => Ok(Command::Channel( - 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) - } - }, - ], + Channel => { + let channel = choice![ + Number => { + CHANNEL_IDS.iter() + .position(|id| *id == lexer.slice()) + .ok_or(Error::NoSuchChannel) + }, + ]? as u8; + choice![ + Enable => + Ok(Command::Channel(channel, ChannelCommand::Enable)), + Disable => + Ok(Command::Channel(channel, ChannelCommand::Enable)), + Setup => { + let setup = choice![ + Number => { + SETUP_IDS.iter() + .position(|id| *id == lexer.slice()) + .ok_or(Error::NoSuchSetup) + }, + ]? as u8; + end!(Command::Channel(channel, ChannelCommand::Setup(setup))) + }, + ] + }, ] } }