use crate::HamAddrType;
use core::fmt;
use core::fmt::{Debug, Display, Formatter};
pub type Result<T = (), E = Error> = core::result::Result<T, E>;
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum Error {
InvalidSliceLength,
UnsupportedRawNotation,
CallsignTooLong,
HamAddrTooBig,
CannotConvertToEui48(HamAddrType),
CannotConvertToEui64(HamAddrType),
MulticastEui64NotSupported,
Eui48MissingCallsign,
Eui64MissingCallsign,
InvalidCharAt(InvalidCharAt),
InvalidChar(InvalidChar),
InvalidChunk(InvalidChunk),
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidSliceLength => f.write_str("invalid slice length"),
Self::UnsupportedRawNotation => f.write_str("unsupported raw notation"),
Self::CallsignTooLong => f.write_str("callsign too long"),
Self::HamAddrTooBig => f.write_str("HamAddr too big"),
Self::CannotConvertToEui48(kind) => write!(f, "cannot convert {kind:?} to EUI48"),
Self::CannotConvertToEui64(kind) => write!(f, "cannot convert {kind:?} to EUI64"),
Self::MulticastEui64NotSupported => {
f.write_str("multicast EUI64 conversion not supported")
}
Self::Eui48MissingCallsign => f.write_str("EUI48 did not contain a callsign"),
Self::Eui64MissingCallsign => f.write_str("EUI64 did not contain a callsign"),
Self::InvalidCharAt(error) => Display::fmt(error, f),
Self::InvalidChar(error) => Display::fmt(error, f),
Self::InvalidChunk(error) => Display::fmt(error, f),
}
}
}
impl core::error::Error for Error {}
impl From<InvalidCharAt> for Error {
fn from(value: InvalidCharAt) -> Self {
Self::InvalidCharAt(value)
}
}
impl From<InvalidChar> for Error {
fn from(value: InvalidChar) -> Self {
Self::InvalidChar(value)
}
}
impl From<InvalidChunk> for Error {
fn from(value: InvalidChunk) -> Self {
Self::InvalidChunk(value)
}
}
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct InvalidCharAt(pub usize);
impl Display for InvalidCharAt {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "invalid character at index {}", self.0)
}
}
impl core::error::Error for InvalidCharAt {}
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct InvalidChar;
impl Display for InvalidChar {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str("invalid character")
}
}
impl core::error::Error for InvalidChar {}
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct InvalidChunk;
impl Display for InvalidChunk {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str("invalid chunk")
}
}
impl core::error::Error for InvalidChunk {}