1use crate::error::Error;
9use alloc::vec::Vec;
10
11#[derive(Debug, Clone, PartialEq, Eq)]
13pub struct Xrd {
14 pub mode: u8,
16 pub payload: Vec<u8>,
18}
19
20impl Xrd {
21 pub fn encode(&self) -> Result<Vec<u8>, Error> {
23 let mut out = Vec::with_capacity(1 + self.payload.len());
24 out.push(self.mode);
25 out.extend_from_slice(&self.payload);
26 Ok(out)
27 }
28
29 pub fn decode(data: &[u8]) -> Result<Self, Error> {
31 if data.is_empty() {
32 return Err(Error::MalformedPayload {
33 code: 0xB1,
34 reason: "XRD requires XRW_MODE byte",
35 });
36 }
37 Ok(Self {
38 mode: data[0],
39 payload: data[1..].to_vec(),
40 })
41 }
42}