use core::fmt;
use crate::{BvlcFunction, SinkFull};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum BvllError {
TooShort {
length: usize,
},
NotBip {
bvlc_type: u8,
},
LengthMismatch {
declared: usize,
received: usize,
},
UnknownFunction {
function: u8,
},
PayloadLength {
function: BvlcFunction,
length: usize,
},
TableLength {
length: usize,
entry_length: usize,
},
AddressLength {
length: usize,
},
TooLong {
length: usize,
},
UndefinedResultCode {
code: u16,
},
BufferExhausted {
needed: usize,
available: usize,
},
}
impl fmt::Display for BvllError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::TooShort { length } => {
write!(
f,
"BVLL datagram too short: {length} octets, header needs 4"
)
}
Self::NotBip { bvlc_type } => {
write!(f, "BVLC type {bvlc_type:#04X} is not B/IP (0x81)")
}
Self::LengthMismatch { declared, received } => write!(
f,
"BVLC length {declared} does not match the {received}-octet datagram"
),
Self::UnknownFunction { function } => {
write!(f, "unknown BVLC function {function:#04X}")
}
Self::PayloadLength { function, length } => {
write!(f, "{function} cannot carry a {length}-octet payload")
}
Self::TableLength {
length,
entry_length,
} => write!(
f,
"table of {length} octets is not a whole number of {entry_length}-octet entries"
),
Self::AddressLength { length } => {
write!(f, "B/IP address must be 6 octets, got {length}")
}
Self::TooLong { length } => write!(
f,
"BVLL message of {length} octets exceeds the 16-bit BVLC length field"
),
Self::UndefinedResultCode { code } => {
write!(f, "BVLC result code {code:#06X} is not defined")
}
Self::BufferExhausted { needed, available } => {
write!(f, "buffer exhausted: need {needed}, have {available}")
}
}
}
}
impl core::error::Error for BvllError {}
impl From<SinkFull> for BvllError {
fn from(full: SinkFull) -> Self {
Self::BufferExhausted {
needed: full.needed,
available: full.available,
}
}
}