Skip to content

Commit b660902

Browse files
committed
Refactor queue parsing
1 parent bb5711d commit b660902

File tree

1 file changed

+55
-26
lines changed

1 file changed

+55
-26
lines changed

site/src/request_handlers/github.rs

Lines changed: 55 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,13 @@ async fn handle_rust_timer(
120120
let msg = match queue {
121121
Ok(cmd) => {
122122
let conn = ctxt.conn().await;
123-
conn.queue_pr(issue.number, cmd.include, cmd.exclude, cmd.runs)
124-
.await;
123+
conn.queue_pr(
124+
issue.number,
125+
cmd.params.include,
126+
cmd.params.exclude,
127+
cmd.params.runs,
128+
)
129+
.await;
125130
format!(
126131
"Awaiting bors try build completion.
127132
@@ -160,40 +165,59 @@ async fn handle_rust_timer(
160165
Ok(github::Response)
161166
}
162167

163-
/// Parses the first occurrence of a `@rust-timer queue <...>` command
168+
/// Parses the first occurrence of a `@rust-timer queue <shared-args>` command
164169
/// in the input string.
165170
fn parse_queue_command(body: &str) -> Option<Result<QueueCommand, String>> {
166-
let prefix = "@rust-timer";
167-
let bot_line = body.lines().find_map(|line| {
168-
line.find(prefix)
169-
.map(|index| line[index + prefix.len()..].trim())
170-
})?;
171-
172-
let args = bot_line.strip_prefix("queue").map(|l| l.trim())?;
173-
let mut args = match parse_command_arguments(args) {
171+
let args = get_command_lines(body, "queue").next()?;
172+
let args = match parse_command_arguments(args) {
174173
Ok(args) => args,
175174
Err(error) => return Some(Err(error)),
176175
};
177-
let mut cmd = QueueCommand {
176+
let params = match parse_benchmark_parameters(args) {
177+
Ok(params) => params,
178+
Err(error) => return Some(Err(error)),
179+
};
180+
181+
Some(Ok(QueueCommand { params }))
182+
}
183+
184+
fn get_command_lines<'a: 'b, 'b>(
185+
body: &'a str,
186+
command: &'b str,
187+
) -> impl Iterator<Item = &'a str> + 'b {
188+
let prefix = "@rust-timer";
189+
body.lines()
190+
.filter_map(move |line| {
191+
line.find(prefix)
192+
.map(|index| line[index + prefix.len()..].trim())
193+
})
194+
.filter_map(move |line| line.strip_prefix(command))
195+
.map(move |l| l.trim())
196+
}
197+
198+
fn parse_benchmark_parameters<'a>(
199+
mut args: HashMap<&'a str, &'a str>,
200+
) -> Result<BenchmarkParameters<'a>, String> {
201+
let mut params = BenchmarkParameters {
178202
include: args.remove("include"),
179203
exclude: args.remove("exclude"),
180204
runs: None,
181205
};
182206
if let Some(runs) = args.remove("runs") {
183207
let Ok(runs) = runs.parse::<u32>() else {
184-
return Some(Err(format!("Cannot parse runs {runs} as a number")));
208+
return Err(format!("Cannot parse runs {runs} as a number"));
185209
};
186-
cmd.runs = Some(runs as i32);
210+
params.runs = Some(runs as i32);
187211
}
188212

189213
if !args.is_empty() {
190-
return Some(Err(format!(
214+
Err(format!(
191215
"Unknown command argument(s) `{}`",
192216
args.into_keys().collect::<Vec<_>>().join(",")
193-
)));
217+
))
218+
} else {
219+
Ok(params)
194220
}
195-
196-
Some(Ok(cmd))
197221
}
198222

199223
/// Parses command arguments from a single line of text.
@@ -219,6 +243,11 @@ fn parse_command_arguments(args: &str) -> Result<HashMap<&str, &str>, String> {
219243

220244
#[derive(Debug)]
221245
struct QueueCommand<'a> {
246+
params: BenchmarkParameters<'a>,
247+
}
248+
249+
#[derive(Debug)]
250+
struct BenchmarkParameters<'a> {
222251
include: Option<&'a str>,
223252
exclude: Option<&'a str>,
224253
runs: Option<i32>,
@@ -291,7 +320,7 @@ Going to do perf runs for a few of these:
291320
#[test]
292321
fn queue_command() {
293322
insta::assert_compact_debug_snapshot!(parse_queue_command("@rust-timer queue"),
294-
@"Some(Ok(QueueCommand { include: None, exclude: None, runs: None }))");
323+
@"Some(Ok(QueueCommand { params: BenchmarkParameters { include: None, exclude: None, runs: None } }))");
295324
}
296325

297326
#[test]
@@ -309,19 +338,19 @@ Going to do perf runs for a few of these:
309338
#[test]
310339
fn queue_command_include() {
311340
insta::assert_compact_debug_snapshot!(parse_queue_command("@rust-timer queue include=abcd,feih"),
312-
@r###"Some(Ok(QueueCommand { include: Some("abcd,feih"), exclude: None, runs: None }))"###);
341+
@r###"Some(Ok(QueueCommand { params: BenchmarkParameters { include: Some("abcd,feih"), exclude: None, runs: None } }))"###);
313342
}
314343

315344
#[test]
316345
fn queue_command_exclude() {
317346
insta::assert_compact_debug_snapshot!(parse_queue_command("@rust-timer queue exclude=foo134,barzbaz41baf"),
318-
@r###"Some(Ok(QueueCommand { include: None, exclude: Some("foo134,barzbaz41baf"), runs: None }))"###);
347+
@r###"Some(Ok(QueueCommand { params: BenchmarkParameters { include: None, exclude: Some("foo134,barzbaz41baf"), runs: None } }))"###);
319348
}
320349

321350
#[test]
322351
fn queue_command_runs() {
323352
insta::assert_compact_debug_snapshot!(parse_queue_command("@rust-timer queue runs=5"),
324-
@"Some(Ok(QueueCommand { include: None, exclude: None, runs: Some(5) }))");
353+
@"Some(Ok(QueueCommand { params: BenchmarkParameters { include: None, exclude: None, runs: Some(5) } }))");
325354
}
326355

327356
#[test]
@@ -333,7 +362,7 @@ Going to do perf runs for a few of these:
333362
#[test]
334363
fn queue_command_combination() {
335364
insta::assert_compact_debug_snapshot!(parse_queue_command("@rust-timer queue include=acda,13asd exclude=c13,DA runs=5"),
336-
@r###"Some(Ok(QueueCommand { include: Some("acda,13asd"), exclude: Some("c13,DA"), runs: Some(5) }))"###);
365+
@r###"Some(Ok(QueueCommand { params: BenchmarkParameters { include: Some("acda,13asd"), exclude: Some("c13,DA"), runs: Some(5) } }))"###);
337366
}
338367

339368
#[test]
@@ -345,18 +374,18 @@ Going to do perf runs for a few of these:
345374
#[test]
346375
fn queue_command_spaces() {
347376
insta::assert_compact_debug_snapshot!(parse_queue_command("@rust-timer queue include=abcd,das "),
348-
@r###"Some(Ok(QueueCommand { include: Some("abcd,das"), exclude: None, runs: None }))"###);
377+
@r###"Some(Ok(QueueCommand { params: BenchmarkParameters { include: Some("abcd,das"), exclude: None, runs: None } }))"###);
349378
}
350379

351380
#[test]
352381
fn queue_command_with_bors() {
353382
insta::assert_compact_debug_snapshot!(parse_queue_command("@bors try @rust-timer queue include=foo,bar"),
354-
@r###"Some(Ok(QueueCommand { include: Some("foo,bar"), exclude: None, runs: None }))"###);
383+
@r###"Some(Ok(QueueCommand { params: BenchmarkParameters { include: Some("foo,bar"), exclude: None, runs: None } }))"###);
355384
}
356385

357386
#[test]
358387
fn queue_command_parameter_order() {
359388
insta::assert_compact_debug_snapshot!(parse_queue_command("@rust-timer queue runs=3 exclude=c,a include=b"),
360-
@r###"Some(Ok(QueueCommand { include: Some("b"), exclude: Some("c,a"), runs: Some(3) }))"###);
389+
@r###"Some(Ok(QueueCommand { params: BenchmarkParameters { include: Some("b"), exclude: Some("c,a"), runs: Some(3) } }))"###);
361390
}
362391
}

0 commit comments

Comments
 (0)