-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathshortcut.rs
86 lines (73 loc) · 2.1 KB
/
shortcut.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//! The shortcut command parser.
//!
//! This can parse predefined shortcut input, single word commands.
//!
//! The grammar is as follows:
//!
//! ```text
//! Command: `@bot ready`/`@bot review`, or `@bot author`.
//! ```
use crate::error::Error;
use crate::token::{Token, Tokenizer};
use std::collections::HashMap;
use std::fmt;
#[derive(PartialEq, Eq, Debug, Copy, Clone)]
pub enum ShortcutCommand {
Ready,
Author,
Blocked,
}
#[derive(PartialEq, Eq, Debug)]
pub enum ParseError {}
impl std::error::Error for ParseError {}
impl fmt::Display for ParseError {
fn fmt(&self, _: &mut fmt::Formatter) -> fmt::Result {
match *self {}
}
}
impl ShortcutCommand {
pub fn parse<'a>(input: &mut Tokenizer<'a>) -> Result<Option<Self>, Error<'a>> {
let mut shortcuts = HashMap::new();
shortcuts.insert("ready", ShortcutCommand::Ready);
shortcuts.insert("review", ShortcutCommand::Ready);
shortcuts.insert("reviewer", ShortcutCommand::Ready);
shortcuts.insert("author", ShortcutCommand::Author);
shortcuts.insert("blocked", ShortcutCommand::Blocked);
let mut toks = input.clone();
if let Some(Token::Word(word)) = toks.peek_token()? {
if !shortcuts.contains_key(word) {
return Ok(None);
}
toks.next_token()?;
*input = toks;
let command = shortcuts.get(word).unwrap();
return Ok(Some(*command));
}
Ok(None)
}
}
#[cfg(test)]
fn parse(input: &str) -> Result<Option<ShortcutCommand>, Error<'_>> {
let mut toks = Tokenizer::new(input);
Ok(ShortcutCommand::parse(&mut toks)?)
}
#[test]
fn test_1() {
assert_eq!(parse("ready."), Ok(Some(ShortcutCommand::Ready)));
}
#[test]
fn test_2() {
assert_eq!(parse("ready"), Ok(Some(ShortcutCommand::Ready)));
}
#[test]
fn test_3() {
assert_eq!(parse("author"), Ok(Some(ShortcutCommand::Author)),);
}
#[test]
fn test_4() {
assert_eq!(parse("ready word"), Ok(Some(ShortcutCommand::Ready)));
}
#[test]
fn test_5() {
assert_eq!(parse("blocked"), Ok(Some(ShortcutCommand::Blocked)));
}