1use crate::error::Error;
9use alloc::vec::Vec;
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub struct Com {
14 pub address: u8,
16 pub baud: u32,
18}
19
20impl Com {
21 pub fn encode(&self) -> Result<Vec<u8>, Error> {
23 let mut out = Vec::with_capacity(5);
24 out.push(self.address);
25 out.extend_from_slice(&self.baud.to_le_bytes());
26 Ok(out)
27 }
28
29 pub fn decode(data: &[u8]) -> Result<Self, Error> {
31 if data.len() != 5 {
32 return Err(Error::MalformedPayload {
33 code: 0x54,
34 reason: "COM requires 5 bytes",
35 });
36 }
37 Ok(Self {
38 address: data[0],
39 baud: u32::from_le_bytes([data[1], data[2], data[3], data[4]]),
40 })
41 }
42}