10
10
11
11
use crate :: error:: Error ;
12
12
use crate :: token:: { Token , Tokenizer } ;
13
+ use std:: collections:: HashMap ;
13
14
use std:: fmt;
14
15
15
- #[ derive( PartialEq , Eq , Debug ) ]
16
+ #[ derive( PartialEq , Eq , Debug , Copy , Clone ) ]
16
17
pub enum ShortcutCommand {
17
18
Ready ,
18
19
Author ,
@@ -35,28 +36,26 @@ impl fmt::Display for ParseError {
35
36
36
37
impl ShortcutCommand {
37
38
pub fn parse < ' a > ( input : & mut Tokenizer < ' a > ) -> Result < Option < Self > , Error < ' a > > {
39
+ let mut shortcuts = HashMap :: new ( ) ;
40
+ shortcuts. insert ( "ready" , ShortcutCommand :: Ready ) ;
41
+ shortcuts. insert ( "author" , ShortcutCommand :: Author ) ;
42
+
38
43
let mut toks = input. clone ( ) ;
39
- if let Some ( Token :: Word ( "ready" ) ) = toks. peek_token ( ) ? {
40
- toks. next_token ( ) ?;
41
- if let Some ( Token :: Dot ) | Some ( Token :: EndOfLine ) = toks. peek_token ( ) ? {
42
- toks. next_token ( ) ?;
43
- * input = toks;
44
- return Ok ( Some ( ShortcutCommand :: Ready ) ) ;
45
- } else {
46
- return Err ( toks. error ( ParseError :: ExpectedEnd ) ) ;
47
- }
48
- } else if let Some ( Token :: Word ( "author" ) ) = toks. peek_token ( ) ? {
44
+ if let Some ( Token :: Word ( word) ) = toks. peek_token ( ) ? {
49
45
toks. next_token ( ) ?;
50
46
if let Some ( Token :: Dot ) | Some ( Token :: EndOfLine ) = toks. peek_token ( ) ? {
47
+ if !shortcuts. contains_key ( word) {
48
+ return Ok ( None ) ;
49
+ }
51
50
toks. next_token ( ) ?;
52
51
* input = toks;
53
- return Ok ( Some ( ShortcutCommand :: Author ) ) ;
52
+ let command = shortcuts. get ( word) . unwrap ( ) ;
53
+ return Ok ( Some ( * command) ) ;
54
54
} else {
55
55
return Err ( toks. error ( ParseError :: ExpectedEnd ) ) ;
56
56
}
57
- } else {
58
- return Ok ( None ) ;
59
57
}
58
+ Ok ( None )
60
59
}
61
60
}
62
61
0 commit comments