miden_objects/address/
network_id.rs1use alloc::boxed::Box;
2use alloc::str::FromStr;
3use alloc::string::ToString;
4
5use bech32::Hrp;
6
7use crate::errors::NetworkIdError;
8
9#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
14pub enum NetworkId {
15 Mainnet,
16 Testnet,
17 Devnet,
18 Custom(Box<CustomNetworkId>),
20}
21
22impl NetworkId {
23 const MAINNET: &str = "mm";
24 const TESTNET: &str = "mtst";
25 const DEVNET: &str = "mdev";
26
27 pub fn new(string: &str) -> Result<Self, NetworkIdError> {
35 Hrp::parse(string)
36 .map(Self::from_hrp)
37 .map_err(|source| NetworkIdError::NetworkIdParseError(source.to_string().into()))
38 }
39
40 pub(crate) fn from_hrp(hrp: Hrp) -> Self {
44 match hrp.as_str() {
45 NetworkId::MAINNET => NetworkId::Mainnet,
46 NetworkId::TESTNET => NetworkId::Testnet,
47 NetworkId::DEVNET => NetworkId::Devnet,
48 _ => NetworkId::Custom(Box::new(CustomNetworkId::from_hrp(hrp))),
49 }
50 }
51
52 pub(crate) fn into_hrp(self) -> Hrp {
56 match self {
57 NetworkId::Mainnet => {
58 Hrp::parse(NetworkId::MAINNET).expect("mainnet hrp should be valid")
59 },
60 NetworkId::Testnet => {
61 Hrp::parse(NetworkId::TESTNET).expect("testnet hrp should be valid")
62 },
63 NetworkId::Devnet => Hrp::parse(NetworkId::DEVNET).expect("devnet hrp should be valid"),
64 NetworkId::Custom(custom) => custom.as_hrp(),
65 }
66 }
67
68 pub fn as_str(&self) -> &str {
70 match self {
71 NetworkId::Mainnet => NetworkId::MAINNET,
72 NetworkId::Testnet => NetworkId::TESTNET,
73 NetworkId::Devnet => NetworkId::DEVNET,
74 NetworkId::Custom(custom) => custom.as_str(),
75 }
76 }
77
78 pub fn is_mainnet(&self) -> bool {
80 matches!(self, NetworkId::Mainnet)
81 }
82
83 pub fn is_testnet(&self) -> bool {
85 matches!(self, NetworkId::Testnet)
86 }
87
88 pub fn is_devnet(&self) -> bool {
90 matches!(self, NetworkId::Devnet)
91 }
92}
93
94impl FromStr for NetworkId {
95 type Err = NetworkIdError;
96
97 fn from_str(string: &str) -> Result<Self, Self::Err> {
101 Self::new(string)
102 }
103}
104
105impl core::fmt::Display for NetworkId {
106 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
107 f.write_str(self.as_str())
108 }
109}
110
111#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
116pub struct CustomNetworkId {
117 hrp: Hrp,
118}
119
120impl CustomNetworkId {
121 pub(crate) fn from_hrp(hrp: Hrp) -> Self {
123 CustomNetworkId { hrp }
124 }
125
126 pub(crate) fn as_hrp(&self) -> Hrp {
128 self.hrp
129 }
130
131 pub fn as_str(&self) -> &str {
133 self.hrp.as_str()
134 }
135}
136
137impl FromStr for CustomNetworkId {
138 type Err = NetworkIdError;
139
140 fn from_str(hrp_str: &str) -> Result<Self, Self::Err> {
145 Ok(CustomNetworkId {
146 hrp: Hrp::parse(hrp_str)
147 .map_err(|source| NetworkIdError::NetworkIdParseError(source.to_string().into()))?,
148 })
149 }
150}
151
152impl core::fmt::Display for CustomNetworkId {
153 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
154 f.write_str(self.as_str())
155 }
156}