From 2ca06e023b5aa36806f123e258e8b3cb66f2eae3 Mon Sep 17 00:00:00 2001 From: Astro Date: Thu, 19 Sep 2019 00:30:32 +0200 Subject: [PATCH] command_parser: use combinators, allow trailing whitespace --- firmware/src/command_parser.rs | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/firmware/src/command_parser.rs b/firmware/src/command_parser.rs index 7ab332a..49c00c4 100644 --- a/firmware/src/command_parser.rs +++ b/firmware/src/command_parser.rs @@ -3,10 +3,10 @@ use nom::{ IResult, branch::alt, bytes::complete::{is_a, tag, take_while1}, - character::{is_digit, complete::char}, - combinator::{map, value}, + character::{is_digit, complete::{char, one_of}}, + combinator::{complete, map, value}, sequence::preceded, - multi::fold_many1, + multi::{fold_many0, fold_many1}, error::ErrorKind, }; use lexical_core as lexical; @@ -101,6 +101,15 @@ pub enum Command { }, } +fn end(input: &[u8]) -> IResult<&[u8], ()> { + complete( + fold_many0( + one_of("\r\n\t "), + (), |(), _| () + ) + )(input) +} + fn whitespace(input: &[u8]) -> IResult<&[u8], ()> { fold_many1(char(' '), (), |(), _| ())(input) } @@ -141,11 +150,11 @@ fn report(input: &[u8]) -> IResult<&[u8], Command> { |reporting| Command::Reporting(reporting)) ), // `report mode` - Show current reporting state - |input| Ok((input, Command::Show(ShowCommand::Reporting))) + value(Command::Show(ShowCommand::Reporting), end) )) )), // `report` - Report once - |input| Ok((input, Command::Show(ShowCommand::Input))) + value(Command::Show(ShowCommand::Input), end) )) )(input) } @@ -180,7 +189,7 @@ fn pwm(input: &[u8]) -> IResult<&[u8], Result> { pwm_pid, pwm_manual, ))), - |input| Ok((input, Ok(Command::Show(ShowCommand::Pwm)))) + value(Ok(Command::Show(ShowCommand::Pwm)), end) ))(input) } @@ -213,15 +222,13 @@ fn pid(input: &[u8]) -> IResult<&[u8], Result> { whitespace, pid_parameter ), - |input| Ok((input, Ok(Command::Show(ShowCommand::Pid)))) + value(Ok(Command::Show(ShowCommand::Pid)), end) ))(input) } fn command(input: &[u8]) -> IResult<&[u8], Result> { alt((value(Ok(Command::Quit), tag("quit")), - |input| report(input).map(|(input, command)| { - (input, Ok(command)) - }), + map(report, Ok), pwm, pid, ))(input)