use bitvec::order::Msb0;
use crate::protocol::prelude::common::{
bits::{util::BitReversible, BitDecoder, Frame},
error::Error,
event::{EventDecoder, EventEncoder},
};
pub struct ErrorResponse {
decoder: BitDecoder<Msb0>,
pub code: u16,
pub message: String,
}
impl ErrorResponse {
pub fn new(decoder: BitDecoder<Msb0>) -> Self {
if cfg!(feature = "debug") {
println!("[\x1b[38;5;187mSHDP\x1b[0m] \x1b[38;5;21m0x0002\x1b[0m received");
}
ErrorResponse {
decoder,
code: 0,
message: String::new(),
}
}
}
impl EventDecoder<Msb0> for ErrorResponse {
fn decode(&mut self, frame: Frame<Msb0>) -> Result<(), Error> {
self.code = self.decoder.read_data(16)? as u16;
self.decoder.position += 8;
let mut bytes = Vec::<u8>::new();
for _ in 0..(frame.data_size - 24) / 8 {
bytes.push(self.decoder.read_data(8)? as u8);
}
self.message = String::from_utf8(bytes).unwrap();
Ok(())
}
fn get_responses(
&self,
) -> Result<Vec<Box<dyn EventEncoder<<Msb0 as BitReversible>::Opposite>>>, Error> {
Ok(Vec::new())
}
}