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 {
WrongLength {
expected: usize,
actual: usize,
},
InvalidHex,
UnsupportedDownlinkFormat(u8),
ChecksumMismatch,
}
impl fmt::Display for FrameError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::WrongLength { expected, actual } => {
write!(f, "expected {expected} hex characters, got {actual}")
}
Self::InvalidHex => write!(f, "invalid hexadecimal in frame"),
Self::UnsupportedDownlinkFormat(df) => {
write!(
f,
"unsupported downlink format: {df} (only DF17 is decoded)"
)
}
Self::ChecksumMismatch => write!(f, "CRC-24 checksum mismatch"),
}
}
}
impl core::error::Error for FrameError {}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MessageError {
UnknownTypeCode(u8),
UnknownVelocitySubtype(u8),
Bit(BitError),
}
impl fmt::Display for MessageError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::UnknownTypeCode(tc) => write!(f, "unknown/undecoded ADS-B type code: {tc}"),
Self::UnknownVelocitySubtype(st) => {
write!(f, "unknown/reserved airborne velocity subtype: {st}")
}
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 AdsbError {
Frame(FrameError),
Bit(BitError),
Message(MessageError),
}
impl fmt::Display for AdsbError {
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 AdsbError {}
impl From<FrameError> for AdsbError {
fn from(e: FrameError) -> Self {
Self::Frame(e)
}
}
impl From<BitError> for AdsbError {
fn from(e: BitError) -> Self {
Self::Bit(e)
}
}
impl From<MessageError> for AdsbError {
fn from(e: MessageError) -> Self {
Self::Message(e)
}
}