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