OXYGENHUB
# f59e9680d880 oxygenhub / crates / hub / src / repos / names.rs
2.7 KiB · Rust · last changed A security contact page; HSTS from [server] hsts_max_age 21 days ago source blame history raw
1
//! Owner and repository names (hub spec §3.2) and the top-level path
2
//! segments no owner may take.
3
4
use std::fmt;
5
6
/// `owner/name`, both validated.
7
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
8
pub struct RepoName {
9
    pub owner: String,
10
    pub name: String,
11
}
12
13
/// Top-level path segments reserved for the hub itself.
14
pub const RESERVED: &[&str] = &[
15
    "api",
16
    "static",
17
    "login",
18
    "logout",
19
    "register",
20
    "settings",
21
    "explore",
22
    "search",
23
    "notifications",
24
    "new",
25
    "orgs",
26
    "admin",
27
    "style.css",
28
    "favicon.ico",
29
    "favicon.svg",
30
    "health",
31
    "avatar",
32
    "help",
33
    "projects",
34
    "assets",
35
    ".well-known",
36
    // Live pages, CSP reports and the security pages (oxygenhub#204, #222, #224, #230).
37
    "live",
38
    "csp-report",
39
    "security.txt",
40
    "security-contact",
41
];
42
43
/// `[A-Za-z0-9][A-Za-z0-9._-]{0,63}`, not `.`/`..`, not ending in `.git`.
44
pub fn valid_segment(s: &str) -> bool {
45
    let mut chars = s.chars();
46
    let Some(first) = chars.next() else { return false };
47
    if !first.is_ascii_alphanumeric() || s.len() > 64 {
48
        return false;
49
    }
50
    if !chars.all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '_' | '-')) {
51
        return false;
52
    }
53
    s != "." && s != ".." && !s.ends_with(".git")
54
}
55
56
pub fn valid_owner(s: &str) -> bool {
57
    valid_segment(s) && !RESERVED.contains(&s)
58
}
59
60
impl RepoName {
61
    pub fn new(owner: &str, name: &str) -> Option<RepoName> {
62
        if valid_owner(owner) && valid_segment(name) {
63
            Some(RepoName { owner: owner.into(), name: name.into() })
64
        } else {
65
            None
66
        }
67
    }
68
69
    /// `owner/name`.
70
    pub fn parse(slug: &str) -> Option<RepoName> {
71
        let (owner, name) = slug.split_once('/')?;
72
        RepoName::new(owner, name)
73
    }
74
75
    pub fn url(&self) -> String {
76
        format!("/{}/{}", self.owner, self.name)
77
    }
78
}
79
80
impl fmt::Display for RepoName {
81
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
82
        write!(f, "{}/{}", self.owner, self.name)
83
    }
84
}
85
86
#[cfg(test)]
87
mod tests {
88
    use super::*;
89
90
    #[test]
91
    fn names() {
92
        assert!(RepoName::parse("andy/oxygen").is_some());
93
        assert!(RepoName::parse("andy/oxy.gen-2_x").is_some());
94
        assert!(RepoName::parse("api/x").is_none(), "reserved owner");
95
        assert!(RepoName::parse("andy/x.git").is_none(), "mirror URL");
96
        assert!(RepoName::parse("andy/..").is_none());
97
        assert!(RepoName::parse(".andy/x").is_none());
98
        assert!(RepoName::parse("andy/a b").is_none());
99
        assert!(RepoName::parse("andy").is_none());
100
        assert!(RepoName::parse(&format!("andy/{}", "a".repeat(65))).is_none());
101
        assert_eq!(RepoName::parse("andy/oxygen").unwrap().url(), "/andy/oxygen");
102
    }
103
}