Skip to content

Commit 783d2bf

Browse files
committed
Add concern/resolve command parsing
1 parent f5d1247 commit 783d2bf

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

parser/src/command.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use regex::Regex;
55

66
pub mod assign;
77
pub mod close;
8+
pub mod concern;
89
pub mod nominate;
910
pub mod note;
1011
pub mod ping;
@@ -25,6 +26,7 @@ pub enum Command<'a> {
2526
Shortcut(Result<shortcut::ShortcutCommand, Error<'a>>),
2627
Close(Result<close::CloseCommand, Error<'a>>),
2728
Note(Result<note::NoteCommand, Error<'a>>),
29+
Concern(Result<concern::ConcernCommand, Error<'a>>),
2830
Transfer(Result<transfer::TransferCommand, Error<'a>>),
2931
}
3032

@@ -97,6 +99,11 @@ impl<'a> Input<'a> {
9799
Command::Note,
98100
&original_tokenizer,
99101
));
102+
success.extend(parse_single_command(
103+
concern::ConcernCommand::parse,
104+
Command::Concern,
105+
&original_tokenizer,
106+
));
100107
success.extend(parse_single_command(
101108
ping::PingCommand::parse,
102109
Command::Ping,
@@ -206,6 +213,7 @@ impl<'a> Command<'a> {
206213
Command::Shortcut(r) => r.is_ok(),
207214
Command::Close(r) => r.is_ok(),
208215
Command::Note(r) => r.is_ok(),
216+
Command::Concern(r) => r.is_ok(),
209217
Command::Transfer(r) => r.is_ok(),
210218
}
211219
}
@@ -353,3 +361,27 @@ fn review_ignored() {
353361
assert_eq!(input.next(), None);
354362
}
355363
}
364+
365+
#[test]
366+
fn concern() {
367+
let input = "@bot concern this is my concern";
368+
let mut input = Input::new(input, vec!["bot"]);
369+
assert_eq!(
370+
input.next(),
371+
Some(Command::Concern(Ok(concern::ConcernCommand::Concern {
372+
title: "this is my concern".to_string()
373+
})))
374+
);
375+
}
376+
377+
#[test]
378+
fn resolve() {
379+
let input = "@bot resolve this is my concern";
380+
let mut input = Input::new(input, vec!["bot"]);
381+
assert_eq!(
382+
input.next(),
383+
Some(Command::Concern(Ok(concern::ConcernCommand::Resolve {
384+
title: "this is my concern".to_string()
385+
})))
386+
);
387+
}

parser/src/command/concern.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use crate::error::Error;
2+
use crate::token::{Token, Tokenizer};
3+
use std::fmt;
4+
5+
#[derive(PartialEq, Eq, Debug)]
6+
pub enum ConcernCommand {
7+
Concern { title: String },
8+
Resolve { title: String },
9+
}
10+
11+
#[derive(PartialEq, Eq, Debug)]
12+
pub enum ParseError {
13+
MissingTitle,
14+
}
15+
16+
impl std::error::Error for ParseError {}
17+
impl fmt::Display for ParseError {
18+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
19+
match self {
20+
ParseError::MissingTitle => write!(f, "missing required title"),
21+
}
22+
}
23+
}
24+
25+
impl ConcernCommand {
26+
pub fn parse<'a>(input: &mut Tokenizer<'a>) -> Result<Option<Self>, Error<'a>> {
27+
let mut toks = input.clone();
28+
if let Some(Token::Word(action @ ("concern" | "resolve"))) = toks.peek_token()? {
29+
toks.next_token()?;
30+
31+
let title = toks.take_line()?.trim().to_string();
32+
33+
if title.is_empty() {
34+
return Err(toks.error(ParseError::MissingTitle));
35+
}
36+
37+
let command = if action == "resolve" {
38+
ConcernCommand::Resolve { title }
39+
} else {
40+
ConcernCommand::Concern { title }
41+
};
42+
43+
Ok(Some(command))
44+
} else {
45+
Ok(None)
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)