use core::fmt;
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FrameError {
TooShort {
needed: usize,
got: usize,
},
UnknownMagicByte(u8),
SigningNotSupported,
PayloadTooLong {
max: usize,
got: usize,
},
}
impl fmt::Display for FrameError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::TooShort { needed, got } => {
write!(f, "frame needs {needed} bytes, buffer has {got}")
}
Self::UnknownMagicByte(b) => write!(f, "unrecognized magic byte: {b:#04x}"),
Self::SigningNotSupported => write!(f, "signed v2 frames are not supported"),
Self::PayloadTooLong { max, got } => {
write!(f, "payload of {got} bytes exceeds the {max}-byte maximum")
}
}
}
}
impl core::error::Error for FrameError {}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MessageError {
UnknownMessageId(u32),
ChecksumMismatch,
}
impl fmt::Display for MessageError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::UnknownMessageId(id) => write!(f, "unknown/undecoded message id: {id}"),
Self::ChecksumMismatch => write!(f, "checksum mismatch"),
}
}
}
impl core::error::Error for MessageError {}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MavError {
Frame(FrameError),
Message(MessageError),
}
impl fmt::Display for MavError {
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 MavError {}
impl From<FrameError> for MavError {
fn from(e: FrameError) -> Self {
Self::Frame(e)
}
}
impl From<MessageError> for MavError {
fn from(e: MessageError) -> Self {
Self::Message(e)
}
}