use std::net::IpAddr;
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum PacketError {
#[error("{field} cannot describe {actual} bytes; the most it can hold is {limit}")]
TooLong {
field: &'static str,
actual: usize,
limit: usize,
},
#[error("cannot checksum from {src} to {dst}: an IPv4 and an IPv6 address")]
FamilyMismatch {
src: IpAddr,
dst: IpAddr,
},
#[error("nothing here reads ethertype {0:#06x}")]
UnsupportedEtherType(u16),
#[error("{what} needs at least {needed} bytes and got {got}")]
Truncated {
what: &'static str,
needed: usize,
got: usize,
},
}
impl PacketError {
pub(crate) fn too_long(field: &'static str, header: usize, payload: usize) -> Self {
Self::TooLong {
field,
actual: header.saturating_add(payload),
limit: u16::MAX as usize,
}
}
pub(crate) fn truncated(what: &'static str, needed: usize, got: usize) -> Self {
Self::Truncated { what, needed, got }
}
}
pub type Result<T> = std::result::Result<T, PacketError>;