diff options
Diffstat (limited to 'src/command.rs')
-rw-r--r-- | src/command.rs | 66 |
1 files changed, 36 insertions, 30 deletions
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 { | |||
182 | } | 182 | } |
183 | 183 | ||
184 | fn parse_command_name(input: &str) -> Result<(CommandName, &str)> { | 184 | fn parse_command_name(input: &str) -> Result<(CommandName, &str)> { |
185 | let pieces: Vec<&str> = input.trim().splitn(2, ' ').collect(); | 185 | let chars = input.trim().chars(); |
186 | 186 | let mut parsed_name = "".to_owned(); | |
187 | let command = pieces.first().unwrap(); | 187 | let mut pos = 0; |
188 | let rest = pieces.iter().skip(1).next().map(|&x| x).unwrap_or(""); | 188 | |
189 | 189 | for c in chars { | |
190 | match command.as_ref() { | 190 | pos = pos + 1; |
191 | "add" | "a" => Ok((CommandName::Add, rest)), | 191 | if c == ' ' { |
192 | "add-auto" | "aa" => Ok((CommandName::AddAuto, rest)), | 192 | break; |
193 | "delete" | "d" => Ok((CommandName::Delete, rest)), | 193 | } |
194 | "track-up" | "tup" => Ok((CommandName::TrackUp, rest)), | 194 | |
195 | "track-down" | "tdown" => Ok((CommandName::TrackDown, rest)), | 195 | parsed_name.push(c); |
196 | "h" | "?" | "help" => Ok((CommandName::Help, rest)), | 196 | } |
197 | "mprev" => Ok((CommandName::MonthPrev, "")), | 197 | |
198 | "mnext" => Ok((CommandName::MonthNext, "")), | 198 | match parsed_name.as_ref() { |
199 | "quit" | "q" => Ok((CommandName::Quit, "")), | 199 | "add" | "a" => Ok((CommandName::Add, &input[pos..])), |
200 | "write" | "w" => Ok((CommandName::Write, "")), | 200 | "add-auto" | "aa" => Ok((CommandName::AddAuto, &input[pos..])), |
201 | "" => Ok((CommandName::Blank, "")), | 201 | "delete" | "d" => Ok((CommandName::Delete, &input[pos..])), |
202 | c => Err(CommandLineError::InvalidCommand(c.to_string())), | 202 | "track-up" | "tup" => Ok((CommandName::TrackUp, &input[pos..])), |
203 | "track-down" | "tdown" => Ok((CommandName::TrackDown, &input[pos..])), | ||
204 | "h" | "?" | "help" => Ok((CommandName::Help, &input[pos..])), | ||
205 | "mprev" => Ok((CommandName::MonthPrev, &input[pos..])), | ||
206 | "mnext" => Ok((CommandName::MonthNext, &input[pos..])), | ||
207 | "quit" | "q" => Ok((CommandName::Quit, &input[pos..])), | ||
208 | "write" | "w" => Ok((CommandName::Write, &input[pos..])), | ||
209 | "" => Ok((CommandName::Blank, &input[pos..])), | ||
210 | _ => Err(CommandLineError::InvalidCommand(parsed_name)), | ||
203 | } | 211 | } |
204 | } | 212 | } |
205 | 213 | ||
@@ -583,22 +591,20 @@ mod tests { | |||
583 | #[test] | 591 | #[test] |
584 | fn parse_error_not_enough_args() { | 592 | fn parse_error_not_enough_args() { |
585 | let test_cases = [ | 593 | let test_cases = [ |
586 | ("add", "add", 2), | 594 | ("add".to_owned(), "add".to_owned(), 2), |
587 | ("add-auto", "add-auto", 2), | 595 | ("add-auto".to_owned(), "add-auto".to_owned(), 2), |
588 | ("delete", "delete", 1), | 596 | ("delete".to_owned(), "delete".to_owned(), 1), |
589 | ("track-up", "track-up", 1), | 597 | ("track-up".to_owned(), "track-up".to_owned(), 1), |
590 | ("track-down", "track-down", 1), | 598 | ("track-down".to_owned(), "track-down".to_owned(), 1), |
591 | ] | 599 | ]; |
592 | .iter() | 600 | |
593 | .map(|(a, b, c)| (a.to_owned(), b.to_owned(), c)); | 601 | for test_case in test_cases.iter() { |
594 | 602 | let result = Command::from_string(&test_case.0); | |
595 | for test_case in test_cases { | ||
596 | let result = Command::from_string(test_case.0); | ||
597 | 603 | ||
598 | assert!(result.is_err()); | 604 | assert!(result.is_err()); |
599 | assert_eq!( | 605 | assert_eq!( |
600 | result.err().unwrap(), | 606 | result.err().unwrap(), |
601 | CommandLineError::NotEnoughArgs(test_case.1.to_string(), *test_case.2) | 607 | CommandLineError::NotEnoughArgs(test_case.1.clone(), test_case.2) |
602 | ); | 608 | ); |
603 | } | 609 | } |
604 | } | 610 | } |