@@ -28,11 +28,22 @@ enum CommandPart<'a> {
28
28
29
29
pub struct CommandParser {
30
30
prefix : String ,
31
+ parsers : Vec < ParserFn > ,
31
32
}
32
33
33
34
impl CommandParser {
34
35
pub fn new ( prefix : String ) -> Self {
35
- Self { prefix }
36
+ Self {
37
+ prefix,
38
+ parsers : DEFAULT_PARSERS . to_vec ( ) ,
39
+ }
40
+ }
41
+
42
+ pub fn new_try_only ( prefix : String ) -> Self {
43
+ Self {
44
+ prefix,
45
+ parsers : ONLY_TRY_PARSERS . to_vec ( ) ,
46
+ }
36
47
}
37
48
38
49
/// Prefix of the bot, used to invoke commands from PR comments.
@@ -52,7 +63,7 @@ impl CommandParser {
52
63
. filter_map ( |line| match line. find ( & self . prefix ) {
53
64
Some ( index) => {
54
65
let input = & line[ index + self . prefix . len ( ) ..] ;
55
- parse_command ( input)
66
+ parse_command ( input, & self . parsers )
56
67
}
57
68
None => None ,
58
69
} )
@@ -116,9 +127,10 @@ fn extract_text_from_markdown(text: &str) -> String {
116
127
}
117
128
118
129
type ParseResult < T = BorsCommand > = Option < Result < T , CommandParseError > > ;
130
+ type ParserFn = fn ( & CommandPart < ' _ > , & [ CommandPart < ' _ > ] ) -> ParseResult ;
119
131
120
132
// The order of the parsers in the vector is important
121
- const PARSERS : & [ fn ( & CommandPart < ' _ > , & [ CommandPart < ' _ > ] ) -> ParseResult ] = & [
133
+ const DEFAULT_PARSERS : & [ ParserFn ] = & [
122
134
parser_approval,
123
135
parser_unapprove,
124
136
parser_rollup,
@@ -133,12 +145,14 @@ const PARSERS: &[fn(&CommandPart<'_>, &[CommandPart<'_>]) -> ParseResult] = &[
133
145
parser_tree_ops,
134
146
] ;
135
147
136
- fn parse_command ( input : & str ) -> ParseResult {
148
+ const ONLY_TRY_PARSERS : & [ ParserFn ] = & [ parser_try_cancel, parser_try] ;
149
+
150
+ fn parse_command ( input : & str , parsers : & [ ParserFn ] ) -> ParseResult {
137
151
match parse_parts ( input) {
138
152
Ok ( parts) => match parts. as_slice ( ) {
139
153
[ ] => Some ( Err ( CommandParseError :: MissingCommand ) ) ,
140
154
[ command, arguments @ ..] => {
141
- for parser in PARSERS {
155
+ for parser in parsers {
142
156
if let Some ( result) = parser ( command, arguments) {
143
157
return Some ( result) ;
144
158
}
0 commit comments