use crate::error::Error;
use crate::payload_util::require_exact_len;
use alloc::vec::Vec;
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct Cap {
pub reserved: u8,
}
impl Cap {
pub const fn standard() -> Self {
Self { reserved: 0 }
}
pub fn encode(&self) -> Result<Vec<u8>, Error> {
Ok(alloc::vec![self.reserved])
}
pub fn decode(data: &[u8]) -> Result<Self, Error> {
require_exact_len(data, 1, 0x62)?;
Ok(Self { reserved: data[0] })
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn standard_encodes_zero() {
assert_eq!(Cap::standard().encode().unwrap(), [0x00]);
}
#[test]
fn roundtrip_preserves_reserved() {
let body = Cap { reserved: 0x42 };
let bytes = body.encode().unwrap();
assert_eq!(bytes, [0x42]);
assert_eq!(Cap::decode(&bytes).unwrap(), body);
}
#[test]
fn decode_rejects_wrong_length() {
assert!(matches!(
Cap::decode(&[]),
Err(Error::PayloadLength { code: 0x62, .. })
));
assert!(matches!(
Cap::decode(&[0x00, 0x00]),
Err(Error::PayloadLength { code: 0x62, .. })
));
}
}