From 5b4baacef870811401751417c4e72fd25e02e5a2 Mon Sep 17 00:00:00 2001 From: Akshay Date: Mon, 3 Aug 2020 16:38:43 +0530 Subject: Revert "Merge remote-tracking branch 'origin/master' into feature/larger-names" This reverts commit e794c6ecf133df248471b465a7553d454b527399, reversing changes made to 2dafbfe51b2b4ebafc21fe68a9b909ebe3a4e23b. --- src/command.rs | 98 +++++++++++----------------------------------------------- 1 file changed, 18 insertions(+), 80 deletions(-) diff --git a/src/command.rs b/src/command.rs index 40801e7..e4c099e 100644 --- a/src/command.rs +++ b/src/command.rs @@ -122,6 +122,7 @@ pub enum Command { #[derive(Debug)] pub enum CommandLineError { InvalidCommand(String), // command name + InvalidArg(u32), // position NotEnoughArgs(String, u32), // command name, required no. of args } @@ -131,6 +132,7 @@ impl fmt::Display for CommandLineError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { CommandLineError::InvalidCommand(s) => write!(f, "Invalid command: `{}`", s), + CommandLineError::InvalidArg(p) => write!(f, "Invalid argument at position {}", p), CommandLineError::NotEnoughArgs(s, n) => { write!(f, "Command `{}` requires atleast {} argument(s)!", s, n) } @@ -148,9 +150,23 @@ impl Command { } let first = strings.first().unwrap().to_string(); - let args: Vec = strings.iter_mut().skip(1).map(|s| s.to_string()).collect(); + let mut args: Vec = strings.iter_mut().skip(1).map(|s| s.to_string()).collect(); let mut _add = |auto: bool, first: String| { - return parse_add(first, args.clone(), auto); + if args.is_empty() { + return Err(CommandLineError::NotEnoughArgs(first, 1)); + } + let goal = args + .get(1) + .map(|x| { + x.parse::() + .map_err(|_| CommandLineError::InvalidArg(2)) + }) + .transpose()?; + return Ok(Command::Add( + args.get_mut(0).unwrap().to_string(), + goal, + auto, + )); }; match first.as_ref() { @@ -189,81 +205,3 @@ impl Command { } } } - -fn parse_add(verb: String, args: Vec, auto: bool) -> Result { - if args.is_empty() { - return Err(CommandLineError::NotEnoughArgs(verb, 1)); - } - - let mut pos = 1; - let mut acc = "".to_owned(); - let mut new_goal: Option = None; - for s1 in args { - if pos == 1 { - acc.push_str(&s1); - } else { - if let Ok(n) = s1.parse::() { - new_goal = Some(n); - } else { - acc.push(' '); - acc.push_str(&s1); - } - } - pos = pos + 1; - } - - return Ok(Command::Add(acc, new_goal, auto)); -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn parse_add_command_with_goal() { - let command: Vec = "eat healthy 3" - .trim() - .split(' ') - .into_iter() - .map(|s| s.to_string()) - .collect(); - - let verb = "add".to_owned(); - let auto = false; - - let result = parse_add(verb, command, auto); - - match result.unwrap() { - Command::Add(name, goal, a) => { - assert_eq!(name, "eat healthy".to_owned()); - assert_eq!(goal.unwrap(), 3); - assert_eq!(a, auto); - } - _ => panic!(), - } - } - - #[test] - fn parse_add_command_without_goal() { - let command: Vec = "eat healthy" - .trim() - .split(' ') - .into_iter() - .map(|s| s.to_string()) - .collect(); - - let verb = "add".to_owned(); - let auto = false; - - let result = parse_add(verb, command, auto); - - match result.unwrap() { - Command::Add(name, goal, a) => { - assert_eq!(name, "eat healthy".to_owned()); - assert!(goal.is_none()); - assert_eq!(a, auto); - } - _ => panic!(), - } - } -} -- cgit v1.2.3