1use serde::{Deserialize, Serialize};
8
9#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
11#[serde(tag = "kind", rename_all = "kebab-case")]
12pub enum Registry {
13 CratesIo,
15 Npm,
17 RubyGems,
19 PyPi,
21 GoProxy,
23 Hex,
25 Hackage,
27 Packagist,
29 Maven,
31 Pub,
33 Oci { registry_url: String },
35 Private {
38 url: String,
39 protocol: String,
40 },
41 None,
44}
45
46impl Registry {
47 #[must_use]
49 pub const fn as_str(&self) -> &'static str {
50 match self {
51 Self::CratesIo => "crates-io",
52 Self::Npm => "npm",
53 Self::RubyGems => "rubygems",
54 Self::PyPi => "pypi",
55 Self::GoProxy => "go-proxy",
56 Self::Hex => "hex",
57 Self::Hackage => "hackage",
58 Self::Packagist => "packagist",
59 Self::Maven => "maven",
60 Self::Pub => "pub",
61 Self::Oci { .. } => "oci",
62 Self::Private { .. } => "private",
63 Self::None => "none",
64 }
65 }
66}
67
68#[cfg(test)]
69mod tests {
70 use super::*;
71
72 #[test]
73 fn as_str_covers_every_variant_stably() {
74 for r in [
75 Registry::CratesIo,
76 Registry::Npm,
77 Registry::RubyGems,
78 Registry::PyPi,
79 Registry::GoProxy,
80 Registry::Hex,
81 Registry::Hackage,
82 Registry::Packagist,
83 Registry::Maven,
84 Registry::Pub,
85 Registry::Oci {
86 registry_url: "ghcr.io".into(),
87 },
88 Registry::Private {
89 url: "https://internal".into(),
90 protocol: "cargo-v1".into(),
91 },
92 Registry::None,
93 ] {
94 assert!(!r.as_str().is_empty());
95 }
96 }
97
98 #[test]
99 fn round_trips_through_serde() {
100 let r = Registry::Oci {
101 registry_url: "ghcr.io/pleme-io".into(),
102 };
103 let j = serde_json::to_string(&r).unwrap();
104 let parsed: Registry = serde_json::from_str(&j).unwrap();
105 assert_eq!(r, parsed);
106 }
107}