use crate::{ByteArray, DecodeError, DecodePacket, EncodeError, EncodePacket};
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct BoolData(bool);
impl BoolData {
#[must_use]
pub const fn new(value: bool) -> Self {
Self(value)
}
#[must_use]
pub const fn value(&self) -> bool {
self.0
}
#[must_use]
pub const fn bytes() -> usize {
1
}
}
impl DecodePacket for BoolData {
fn decode(ba: &mut ByteArray) -> Result<Self, DecodeError> {
let byte = ba.read_byte()?;
match byte {
0x00 => Ok(Self(false)),
0x01 => Ok(Self(true)),
_ => Err(DecodeError::InvalidBoolData),
}
}
}
impl EncodePacket for BoolData {
fn encode(&self, buf: &mut Vec<u8>) -> Result<usize, EncodeError> {
let byte = u8::from(self.0);
buf.push(byte);
Ok(Self::bytes())
}
}