1use core::fmt;
2
3#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
25pub struct ServerId<'a>(&'a [u8]);
26
27impl<'a> ServerId<'a> {
28 pub const fn new(bytes: &'a [u8]) -> Self {
30 Self(bytes)
31 }
32
33 pub const fn as_bytes(&self) -> &'a [u8] {
35 self.0
36 }
37
38 fn standard(&self) -> Option<(u8, &'a str, u8, u32)> {
40 let [_, medium, m0, m1, m2, block, s0, s1, s2, s3] = *self.0 else { return None };
41 let flag = self.0.get(2..5)?;
42 if ![m0, m1, m2].iter().all(u8::is_ascii_uppercase) {
43 return None;
44 }
45 let manufacturer = core::str::from_utf8(flag).ok()?;
46 Some((medium, manufacturer, block, u32::from_be_bytes([s0, s1, s2, s3])))
47 }
48
49 pub fn manufacturer(&self) -> Option<&'a str> {
51 self.standard().map(|(_, m, _, _)| m)
52 }
53
54 pub fn medium(&self) -> Option<u8> {
56 self.standard().map(|(m, _, _, _)| m)
57 }
58
59 pub fn block(&self) -> Option<u8> {
61 self.standard().map(|(_, _, b, _)| b)
62 }
63
64 pub fn serial(&self) -> Option<u32> {
66 self.standard().map(|(_, _, _, s)| s)
67 }
68}
69
70impl fmt::Display for ServerId<'_> {
71 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
72 match self.standard() {
75 Some((medium, manufacturer, block, serial)) => {
76 write!(f, "{medium} {manufacturer}{block:02x} {serial:08}")
77 }
78 None => self.0.iter().try_for_each(|b| write!(f, "{b:02x}")),
79 }
80 }
81}
82
83impl fmt::Debug for ServerId<'_> {
84 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
85 write!(f, "ServerId({self})")
86 }
87}
88
89impl PartialEq<[u8]> for ServerId<'_> {
90 fn eq(&self, other: &[u8]) -> bool {
91 self.0 == other
92 }
93}
94
95impl PartialEq<&[u8]> for ServerId<'_> {
96 fn eq(&self, other: &&[u8]) -> bool {
97 self.0 == *other
98 }
99}
100
101impl<'a> From<&'a [u8]> for ServerId<'a> {
102 fn from(bytes: &'a [u8]) -> Self {
103 Self(bytes)
104 }
105}
106
107#[cfg(test)]
108mod tests {
109 extern crate std;
110 use super::*;
111 use std::format;
112 use std::string::ToString;
113
114 #[test]
115 fn standard_ids_from_real_meters() {
116 let esy = [0x09, 0x01, b'E', b'S', b'Y', 0x11, 0x03, 0xb5, 0x99, 0xa5];
118 assert_eq!(ServerId::new(&esy).to_string(), "1 ESY11 62232997");
119 let emh = [0x09, 0x01, b'E', b'M', b'H', 0x00, 0x00, 0x41, 0xf0, 0x45];
120 assert_eq!(ServerId::new(&emh).to_string(), "1 EMH00 04321349");
121 assert_eq!(ServerId::new(&emh).medium(), Some(1));
122 assert_eq!(ServerId::new(&emh).block(), Some(0));
123 }
124
125 #[test]
127 fn meter_bytes_never_reach_the_output_as_text() {
128 let gas = [0x0a, 0x07, b'A', b'B', b'C', 0x42, 0, 0, 0, 7];
129 let id = ServerId::new(&gas);
130 assert_eq!((id.medium(), id.block(), id.serial()), (Some(7), Some(0x42), Some(7)));
131 assert_eq!(format!("{id:?}"), "ServerId(7 ABC42 00000007)");
132 #[allow(clippy::op_ref)]
134 let both = id == gas[..] && id == &gas[..];
135 assert!(both);
136 #[allow(clippy::op_ref)]
137 let neither = id != [1u8][..] && id != &[1u8][..];
138 assert!(neither);
139 let evil = [0x0a, 0x01, 0x1b, b'[', b'2', b'J', 0, 0, 0, 1];
140 let shown = ServerId::new(&evil).to_string();
141 assert_eq!(shown, "0a011b5b324a00000001");
142 assert!(shown.bytes().all(|b| b.is_ascii_hexdigit()));
143 assert_eq!(ServerId::new(&evil).manufacturer(), None);
144 assert_eq!(ServerId::new(&[]).to_string(), "");
145 let lower = [0x0a, 0x01, b'e', b'm', b'h', 0, 0, 0, 0, 1];
146 assert_eq!(ServerId::new(&lower).manufacturer(), None);
147 }
148}