canic_core/ids/
subnet.rs

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