1use crate::error::Error;
6use crate::payload_util::require_exact_len;
7use alloc::vec::Vec;
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11pub struct RMacI {
12 pub r_mac_i: [u8; 16],
14}
15
16impl RMacI {
17 pub fn encode(&self) -> Result<Vec<u8>, Error> {
19 Ok(self.r_mac_i.to_vec())
20 }
21
22 pub fn decode(data: &[u8]) -> Result<Self, Error> {
24 require_exact_len(data, 16, 0x78)?;
25 let mut r_mac_i = [0u8; 16];
26 r_mac_i.copy_from_slice(data);
27 Ok(Self { r_mac_i })
28 }
29}
30
31#[cfg(test)]
32mod tests {
33 use super::*;
34
35 #[test]
36 fn roundtrip() {
37 let body = RMacI {
38 r_mac_i: [0x77; 16],
39 };
40 let bytes = body.encode().unwrap();
41 assert_eq!(bytes.len(), 16);
42 assert_eq!(RMacI::decode(&bytes).unwrap(), body);
43 }
44
45 #[test]
46 fn decode_rejects_wrong_length() {
47 assert!(matches!(
48 RMacI::decode(&[0; 15]),
49 Err(Error::PayloadLength { code: 0x78, .. })
50 ));
51 }
52}