From 5ea795fc7436280e8f29b3a580b5254a57ffc76a Mon Sep 17 00:00:00 2001 From: Akshay Date: Mon, 3 Aug 2020 16:23:12 +0530 Subject: Revert "simplify command parsing logic" This reverts commit 83cba9dae6e4cc6a348ef0b9ee7e2c7f747409f5. --- src/command.rs | 66 ++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 36 insertions(+), 30 deletions(-) (limited to 'src') diff --git a/src/command.rs b/src/command.rs index 8c823a2..13ba194 100644 --- a/src/command.rs +++ b/src/command.rs @@ -182,24 +182,32 @@ impl Command { } fn parse_command_name(input: &str) -> Result<(CommandName, &str)> { - let pieces: Vec<&str> = input.trim().splitn(2, ' ').collect(); - - let command = pieces.first().unwrap(); - let rest = pieces.iter().skip(1).next().map(|&x| x).unwrap_or(""); - - match command.as_ref() { - "add" | "a" => Ok((CommandName::Add, rest)), - "add-auto" | "aa" => Ok((CommandName::AddAuto, rest)), - "delete" | "d" => Ok((CommandName::Delete, rest)), - "track-up" | "tup" => Ok((CommandName::TrackUp, rest)), - "track-down" | "tdown" => Ok((CommandName::TrackDown, rest)), - "h" | "?" | "help" => Ok((CommandName::Help, rest)), - "mprev" => Ok((CommandName::MonthPrev, "")), - "mnext" => Ok((CommandName::MonthNext, "")), - "quit" | "q" => Ok((CommandName::Quit, "")), - "write" | "w" => Ok((CommandName::Write, "")), - "" => Ok((CommandName::Blank, "")), - c => Err(CommandLineError::InvalidCommand(c.to_string())), + let chars = input.trim().chars(); + let mut parsed_name = "".to_owned(); + let mut pos = 0; + + for c in chars { + pos = pos + 1; + if c == ' ' { + break; + } + + parsed_name.push(c); + } + + match parsed_name.as_ref() { + "add" | "a" => Ok((CommandName::Add, &input[pos..])), + "add-auto" | "aa" => Ok((CommandName::AddAuto, &input[pos..])), + "delete" | "d" => Ok((CommandName::Delete, &input[pos..])), + "track-up" | "tup" => Ok((CommandName::TrackUp, &input[pos..])), + "track-down" | "tdown" => Ok((CommandName::TrackDown, &input[pos..])), + "h" | "?" | "help" => Ok((CommandName::Help, &input[pos..])), + "mprev" => Ok((CommandName::MonthPrev, &input[pos..])), + "mnext" => Ok((CommandName::MonthNext, &input[pos..])), + "quit" | "q" => Ok((CommandName::Quit, &input[pos..])), + "write" | "w" => Ok((CommandName::Write, &input[pos..])), + "" => Ok((CommandName::Blank, &input[pos..])), + _ => Err(CommandLineError::InvalidCommand(parsed_name)), } } @@ -583,22 +591,20 @@ mod tests { #[test] fn parse_error_not_enough_args() { let test_cases = [ - ("add", "add", 2), - ("add-auto", "add-auto", 2), - ("delete", "delete", 1), - ("track-up", "track-up", 1), - ("track-down", "track-down", 1), - ] - .iter() - .map(|(a, b, c)| (a.to_owned(), b.to_owned(), c)); - - for test_case in test_cases { - let result = Command::from_string(test_case.0); + ("add".to_owned(), "add".to_owned(), 2), + ("add-auto".to_owned(), "add-auto".to_owned(), 2), + ("delete".to_owned(), "delete".to_owned(), 1), + ("track-up".to_owned(), "track-up".to_owned(), 1), + ("track-down".to_owned(), "track-down".to_owned(), 1), + ]; + + for test_case in test_cases.iter() { + let result = Command::from_string(&test_case.0); assert!(result.is_err()); assert_eq!( result.err().unwrap(), - CommandLineError::NotEnoughArgs(test_case.1.to_string(), *test_case.2) + CommandLineError::NotEnoughArgs(test_case.1.clone(), test_case.2) ); } } -- cgit v1.2.3