use thiserror::Error;
pub type PcapResult<T> = Result<T, PcapError>;
#[derive(Error, Debug)]
pub enum PcapError {
#[error("Need more bytes to parse the element: needed {0}B, actual {1}B")]
IncompleteBuffer(usize, usize),
#[error("Error reading/writing bytes")]
IoError(#[source] std::io::Error),
#[error("Invalid field value: {0}")]
InvalidField(&'static str),
#[error("UTF8 error")]
Utf8Error(#[source] std::str::Utf8Error),
#[error("UTF8 error")]
FromUtf8Error(#[source] std::string::FromUtf8Error),
#[error("The interface id ({0}) of the current block doesn't exists")]
InvalidInterfaceId(u32),
#[error("Invalid timestamp resolution: {0} is not in [0-9]")]
InvalidTsResolution(u8),
#[error("Packet's timestamp too big, please choose a bigger timestamp resolution")]
TimestampTooBig,
#[error("Packet's included length ({0}) is bigger than the snaplen of the file ({1})")]
PacketTooLarge(u32, u32),
#[error("Error in custom conversion for PEN {0}: {1}")]
CustomConversionError(u32, Box<dyn std::error::Error + Sync + Send>),
}
impl From<std::str::Utf8Error> for PcapError {
fn from(err: std::str::Utf8Error) -> Self {
PcapError::Utf8Error(err)
}
}
impl From<std::string::FromUtf8Error> for PcapError {
fn from(err: std::string::FromUtf8Error) -> Self {
PcapError::FromUtf8Error(err)
}
}
impl From<std::io::Error> for PcapError {
fn from(err: std::io::Error) -> Self {
PcapError::IoError(err)
}
}