refactor(atward): Construct an atward query type from user requests

Rather than dealing with passing down the rouille request to
handlers (which would have become necessary as we start supporting
more user-controlled features), a new `atward::Query` type is
constructed from requests and passed to the dispatching logic instead.

For now this introduces no new features, it just shuffles things
around to prepare for that.

Change-Id: I08e18422c1fdbac4712c739a7acbb810ada697ca
Reviewed-on: https://cl.tvl.fyi/c/depot/+/3101
Tested-by: BuildkiteCI
Reviewed-by: tazjin <mail@tazj.in>
This commit is contained in:
Vincent Ambo 2021-05-06 16:11:45 +02:00 committed by tazjin
parent 8c2084d0a1
commit d4bdfe8127

View file

@ -19,7 +19,33 @@ struct Handler {
/// ///
/// Returning `None` causes atward to fall through to the next /// Returning `None` causes atward to fall through to the next
/// query (and eventually to the default search engine). /// query (and eventually to the default search engine).
target: for<'s> fn(&'s str, regex::Captures<'s>) -> Option<String>, target: for<'s> fn(&Query, regex::Captures<'s>) -> Option<String>,
}
/// An Atward query supplied by a user.
struct Query {
/// Query string itself.
query: String,
}
impl Query {
fn from_request(req: &rouille::Request) -> Option<Query> {
let query = match req.get_param("q") {
Some(q) => q,
None => return None,
};
Some(Query { query })
}
}
#[cfg(test)]
impl From<&str> for Query {
fn from(query: &str) -> Query {
Query {
query: query.to_string(),
}
}
} }
/// Create a URL to a file (and, optionally, specific line) in cgit. /// Create a URL to a file (and, optionally, specific line) in cgit.
@ -55,10 +81,10 @@ fn handlers() -> Vec<Handler> {
/// Attempt to match against all known query types, and return the /// Attempt to match against all known query types, and return the
/// destination URL if one is found. /// destination URL if one is found.
fn dispatch(queries: &[Handler], uri: &str) -> Option<String> { fn dispatch(handlers: &[Handler], query: &Query) -> Option<String> {
for query in queries { for handler in handlers {
if let Some(captures) = query.pattern.captures(uri) { if let Some(captures) = handler.pattern.captures(&query.query) {
if let Some(destination) = (query.target)(uri, captures) { if let Some(destination) = (handler.target)(query, captures) {
return Some(destination); return Some(destination);
} }
} }
@ -79,7 +105,7 @@ fn main() {
rouille::start_server(&address, move |request| { rouille::start_server(&address, move |request| {
rouille::log(&request, std::io::stderr(), || { rouille::log(&request, std::io::stderr(), || {
let query = match request.get_param("q") { let query = match Query::from_request(&request) {
Some(q) => q, Some(q) => q,
None => return fallback(), None => return fallback(),
}; };
@ -99,40 +125,43 @@ mod tests {
#[test] #[test]
fn bug_query() { fn bug_query() {
assert_eq!( assert_eq!(
dispatch(&handlers(), "b/42"), dispatch(&handlers(), &"b/42".into()),
Some("https://b.tvl.fyi/42".to_string()) Some("https://b.tvl.fyi/42".to_string())
); );
assert_eq!(dispatch(&handlers(), "something only mentioning b/42"), None,); assert_eq!(
assert_eq!(dispatch(&handlers(), "b/invalid"), None,); dispatch(&handlers(), &"something only mentioning b/42".into()),
None,
);
assert_eq!(dispatch(&handlers(), &"b/invalid".into()), None,);
} }
#[test] #[test]
fn cl_query() { fn cl_query() {
assert_eq!( assert_eq!(
dispatch(&handlers(), "cl/42"), dispatch(&handlers(), &"cl/42".into()),
Some("https://cl.tvl.fyi/42".to_string()) Some("https://cl.tvl.fyi/42".to_string())
); );
assert_eq!( assert_eq!(
dispatch(&handlers(), "something only mentioning cl/42"), dispatch(&handlers(), &"something only mentioning cl/42".into()),
None, None,
); );
assert_eq!(dispatch(&handlers(), "cl/invalid"), None,); assert_eq!(dispatch(&handlers(), &"cl/invalid".into()), None,);
} }
#[test] #[test]
fn depot_path_query() { fn depot_path_query() {
assert_eq!( assert_eq!(
dispatch(&handlers(), "//web/atward/default.nix"), dispatch(&handlers(), &"//web/atward/default.nix".into()),
Some("https://code.tvl.fyi/tree/web/atward/default.nix".to_string()), Some("https://code.tvl.fyi/tree/web/atward/default.nix".to_string()),
); );
assert_eq!( assert_eq!(
dispatch(&handlers(), "//nix/readTree/README.md"), dispatch(&handlers(), &"//nix/readTree/README.md".into()),
Some("https://code.tvl.fyi/about/nix/readTree/README.md".to_string()), Some("https://code.tvl.fyi/about/nix/readTree/README.md".to_string()),
); );
assert_eq!(dispatch(&handlers(), "/not/a/depot/path"), None); assert_eq!(dispatch(&handlers(), &"/not/a/depot/path".into()), None);
} }
} }