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#[derive(
17 CandidType, Clone, Debug, Eq, Ord, Display, PartialOrd, Deserialize, Serialize, PartialEq, Hash,
18)]
19#[serde(transparent)]
20pub struct SubnetType(pub Cow<'static, str>);
21
22impl SubnetType {
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 #[must_use]
42 pub fn is_prime(&self) -> bool {
43 self.0.as_ref() == "prime"
44 }
45
46 #[must_use]
48 pub fn into_string(self) -> String {
49 self.0.into_owned()
50 }
51}
52
53impl FromStr for SubnetType {
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 SubnetType {
62 fn from(s: &'static str) -> Self {
63 Self(Cow::Borrowed(s))
64 }
65}
66
67impl From<&String> for SubnetType {
68 fn from(s: &String) -> Self {
69 Self(Cow::Owned(s.clone()))
70 }
71}
72
73impl From<String> for SubnetType {
74 fn from(s: String) -> Self {
75 Self(Cow::Owned(s))
76 }
77}
78
79impl From<SubnetType> for String {
80 fn from(ct: SubnetType) -> Self {
81 ct.into_string()
82 }
83}
84
85impl AsRef<str> for SubnetType {
86 fn as_ref(&self) -> &str {
87 self.as_str()
88 }
89}
90
91impl Borrow<str> for SubnetType {
92 fn borrow(&self) -> &str {
93 self.as_str()
94 }
95}
96
97impl_storable_bounded!(SubnetType, 64, false);
98
99#[cfg(test)]
104mod tests {
105 use super::SubnetType;
106 #[test]
107 fn basic_traits_and_utils() {
108 let a = SubnetType::PRIME;
109 assert!(a.is_prime());
110 assert_eq!(a.as_str(), "prime");
111 let b: SubnetType = "example".into();
112 assert_eq!(b.as_str(), "example");
113 let s: String = b.clone().into();
114 assert_eq!(s, "example");
115 assert_eq!(b.as_ref(), "example");
116 }
117}