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