canic_core/ids/
subnet.rs

1use crate::impl_storable_bounded;
2use candid::CandidType;
3use derive_more::Display;
4use serde::{Deserialize, Serialize};
5use std::{borrow::Borrow, borrow::Cow, str::FromStr};
6
7///
8/// SubnetRole
9///
10/// A human-readable identifier for a subnet type
11///
12/// Stored as `Cow<'static, str>` so known constants can be zero‑copy while
13/// dynamic values allocate only when needed.
14///
15
16#[derive(
17    CandidType, Clone, Debug, Eq, Ord, Display, PartialOrd, Deserialize, Serialize, PartialEq, Hash,
18)]
19#[serde(transparent)]
20pub struct SubnetRole(pub Cow<'static, str>);
21
22impl SubnetRole {
23    pub const PRIME: Self = Self(Cow::Borrowed("prime"));
24
25    #[must_use]
26    pub const fn new(s: &'static str) -> Self {
27        Self(Cow::Borrowed(s))
28    }
29
30    #[must_use]
31    pub const fn owned(s: String) -> Self {
32        Self(Cow::Owned(s))
33    }
34
35    #[must_use]
36    pub fn as_str(&self) -> &str {
37        &self.0
38    }
39
40    /// Returns true if this type represents the built-in ROOT canister.
41    #[must_use]
42    pub fn is_prime(&self) -> bool {
43        self.0.as_ref() == "prime"
44    }
45
46    /// Convert into an owned string (avoids an extra allocation for owned variants).
47    #[must_use]
48    pub fn into_string(self) -> String {
49        self.0.into_owned()
50    }
51}
52
53impl FromStr for SubnetRole {
54    type Err = String;
55
56    fn from_str(s: &str) -> Result<Self, Self::Err> {
57        Ok(Self::owned(s.to_string()))
58    }
59}
60
61impl From<&'static str> for SubnetRole {
62    fn from(s: &'static str) -> Self {
63        Self(Cow::Borrowed(s))
64    }
65}
66
67impl From<&String> for SubnetRole {
68    fn from(s: &String) -> Self {
69        Self(Cow::Owned(s.clone()))
70    }
71}
72
73impl From<String> for SubnetRole {
74    fn from(s: String) -> Self {
75        Self(Cow::Owned(s))
76    }
77}
78
79impl From<SubnetRole> for String {
80    fn from(ct: SubnetRole) -> Self {
81        ct.into_string()
82    }
83}
84
85impl AsRef<str> for SubnetRole {
86    fn as_ref(&self) -> &str {
87        self.as_str()
88    }
89}
90
91impl Borrow<str> for SubnetRole {
92    fn borrow(&self) -> &str {
93        self.as_str()
94    }
95}
96
97impl_storable_bounded!(SubnetRole, 64, false);
98
99///
100/// TESTS
101///
102
103#[cfg(test)]
104mod tests {
105    use super::SubnetRole;
106
107    #[test]
108    fn basic_traits_and_utils() {
109        let a = SubnetRole::PRIME;
110        assert!(a.is_prime());
111        assert_eq!(a.as_str(), "prime");
112        let b: SubnetRole = "example".into();
113        assert_eq!(b.as_str(), "example");
114        let s: String = b.clone().into();
115        assert_eq!(s, "example");
116        assert_eq!(b.as_ref(), "example");
117    }
118}