about summary refs log tree commit diff
path: root/web/atward/src
diff options
context:
space:
mode:
Diffstat (limited to 'web/atward/src')
-rw-r--r--web/atward/src/main.rs30
1 files changed, 30 insertions, 0 deletions
diff --git a/web/atward/src/main.rs b/web/atward/src/main.rs
index aef41a29b4e7..30b672129b01 100644
--- a/web/atward/src/main.rs
+++ b/web/atward/src/main.rs
@@ -22,6 +22,15 @@ struct Query {
     target: for<'s> fn(&'s str, regex::Captures<'s>) -> Option<String>,
 }
 
+/// Create a URL to a file (and, optionally, specific line) in cgit.
+fn cgit_url(path: &str) -> String {
+    if path.ends_with(".md") {
+        format!("https://code.tvl.fyi/about/{}", path)
+    } else {
+        format!("https://code.tvl.fyi/tree/{}", path)
+    }
+}
+
 /// Definition of all supported queries in atward.
 fn queries() -> Vec<Query> {
     vec![
@@ -35,6 +44,12 @@ fn queries() -> Vec<Query> {
             pattern: Regex::new("^cl/(?P<cl>\\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 {
+            pattern: Regex::new("^//(?P<path>[a-zA-Z].*)$").unwrap(),
+            target: |_, captures| Some(cgit_url(&captures["path"])),
+        },
     ]
 }
 
@@ -104,4 +119,19 @@ mod tests {
         );
         assert_eq!(dispatch(&queries(), "cl/invalid"), None,);
     }
+
+    #[test]
+    fn depot_path_query() {
+        assert_eq!(
+            dispatch(&queries(), "//web/atward/default.nix"),
+            Some("https://code.tvl.fyi/tree/web/atward/default.nix".to_string()),
+        );
+
+        assert_eq!(
+            dispatch(&queries(), "//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);
+    }
 }