use core::fmt::{Display, Formatter};
#[derive(core::fmt::Debug, Clone, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum BufferError {
Utf8Error,
InsufficientBufferSize,
VariableByteIntegerError,
IdNotFound,
EncodingError,
DecodingError,
PacketTypeMismatch,
WrongPacketToDecode,
WrongPacketToEncode,
PropertyNotFound,
}
impl Display for BufferError {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
match *self {
BufferError::Utf8Error => write!(f, "Error encountered during UTF8 decoding!"),
BufferError::InsufficientBufferSize => write!(f, "Buffer size is not sufficient for packet!"),
BufferError::VariableByteIntegerError => write!(f, "Error encountered during variable byte integer decoding / encoding!"),
BufferError::IdNotFound => write!(f, "Packet identifier not found!"),
BufferError::EncodingError => write!(f, "Error encountered during packet encoding!"),
BufferError::DecodingError => write!(f, "Error encountered during packet decoding!"),
BufferError::PacketTypeMismatch => write!(f, "Packet type not matched during decoding (Received different packet type than encode type)!"),
BufferError::WrongPacketToDecode => write!(f, "Not able to decode packet, this packet is used just for sending to broker, not receiving by client!"),
BufferError::WrongPacketToEncode => write!(f, "Not able to encode packet, this packet is used only from server to client not the opposite way!"),
BufferError::PropertyNotFound => write!(f, "Property with ID not found!")
}
}
}
#[derive(Debug, Clone, Default)]
pub struct EncodedString<'a> {
pub string: &'a str,
pub len: u16,
}
impl EncodedString<'_> {
pub fn new() -> Self {
Self { string: "", len: 0 }
}
pub fn encoded_len(&self) -> u16 {
self.len + 2
}
}
#[derive(Debug, Clone, Default)]
pub struct BinaryData<'a> {
pub bin: &'a [u8],
pub len: u16,
}
impl BinaryData<'_> {
pub fn new() -> Self {
Self { bin: &[0], len: 0 }
}
pub fn encoded_len(&self) -> u16 {
self.len + 2
}
}
#[derive(Debug, Clone, Default)]
pub struct StringPair<'a> {
pub name: EncodedString<'a>,
pub value: EncodedString<'a>,
}
impl StringPair<'_> {
pub fn new() -> Self {
Self {
name: EncodedString::new(),
value: EncodedString::new(),
}
}
pub fn encoded_len(&self) -> u16 {
self.name.encoded_len() + self.value.encoded_len()
}
}
#[derive(Debug, Default)]
pub struct TopicFilter<'a> {
pub filter: EncodedString<'a>,
pub sub_options: u8,
}
impl TopicFilter<'_> {
pub fn new() -> Self {
Self {
filter: EncodedString::new(),
sub_options: 0,
}
}
pub fn encoded_len(&self) -> u16 {
self.filter.len + 3
}
}