Skip to main content

docker_registry_client/image/
registry.rs

1#[derive(Debug)]
2pub enum FromStrError {
3    UnkownRegistry(String),
4}
5
6#[derive(Debug, PartialEq, Eq, Clone, Hash)]
7pub enum Registry {
8    DockerHub,
9    Github,
10    Google,
11    K8s,
12    Quay,
13    RedHat,
14    Microsoft,
15}
16
17impl std::fmt::Display for FromStrError {
18    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
19        match self {
20            Self::UnkownRegistry(s) => write!(f, "unknown registry: {s}"),
21        }
22    }
23}
24
25impl std::error::Error for FromStrError {}
26
27impl std::fmt::Display for Registry {
28    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
29        f.write_str(self.registry_domain())
30    }
31}
32
33impl std::str::FromStr for Registry {
34    type Err = FromStrError;
35
36    fn from_str(s: &str) -> Result<Self, Self::Err> {
37        match s {
38            "docker.io" | "index.docker.io" => Ok(Registry::DockerHub),
39            "gcr.io" => Ok(Registry::Google),
40            "ghcr.io" => Ok(Registry::Github),
41            "mcr.microsoft.com" => Ok(Registry::Microsoft),
42            "quay.io" => Ok(Registry::Quay),
43            "registry.access.redhat.com" => Ok(Registry::RedHat),
44            "registry.k8s.io" => Ok(Registry::K8s),
45
46            _ => Err(FromStrError::UnkownRegistry(s.to_string())),
47        }
48    }
49}
50
51impl Registry {
52    #[must_use]
53    pub fn registry_domain(&self) -> &str {
54        match self {
55            Self::DockerHub => "index.docker.io",
56            Self::Github => "ghcr.io",
57            Self::Google => "gcr.io",
58            Self::K8s => "registry.k8s.io",
59            Self::Microsoft => "mcr.microsoft.com",
60            Self::Quay => "quay.io",
61            Self::RedHat => "registry.access.redhat.com",
62        }
63    }
64
65    #[must_use]
66    pub fn needs_authentication(&self) -> bool {
67        match self {
68            Self::DockerHub | Self::Github | Self::Quay => true,
69            Self::RedHat | Self::K8s | Self::Google | Self::Microsoft => false,
70        }
71    }
72}