1use crate::error::Error;
2
3#[derive(Debug, Clone, PartialEq, Eq)]
4pub struct Namespace {
5 org: String,
6 repo: String,
7}
8
9impl Namespace {
10 pub fn new(org: impl Into<String>, repo: impl Into<String>) -> Result<Self, Error> {
11 let (org, repo) = (org.into(), repo.into());
12
13 (is_well_formed(&org) && is_well_formed(&repo))
14 .then_some(Self { org, repo })
15 .ok_or(Error::MalformedNamespace)
16 }
17
18 pub fn org(&self) -> &str {
19 &self.org
20 }
21
22 pub fn repo(&self) -> &str {
23 &self.repo
24 }
25}
26
27impl std::fmt::Display for Namespace {
28 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
29 write!(f, "{}/{}", self.org, self.repo)
30 }
31}
32
33fn is_well_formed(segment: &str) -> bool {
34 !segment.is_empty()
35 && segment.len() <= 100
36 && !segment.starts_with('.')
37 && segment
38 .bytes()
39 .all(|b| b.is_ascii_alphanumeric() || matches!(b, b'-' | b'_' | b'.'))
40}
41
42#[cfg(test)]
43mod tests;