1use crate::error::Error;
8use crate::payload_util::require_exact_len;
9use alloc::vec::Vec;
10
11#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
13pub struct Id {
14 pub reserved: u8,
16}
17
18impl Id {
19 pub const fn standard() -> Self {
21 Self { reserved: 0 }
22 }
23
24 pub fn encode(&self) -> Result<Vec<u8>, Error> {
26 Ok(alloc::vec![self.reserved])
27 }
28
29 pub fn decode(data: &[u8]) -> Result<Self, Error> {
31 require_exact_len(data, 1, 0x61)?;
32 Ok(Self { reserved: data[0] })
33 }
34}
35
36#[cfg(test)]
37mod tests {
38 use super::*;
39
40 #[test]
41 fn standard_encodes_zero() {
42 assert_eq!(Id::standard().encode().unwrap(), [0x00]);
43 }
44
45 #[test]
46 fn roundtrip_preserves_reserved() {
47 let body = Id { reserved: 0x55 };
48 let bytes = body.encode().unwrap();
49 assert_eq!(bytes, [0x55]);
50 assert_eq!(Id::decode(&bytes).unwrap(), body);
51 }
52
53 #[test]
54 fn decode_rejects_wrong_length() {
55 assert!(matches!(
56 Id::decode(&[]),
57 Err(Error::PayloadLength { code: 0x61, .. })
58 ));
59 }
60}