near_kit/types/
network.rs1use std::fmt;
4
5#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
10pub enum Network {
11 #[default]
13 Mainnet,
14 Testnet,
16 Sandbox,
18 Custom,
20}
21
22impl Network {
23 pub fn is_mainnet(&self) -> bool {
25 matches!(self, Network::Mainnet)
26 }
27
28 pub fn is_testnet(&self) -> bool {
30 matches!(self, Network::Testnet)
31 }
32
33 pub fn is_sandbox(&self) -> bool {
35 matches!(self, Network::Sandbox)
36 }
37
38 pub fn as_str(&self) -> &'static str {
40 match self {
41 Network::Mainnet => "mainnet",
42 Network::Testnet => "testnet",
43 Network::Sandbox => "sandbox",
44 Network::Custom => "custom",
45 }
46 }
47}
48
49impl fmt::Display for Network {
50 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51 f.write_str(self.as_str())
52 }
53}
54
55#[cfg(test)]
56mod tests {
57 use super::*;
58
59 #[test]
60 fn test_network_display() {
61 assert_eq!(Network::Mainnet.to_string(), "mainnet");
62 assert_eq!(Network::Testnet.to_string(), "testnet");
63 assert_eq!(Network::Sandbox.to_string(), "sandbox");
64 assert_eq!(Network::Custom.to_string(), "custom");
65 }
66
67 #[test]
68 fn test_network_predicates() {
69 assert!(Network::Mainnet.is_mainnet());
70 assert!(!Network::Mainnet.is_testnet());
71
72 assert!(Network::Testnet.is_testnet());
73 assert!(!Network::Testnet.is_mainnet());
74
75 assert!(Network::Sandbox.is_sandbox());
76 }
77
78 #[test]
79 fn test_default_is_mainnet() {
80 assert_eq!(Network::default(), Network::Mainnet);
81 }
82}