use crate::error::Error;
use crate::payload_util::require_exact_len;
use alloc::vec::Vec;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Chlng {
pub rnd_a: [u8; 8],
}
impl Chlng {
pub const fn new(rnd_a: [u8; 8]) -> Self {
Self { rnd_a }
}
pub fn encode(&self) -> Result<Vec<u8>, Error> {
Ok(self.rnd_a.to_vec())
}
pub fn decode(data: &[u8]) -> Result<Self, Error> {
require_exact_len(data, 8, 0x76)?;
let mut rnd_a = [0u8; 8];
rnd_a.copy_from_slice(data);
Ok(Self { rnd_a })
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn roundtrip() {
let body = Chlng::new([0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88]);
let bytes = body.encode().unwrap();
assert_eq!(bytes.len(), 8);
assert_eq!(Chlng::decode(&bytes).unwrap(), body);
}
#[test]
fn decode_rejects_wrong_length() {
assert!(matches!(
Chlng::decode(&[0; 7]),
Err(Error::PayloadLength { code: 0x76, .. })
));
assert!(matches!(
Chlng::decode(&[0; 9]),
Err(Error::PayloadLength { code: 0x76, .. })
));
}
}