1use crate::error::Error;
8use crate::payload_util::require_at_least;
9use alloc::vec::Vec;
10
11#[derive(Debug, Clone, PartialEq, Eq)]
13pub struct Mfg {
14 pub oui: [u8; 3],
16 pub payload: Vec<u8>,
18}
19
20impl Mfg {
21 pub fn encode(&self) -> Result<Vec<u8>, Error> {
23 let mut out = Vec::with_capacity(3 + self.payload.len());
24 out.extend_from_slice(&self.oui);
25 out.extend_from_slice(&self.payload);
26 Ok(out)
27 }
28
29 pub fn decode(data: &[u8]) -> Result<Self, Error> {
31 require_at_least(data, 3, 0x80)?;
32 let mut oui = [0u8; 3];
33 oui.copy_from_slice(&data[..3]);
34 Ok(Self {
35 oui,
36 payload: data[3..].to_vec(),
37 })
38 }
39}
40
41#[cfg(test)]
42mod tests {
43 use super::*;
44
45 #[test]
46 fn roundtrip() {
47 let body = Mfg {
48 oui: [0x00, 0x06, 0x8E],
49 payload: alloc::vec![0xCA, 0xFE],
50 };
51 let bytes = body.encode().unwrap();
52 assert_eq!(bytes, [0x00, 0x06, 0x8E, 0xCA, 0xFE]);
53 assert_eq!(Mfg::decode(&bytes).unwrap(), body);
54 }
55
56 #[test]
57 fn empty_payload_is_valid() {
58 let body = Mfg {
59 oui: [0xAA, 0xBB, 0xCC],
60 payload: alloc::vec::Vec::new(),
61 };
62 let bytes = body.encode().unwrap();
63 assert_eq!(bytes, [0xAA, 0xBB, 0xCC]);
64 assert_eq!(Mfg::decode(&bytes).unwrap(), body);
65 }
66
67 #[test]
68 fn decode_rejects_short_oui() {
69 assert!(matches!(
70 Mfg::decode(&[0x00, 0x06]),
71 Err(Error::PayloadTooShort { code: 0x80, .. })
72 ));
73 }
74}