use core::fmt;
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ServerId<'a>(&'a [u8]);
impl<'a> ServerId<'a> {
pub const fn new(bytes: &'a [u8]) -> Self {
Self(bytes)
}
pub const fn as_bytes(&self) -> &'a [u8] {
self.0
}
fn standard(&self) -> Option<(u8, &'a str, u8, u32)> {
let [_, medium, m0, m1, m2, block, s0, s1, s2, s3] = *self.0 else { return None };
let flag = self.0.get(2..5)?;
if ![m0, m1, m2].iter().all(u8::is_ascii_uppercase) {
return None;
}
let manufacturer = core::str::from_utf8(flag).ok()?;
Some((medium, manufacturer, block, u32::from_be_bytes([s0, s1, s2, s3])))
}
pub fn manufacturer(&self) -> Option<&'a str> {
self.standard().map(|(_, m, _, _)| m)
}
pub fn medium(&self) -> Option<u8> {
self.standard().map(|(m, _, _, _)| m)
}
pub fn block(&self) -> Option<u8> {
self.standard().map(|(_, _, b, _)| b)
}
pub fn serial(&self) -> Option<u32> {
self.standard().map(|(_, _, _, s)| s)
}
}
impl fmt::Display for ServerId<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.standard() {
Some((medium, manufacturer, block, serial)) => {
write!(f, "{medium} {manufacturer}{block:02x} {serial:08}")
}
None => self.0.iter().try_for_each(|b| write!(f, "{b:02x}")),
}
}
}
impl fmt::Debug for ServerId<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "ServerId({self})")
}
}
impl PartialEq<[u8]> for ServerId<'_> {
fn eq(&self, other: &[u8]) -> bool {
self.0 == other
}
}
impl PartialEq<&[u8]> for ServerId<'_> {
fn eq(&self, other: &&[u8]) -> bool {
self.0 == *other
}
}
impl<'a> From<&'a [u8]> for ServerId<'a> {
fn from(bytes: &'a [u8]) -> Self {
Self(bytes)
}
}
#[cfg(test)]
mod tests {
extern crate std;
use super::*;
use std::format;
use std::string::ToString;
#[test]
fn standard_ids_from_real_meters() {
let esy = [0x09, 0x01, b'E', b'S', b'Y', 0x11, 0x03, 0xb5, 0x99, 0xa5];
assert_eq!(ServerId::new(&esy).to_string(), "1 ESY11 62232997");
let emh = [0x09, 0x01, b'E', b'M', b'H', 0x00, 0x00, 0x41, 0xf0, 0x45];
assert_eq!(ServerId::new(&emh).to_string(), "1 EMH00 04321349");
assert_eq!(ServerId::new(&emh).medium(), Some(1));
assert_eq!(ServerId::new(&emh).block(), Some(0));
}
#[test]
fn meter_bytes_never_reach_the_output_as_text() {
let gas = [0x0a, 0x07, b'A', b'B', b'C', 0x42, 0, 0, 0, 7];
let id = ServerId::new(&gas);
assert_eq!((id.medium(), id.block(), id.serial()), (Some(7), Some(0x42), Some(7)));
assert_eq!(format!("{id:?}"), "ServerId(7 ABC42 00000007)");
#[allow(clippy::op_ref)]
let both = id == gas[..] && id == &gas[..];
assert!(both);
#[allow(clippy::op_ref)]
let neither = id != [1u8][..] && id != &[1u8][..];
assert!(neither);
let evil = [0x0a, 0x01, 0x1b, b'[', b'2', b'J', 0, 0, 0, 1];
let shown = ServerId::new(&evil).to_string();
assert_eq!(shown, "0a011b5b324a00000001");
assert!(shown.bytes().all(|b| b.is_ascii_hexdigit()));
assert_eq!(ServerId::new(&evil).manufacturer(), None);
assert_eq!(ServerId::new(&[]).to_string(), "");
let lower = [0x0a, 0x01, b'e', b'm', b'h', 0, 0, 0, 0, 1];
assert_eq!(ServerId::new(&lower).manufacturer(), None);
}
}