use core::fmt;
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BitError {
UnexpectedEof,
OutOfRange {
field: &'static str,
},
BufferFull,
}
impl fmt::Display for BitError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::UnexpectedEof => write!(f, "unexpected end of bit stream"),
Self::OutOfRange { field } => write!(f, "field width out of range: {field}"),
Self::BufferFull => write!(f, "output buffer is full"),
}
}
}
impl core::error::Error for BitError {}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FrameError {
TooShort {
needed: usize,
got: usize,
},
UnknownPreamble(u8),
ReservedBitsSet,
ChecksumMismatch,
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::UnknownPreamble(b) => write!(f, "unrecognized preamble byte: {b:#04x}"),
Self::ReservedBitsSet => write!(f, "reserved bits before the length field were set"),
Self::ChecksumMismatch => write!(f, "CRC-24Q checksum mismatch"),
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 {
UnknownMessageType(u16),
Bit(BitError),
}
impl fmt::Display for MessageError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::UnknownMessageType(mt) => write!(f, "unknown/undecoded message type: {mt}"),
Self::Bit(e) => write!(f, "{e}"),
}
}
}
impl core::error::Error for MessageError {}
impl From<BitError> for MessageError {
fn from(e: BitError) -> Self {
Self::Bit(e)
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Rtcm3Error {
Frame(FrameError),
Bit(BitError),
Message(MessageError),
}
impl fmt::Display for Rtcm3Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Frame(e) => write!(f, "{e}"),
Self::Bit(e) => write!(f, "{e}"),
Self::Message(e) => write!(f, "{e}"),
}
}
}
impl core::error::Error for Rtcm3Error {}
impl From<FrameError> for Rtcm3Error {
fn from(e: FrameError) -> Self {
Self::Frame(e)
}
}
impl From<BitError> for Rtcm3Error {
fn from(e: BitError) -> Self {
Self::Bit(e)
}
}
impl From<MessageError> for Rtcm3Error {
fn from(e: MessageError) -> Self {
Self::Message(e)
}
}