aienos.com
Source of www.aienos.com, served from my own hardware
Brought by drakestapleton
aienos.com / crates / site-builder / src / forge.rs
| 1 | //! The one table of forge repositories and their Radicle IDs. |
| 2 | //! |
| 3 | //! Source: `data/public-repos.md` (the public table of RIDS.md). Every row is a |
| 4 | //! repository served by the forge at `FORGE/<repo>`; nothing else is. |
| 5 | |
| 6 | use maud::{html, Markup}; |
| 7 | use std::sync::LazyLock; |
| 8 | |
| 9 | pub const FORGE: &str = "https://interplane.io/drakestapleton"; |
| 10 | pub const RADNET: &str = "https://radicle.network/nodes/rosa.radicle.network"; |
| 11 | |
| 12 | const TABLE: &str = include_str!("../../../data/public-repos.md"); |
| 13 | |
| 14 | static ROWS: LazyLock<Vec<(&'static str, &'static str)>> = LazyLock::new(|| { |
| 15 | TABLE |
| 16 | .lines() |
| 17 | .filter_map(|l| { |
| 18 | let mut cells = l.strip_prefix('|')?.split('|').map(str::trim); |
| 19 | let (repo, rid) = (cells.next()?, cells.next()?.strip_prefix("rad:")?); |
| 20 | (rid.starts_with('z') && !repo.is_empty()).then_some((repo, rid)) |
| 21 | }) |
| 22 | .collect() |
| 23 | }); |
| 24 | |
| 25 | /// Radicle ID (without `rad:`) of a forge repository. |
| 26 | pub fn rid(repo: &str) -> Option<&'static str> { |
| 27 | ROWS.iter().find(|(r, _)| *r == repo).map(|(_, id)| *id) |
| 28 | } |
| 29 | |
| 30 | /// Forge page of a repository, when it is on the forge. |
| 31 | pub fn repo_url(repo: &str) -> Option<String> { |
| 32 | rid(repo).map(|_| format!("{FORGE}/{repo}")) |
| 33 | } |
| 34 | |
| 35 | /// A repository link is valid only as a forge repo page or a rosa radicle.network repo. |
| 36 | pub fn valid_repo_link(url: &str) -> bool { |
| 37 | if let Some(seg) = url.strip_prefix(FORGE).and_then(|r| r.strip_prefix('/')) { |
| 38 | return !seg.is_empty() |
| 39 | && !seg.chars().all(|c| c == '.') |
| 40 | && seg.chars().all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '_' | '-')); |
| 41 | } |
| 42 | if let Some(id) = url.strip_prefix(RADNET).and_then(|r| r.strip_prefix("/rad:z")) { |
| 43 | return !id.is_empty() && id.chars().all(|c| c.is_ascii_alphanumeric()); |
| 44 | } |
| 45 | false |
| 46 | } |
| 47 | |
| 48 | /// `(rid, sha)` for a forge href pinned to a commit: `FORGE/<repo>/(commit|blob|tree)/<7-40 hex>...`. |
| 49 | pub fn receipt(href: &str) -> Option<(&'static str, &str)> { |
| 50 | let rest = href.strip_prefix(FORGE)?.strip_prefix('/')?; |
| 51 | let mut parts = rest.splitn(4, '/'); |
| 52 | let (repo, kind, sha) = (parts.next()?, parts.next()?, parts.next()?); |
| 53 | let sha = sha.split(['#', '?']).next()?; |
| 54 | let hex = (7..=40).contains(&sha.len()) && sha.bytes().all(|b| b.is_ascii_hexdigit() && !b.is_ascii_uppercase()); |
| 55 | (matches!(kind, "commit" | "blob" | "tree") && hex).then_some(())?; |
| 56 | Some((rid(repo)?, sha)) |
| 57 | } |
| 58 | |
| 59 | /// The plain-text receipt that follows `</a>` for a commit-pinned forge href, else nothing. |
| 60 | pub fn receipt_code(href: &str) -> Markup { |
| 61 | html! { |
| 62 | @if let Some((rid, sha)) = receipt(href) { |
| 63 | " " code { "rad:" (rid) " " (sha) } |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | #[cfg(test)] |
| 69 | mod tests { |
| 70 | use super::*; |
| 71 | |
| 72 | #[test] |
| 73 | fn table_holds_the_forge_repos() { |
| 74 | assert_eq!(rid("aienos.com"), Some("z3iwnx2pktbGhnfBpJSsaFcTmKgHU")); |
| 75 | assert_eq!(rid(".github"), Some("z36odegYfUoEoX8xwjJ4so68jb1W3")); |
| 76 | assert_eq!(rid("omega"), None); |
| 77 | assert_eq!(repo_url("omega-mv").as_deref(), Some("https://interplane.io/drakestapleton/omega-mv")); |
| 78 | } |
| 79 | |
| 80 | #[test] |
| 81 | fn receipts_only_for_pinned_forge_hrefs() { |
| 82 | let h = format!("{FORGE}/omega-mv/commit/abc1234"); |
| 83 | assert_eq!(receipt(&h), Some(("z6do9WDBhuSnvopDkgFdFr1yWA62", "abc1234"))); |
| 84 | assert_eq!(receipt_code(&h).into_string(), " <code>rad:z6do9WDBhuSnvopDkgFdFr1yWA62 abc1234</code>"); |
| 85 | assert_eq!(receipt(&format!("{FORGE}/omega-mv/blob/main/x.md")), None); |
| 86 | assert_eq!(receipt("https://github.com/aien-dev/omega-mv/commit/abc1234"), None); |
| 87 | } |
| 88 | |
| 89 | #[test] |
| 90 | fn repo_link_rule() { |
| 91 | assert!(valid_repo_link(&format!("{FORGE}/aienux"))); |
| 92 | assert!(valid_repo_link(&format!("{RADNET}/rad:z3iwnx2pktbGhnfBpJSsaFcTmKgHU"))); |
| 93 | for bad in [FORGE, "https://interplane.io/drakestapleton/..", "https://github.com/aien-dev/omega", "javascript:alert(1)"] { |
| 94 | assert!(!valid_repo_link(bad), "{bad}"); |
| 95 | } |
| 96 | } |
| 97 | } |