From 4de6648ae9ef9584ece94d12c2e5099f75e120ea Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Thu, 6 May 2021 15:54:24 +0200 Subject: refactor(atward): Rename Query -> Handler Query is actually going to be a ... Query. Change-Id: Icc910a8eef47e201054cb1346bc4059c0458659f Reviewed-on: https://cl.tvl.fyi/c/depot/+/3099 Tested-by: BuildkiteCI Reviewed-by: tazjin --- web/atward/src/main.rs | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) (limited to 'web/atward') diff --git a/web/atward/src/main.rs b/web/atward/src/main.rs index 30b672129b..ce858e2629 100644 --- a/web/atward/src/main.rs +++ b/web/atward/src/main.rs @@ -7,10 +7,10 @@ use regex::Regex; use rouille::Response; -/// A query type supported by atward. It consists of a pattern on +/// A query handler supported by atward. It consists of a pattern on /// which to match and trigger the query, and a function to execute /// that returns the target URL. -struct Query { +struct Handler { /// Regular expression on which to match the query string. pattern: Regex, @@ -31,22 +31,22 @@ fn cgit_url(path: &str) -> String { } } -/// Definition of all supported queries in atward. -fn queries() -> Vec { +/// Definition of all supported query handlers in atward. +fn handlers() -> Vec { vec![ // Bug IDs (e.g. b/123) - Query { + Handler { pattern: Regex::new("^b/(?P\\d+)$").unwrap(), target: |_, captures| Some(format!("https://b.tvl.fyi/{}", &captures["bug"])), }, // Changelists (e.g. cl/42) - Query { + Handler { pattern: Regex::new("^cl/(?P\\d+)$").unwrap(), target: |_, captures| Some(format!("https://cl.tvl.fyi/{}", &captures["cl"])), }, // Depot paths (e.g. //web/atward or //ops/nixos/whitby/default.nix) // TODO(tazjin): Add support for specifying lines in a query parameter - Query { + Handler { pattern: Regex::new("^//(?P[a-zA-Z].*)$").unwrap(), target: |_, captures| Some(cgit_url(&captures["path"])), }, @@ -55,7 +55,7 @@ fn queries() -> Vec { /// Attempt to match against all known query types, and return the /// destination URL if one is found. -fn dispatch(queries: &[Query], uri: &str) -> Option { +fn dispatch(queries: &[Handler], uri: &str) -> Option { for query in queries { if let Some(captures) = query.pattern.captures(uri) { if let Some(destination) = (query.target)(uri, captures) { @@ -72,7 +72,7 @@ fn fallback() -> Response { } fn main() { - let queries = queries(); + let queries = handlers(); let address = std::env::var("ATWARD_LISTEN_ADDRESS") .expect("ATWARD_LISTEN_ADDRESS environment variable must be set"); @@ -98,40 +98,40 @@ mod tests { #[test] fn bug_query() { assert_eq!( - dispatch(&queries(), "b/42"), + dispatch(&handlers(), "b/42"), Some("https://b.tvl.fyi/42".to_string()) ); - assert_eq!(dispatch(&queries(), "something only mentioning b/42"), None,); - assert_eq!(dispatch(&queries(), "b/invalid"), None,); + assert_eq!(dispatch(&handlers(), "something only mentioning b/42"), None,); + assert_eq!(dispatch(&handlers(), "b/invalid"), None,); } #[test] fn cl_query() { assert_eq!( - dispatch(&queries(), "cl/42"), + dispatch(&handlers(), "cl/42"), Some("https://cl.tvl.fyi/42".to_string()) ); assert_eq!( - dispatch(&queries(), "something only mentioning cl/42"), + dispatch(&handlers(), "something only mentioning cl/42"), None, ); - assert_eq!(dispatch(&queries(), "cl/invalid"), None,); + assert_eq!(dispatch(&handlers(), "cl/invalid"), None,); } #[test] fn depot_path_query() { assert_eq!( - dispatch(&queries(), "//web/atward/default.nix"), + dispatch(&handlers(), "//web/atward/default.nix"), Some("https://code.tvl.fyi/tree/web/atward/default.nix".to_string()), ); assert_eq!( - dispatch(&queries(), "//nix/readTree/README.md"), + dispatch(&handlers(), "//nix/readTree/README.md"), Some("https://code.tvl.fyi/about/nix/readTree/README.md".to_string()), ); - assert_eq!(dispatch(&queries(), "/not/a/depot/path"), None); + assert_eq!(dispatch(&handlers(), "/not/a/depot/path"), None); } } -- cgit 1.4.1