Skip to main content

spodes_rs/
obis.rs

1use serde::{Deserialize, Serialize};
2use std::fmt;
3
4/// An OBIS code (Object Identification System) used by COSEM to identify
5/// objects. It is the six-octet `a.b.c.d.e.f` value group.
6#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
7pub struct ObisCode {
8    a: u8,
9    b: u8,
10    c: u8,
11    d: u8,
12    e: u8,
13    f: u8,
14}
15
16impl ObisCode {
17    /// Creates a new OBIS code from its six components.
18    ///
19    /// # Arguments
20    /// * `a`, `b`, `c`, `d`, `e`, `f` - The OBIS code components.
21    ///
22    /// # Returns
23    /// A new `ObisCode`.
24    pub fn new(a: u8, b: u8, c: u8, d: u8, e: u8, f: u8) -> Self {
25        ObisCode { a, b, c, d, e, f }
26    }
27
28    /// Returns the OBIS code as a byte vector.
29    ///
30    /// # Returns
31    /// A six-byte vector representing the OBIS code.
32    pub fn to_bytes(&self) -> Vec<u8> {
33        vec![self.a, self.b, self.c, self.d, self.e, self.f]
34    }
35}
36
37impl fmt::Display for ObisCode {
38    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
39        write!(f, "{}-{}.{}.{}.{}.{}", self.a, self.b, self.c, self.d, self.e, self.f)
40    }
41}