nectar_primitives/
network_id.rs1use derive_more::{Display, From, Into};
12
13#[cfg(feature = "serde")]
14use serde::{Deserialize, Serialize};
15
16#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Display, From, Into)]
18#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
19#[cfg_attr(feature = "serde", serde(transparent))]
20#[display("{_0}")]
21pub struct NetworkId(u64);
22
23impl NetworkId {
24 pub const MAINNET: Self = Self(1);
26
27 pub const TESTNET: Self = Self(10);
29
30 #[inline]
32 pub const fn new(raw: u64) -> Self {
33 Self(raw)
34 }
35
36 #[inline]
38 pub const fn get(self) -> u64 {
39 self.0
40 }
41
42 #[inline]
45 pub const fn to_le_bytes(self) -> [u8; 8] {
46 self.0.to_le_bytes()
47 }
48
49 #[inline]
52 pub const fn to_be_bytes(self) -> [u8; 8] {
53 self.0.to_be_bytes()
54 }
55}
56
57#[cfg(test)]
58mod tests {
59 use super::*;
60
61 #[test]
62 fn canonical_ids() {
63 assert_eq!(NetworkId::MAINNET.get(), 1);
64 assert_eq!(NetworkId::TESTNET.get(), 10);
65 }
66
67 #[test]
68 fn display_is_decimal() {
69 assert_eq!(format!("{}", NetworkId::new(42)), "42");
70 }
71
72 #[test]
73 fn le_be_byte_distinction() {
74 let id = NetworkId::new(0x0102_0304_0506_0708);
75 assert_eq!(
76 id.to_le_bytes(),
77 [0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01]
78 );
79 assert_eq!(
80 id.to_be_bytes(),
81 [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]
82 );
83 }
84}