feat(web/atward): Implement match scaffolding for TVL redirector
atward is going to be a new TVL service, living at atward.tvl.fyi, which users can configure as a search engine in their browser. It will understand a variety of TVL-specific query types (such as bug/CL links or code paths). In the future it might also support features like go-links. This commit configures the initial setup for query matchers in atward and adds an example query type (for bugs). This is not yet wired up to a web server. Change-Id: Ifaf06c3f5cc378eee7894b7576ef583fc89264f0 Reviewed-on: https://cl.tvl.fyi/c/depot/+/3087 Tested-by: BuildkiteCI Reviewed-by: isomer <isomer@tvl.fyi>
This commit is contained in:
parent
35aa79d14b
commit
51b5475f40
5 changed files with 1085 additions and 0 deletions
3
web/atward/.gitignore
vendored
Normal file
3
web/atward/.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
result
|
||||
/target
|
||||
**/*.rs.bk
|
1001
web/atward/Cargo.lock
generated
Normal file
1001
web/atward/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
9
web/atward/Cargo.toml
Normal file
9
web/atward/Cargo.toml
Normal file
|
@ -0,0 +1,9 @@
|
|||
[package]
|
||||
name = "atward"
|
||||
version = "0.1.0"
|
||||
authors = ["Vincent Ambo <mail@tazj.in>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
regex = "1.5"
|
||||
rouille = "3.0"
|
5
web/atward/default.nix
Normal file
5
web/atward/default.nix
Normal file
|
@ -0,0 +1,5 @@
|
|||
{ depot, ... }:
|
||||
|
||||
depot.third_party.naersk.buildPackage {
|
||||
src = ./.;
|
||||
}
|
67
web/atward/src/main.rs
Normal file
67
web/atward/src/main.rs
Normal file
|
@ -0,0 +1,67 @@
|
|||
//! Atward implements TVL's redirection service, living at
|
||||
//! atward.tvl.fyi
|
||||
//!
|
||||
//! This service is designed to be added as a search engine to web
|
||||
//! browsers and attempts to send users to useful locations based on
|
||||
//! their search query (falling back to another search engine).
|
||||
use regex::Regex;
|
||||
|
||||
/// A query type 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 {
|
||||
/// Regular expression on which to match the query string.
|
||||
pattern: Regex,
|
||||
|
||||
/// Function to construct the target URL. If the pattern matches,
|
||||
/// this is invoked with the captured matches and the entire URI.
|
||||
///
|
||||
/// Returning `None` causes atward to fall through to the next
|
||||
/// query (and eventually to the default search engine).
|
||||
target: for<'s> fn(&'s str, regex::Captures<'s>) -> Option<String>,
|
||||
}
|
||||
|
||||
/// Definition of all supported queries in atward.
|
||||
fn queries() -> Vec<Query> {
|
||||
vec![
|
||||
// Bug IDs (e.g. b/123)
|
||||
Query {
|
||||
pattern: Regex::new("^b/(?P<bug>\\d+)$").unwrap(),
|
||||
target: |_, captures| Some(format!("https://b.tvl.fyi/{}", &captures["bug"])),
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
/// Attempt to match against all known query types, and return the
|
||||
/// destination URL if one is found.
|
||||
fn dispatch(queries: &[Query], uri: &str) -> Option<String> {
|
||||
for query in queries {
|
||||
if let Some(captures) = query.pattern.captures(uri) {
|
||||
if let Some(destination) = (query.target)(uri, captures) {
|
||||
return Some(destination);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn bug_query() {
|
||||
assert_eq!(
|
||||
dispatch(&queries(), "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,);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue