pub use crate::{packet::PacketError, socket::SocketError};
use std::fmt;
pub enum Error {
Cfg(ConfigError),
Packet(PacketError),
Socket(SocketError),
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Cfg(err) => err.source(),
Self::Packet(err) => err.source(),
Self::Socket(err) => err.source(),
}
}
}
impl fmt::Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Cfg(cerr) => {
write!(f, "configuration error: {cerr:?}")
}
Self::Packet(ferr) => {
write!(f, "packet error: {ferr:?}")
}
Self::Socket(serr) => {
write!(f, "socket error: {serr:?}")
}
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Cfg(cerr) => {
write!(f, "configuration error: {cerr}")
}
Self::Packet(ferr) => {
write!(f, "packet error: {ferr}")
}
Self::Socket(serr) => {
write!(f, "socket error: {serr}")
}
}
}
}
impl From<ConfigError> for Error {
fn from(value: ConfigError) -> Self {
Self::Cfg(value)
}
}
impl From<SocketError> for Error {
fn from(value: SocketError) -> Self {
Self::Socket(value)
}
}
impl From<PacketError> for Error {
fn from(value: PacketError) -> Self {
Self::Packet(value)
}
}
#[derive(Debug)]
pub struct ConfigError {
pub name: &'static str,
pub kind: ConfigErrorKind,
}
impl fmt::Display for ConfigError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} is invalid: ", self.name)?;
match &self.kind {
ConfigErrorKind::Zero => f.write_str("cannot be zero"),
ConfigErrorKind::NonPowerOf2 => f.write_str("must be a power of 2"),
ConfigErrorKind::OutOfRange { size, range } => {
write!(f, "value '{size}' was out of range '{range:?}")
}
ConfigErrorKind::MustSendOrRecv => {
f.write_str("the socket must a tx ring or rx ring or both")
}
}
}
}
impl std::error::Error for ConfigError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
None
}
}
#[derive(Debug)]
pub enum ConfigErrorKind {
Zero,
NonPowerOf2,
OutOfRange {
size: usize,
range: std::ops::Range<usize>,
},
MustSendOrRecv,
}