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