m-bus-parser 0.5.0

A library for parsing M-Bus frames
Documentation
use m_bus_parser::{mbus_data::MbusData, MbusError, WiredFrame};

pub const FULL_FRAME: [u8; 66] = [
    0x68, 0x3C, 0x3C, 0x68, 0x08, 0x08, 0x72, 0x78, 0x03, 0x49, 0x11, 0x77, 0x04, 0x0E, 0x16, 0x0A,
    0x00, 0x00, 0x00, 0x0C, 0x78, 0x78, 0x03, 0x49, 0x11, 0x04, 0x13, 0x31, 0xD4, 0x00, 0x00, 0x42,
    0x6C, 0x00, 0x00, 0x44, 0x13, 0x00, 0x00, 0x00, 0x00, 0x04, 0x6D, 0x0B, 0x0B, 0xCD, 0x13, 0x02,
    0x27, 0x00, 0x00, 0x09, 0xFD, 0x0E, 0x02, 0x09, 0xFD, 0x0F, 0x06, 0x0F, 0x00, 0x01, 0x75, 0x13,
    0xD3, 0x16,
];

/// Parses the complete wired frame and eagerly consumes every data record.
#[inline(never)]
pub fn parse_full_wired_frame(data: &[u8]) -> Result<usize, MbusError> {
    let parsed = MbusData::<WiredFrame>::try_from(data)?;
    if let Some(error) = parsed.application_error {
        return Err(error.into());
    }

    let mut record_count = 0;
    if let Some(records) = parsed.data_records {
        for record in records {
            let record = record?;
            core::hint::black_box(record);
            record_count += 1;
        }
    }
    Ok(record_count)
}

#[cfg(test)]
mod tests {
    #[test]
    fn consumes_every_record() {
        assert_eq!(super::parse_full_wired_frame(&super::FULL_FRAME), Ok(9));
    }
}