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