1use crate::cdk::candid::CandidType;
2use crate::impl_storable_bounded;
3use serde::{Deserialize, Serialize};
4use std::{borrow::Borrow, borrow::Cow, fmt, str::FromStr};
5
6const PRIME_ROLE: &str = "prime";
16
17#[derive(
18 CandidType, Clone, Debug, Eq, Ord, 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 #[must_use]
43 pub fn is_prime(&self) -> bool {
44 self.0.as_ref() == PRIME_ROLE
45 }
46
47 #[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 fmt::Display for SubnetRole {
99 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
100 f.write_str(self.as_str())
101 }
102}
103
104impl_storable_bounded!(SubnetRole, 64, false);
105
106#[cfg(test)]
111mod tests {
112 use super::*;
113
114 #[test]
115 fn basic_traits_and_utils() {
116 let a = SubnetRole::PRIME;
117 assert!(a.is_prime());
118 assert_eq!(a.as_str(), "prime");
119 let b: SubnetRole = "example".into();
120 assert_eq!(b.as_str(), "example");
121 let s: String = b.clone().into();
122 assert_eq!(s, "example");
123 assert_eq!(b.as_ref(), "example");
124 }
125}