use core::fmt;
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FrameError {
TooShort {
needed: usize,
got: usize,
},
ChecksumMismatch,
BroadcastResponse,
}
impl fmt::Display for FrameError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::TooShort { needed, got } => {
write!(f, "frame needs at least {needed} bytes, buffer has {got}")
}
Self::ChecksumMismatch => write!(f, "CRC-16 checksum mismatch"),
Self::BroadcastResponse => write!(f, "response frame had the broadcast address (0)"),
}
}
}
impl core::error::Error for FrameError {}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MessageError {
UnknownFunctionCode(u8),
UnexpectedEof,
BufferFull,
ByteCountMismatch {
declared: u8,
available: usize,
},
TooManyItems {
max: usize,
got: usize,
},
}
impl fmt::Display for MessageError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::UnknownFunctionCode(fc) => {
write!(f, "unknown/undecoded function code: {fc:#04x}")
}
Self::UnexpectedEof => write!(f, "unexpected end of PDU data"),
Self::BufferFull => write!(f, "output buffer is full"),
Self::ByteCountMismatch {
declared,
available,
} => {
write!(
f,
"declared byte count {declared} but only {available} bytes available"
)
}
Self::TooManyItems { max, got } => {
write!(f, "{got} items exceeds the maximum of {max}")
}
}
}
}
impl core::error::Error for MessageError {}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MbrtuError {
Frame(FrameError),
Message(MessageError),
}
impl fmt::Display for MbrtuError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Frame(e) => write!(f, "{e}"),
Self::Message(e) => write!(f, "{e}"),
}
}
}
impl core::error::Error for MbrtuError {}
impl From<FrameError> for MbrtuError {
fn from(e: FrameError) -> Self {
Self::Frame(e)
}
}
impl From<MessageError> for MbrtuError {
fn from(e: MessageError) -> Self {
Self::Message(e)
}
}