OXYGENHUB
# 34beb1a7e376 oxygenhub / crates / hub / src / repos / names.rs
2.6 KiB · Rust · last changed Profile and organisation pages, with identicons 1 month 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
    "assets",
34
    ".well-known",
35
];
36
37
/// `[A-Za-z0-9][A-Za-z0-9._-]{0,63}`, not `.`/`..`, not ending in `.git`.
38
pub fn valid_segment(s: &str) -> bool {
39
    let mut chars = s.chars();
40
    let Some(first) = chars.next() else { return false };
41
    if !first.is_ascii_alphanumeric() || s.len() > 64 {
42
        return false;
43
    }
44
    if !chars.all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '_' | '-')) {
45
        return false;
46
    }
47
    s != "." && s != ".." && !s.ends_with(".git")
48
}
49
50
pub fn valid_owner(s: &str) -> bool {
51
    valid_segment(s) && !RESERVED.contains(&s)
52
}
53
54
impl RepoName {
55
    pub fn new(owner: &str, name: &str) -> Option<RepoName> {
56
        if valid_owner(owner) && valid_segment(name) {
57
            Some(RepoName { owner: owner.into(), name: name.into() })
58
        } else {
59
            None
60
        }
61
    }
62
63
    /// `owner/name`.
64
    pub fn parse(slug: &str) -> Option<RepoName> {
65
        let (owner, name) = slug.split_once('/')?;
66
        RepoName::new(owner, name)
67
    }
68
69
    pub fn url(&self) -> String {
70
        format!("/{}/{}", self.owner, self.name)
71
    }
72
}
73
74
impl fmt::Display for RepoName {
75
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
76
        write!(f, "{}/{}", self.owner, self.name)
77
    }
78
}
79
80
#[cfg(test)]
81
mod tests {
82
    use super::*;
83
84
    #[test]
85
    fn names() {
86
        assert!(RepoName::parse("andy/oxygen").is_some());
87
        assert!(RepoName::parse("andy/oxy.gen-2_x").is_some());
88
        assert!(RepoName::parse("api/x").is_none(), "reserved owner");
89
        assert!(RepoName::parse("andy/x.git").is_none(), "mirror URL");
90
        assert!(RepoName::parse("andy/..").is_none());
91
        assert!(RepoName::parse(".andy/x").is_none());
92
        assert!(RepoName::parse("andy/a b").is_none());
93
        assert!(RepoName::parse("andy").is_none());
94
        assert!(RepoName::parse(&format!("andy/{}", "a".repeat(65))).is_none());
95
        assert_eq!(RepoName::parse("andy/oxygen").unwrap().url(), "/andy/oxygen");
96
    }
97
}