pcap_file/
errors.rs

1use thiserror::Error;
2
3/// Result type for the pcap/pcapng parsing
4pub type PcapResult<T> = Result<T, PcapError>;
5
6/// Error type for the pcap/pcapng parsing
7#[derive(Error, Debug)]
8pub enum PcapError {
9    /// Buffer too small
10    #[error("Need more bytes")]
11    IncompleteBuffer,
12
13    /// Generic IO error
14    #[error("Error reading/writing bytes")]
15    IoError(#[source] std::io::Error),
16
17    /// Invalid field
18    #[error("Invalid field value: {0}")]
19    InvalidField(&'static str),
20
21    /// UTF8 conversion error
22    #[error("UTF8 error")]
23    Utf8Error(#[source] std::str::Utf8Error),
24
25    /// From UTF8 conversion error
26    #[error("UTF8 error")]
27    FromUtf8Error(#[source] std::string::FromUtf8Error),
28
29    /// Invalid interface ID (only for Pcap NG)
30    #[error("The interface id ({0}) of the current block doesn't exists")]
31    InvalidInterfaceId(u32),
32
33    /// Invalid timestamp resolution (only for Pcap NG)
34    #[error("Invalid timestamp resolution: {0} is not in [0-9]")]
35    InvalidTsResolution(u8),
36
37    /// The packet's timestamp is too big (only for Pcap NG)
38    #[error("Packet's timestamp too big, please choose a bigger timestamp resolution")]
39    TimestampTooBig,
40}
41
42impl From<std::str::Utf8Error> for PcapError {
43    fn from(err: std::str::Utf8Error) -> Self {
44        PcapError::Utf8Error(err)
45    }
46}
47
48impl From<std::string::FromUtf8Error> for PcapError {
49    fn from(err: std::string::FromUtf8Error) -> Self {
50        PcapError::FromUtf8Error(err)
51    }
52}
53
54impl From<std::io::Error> for PcapError {
55    fn from(err: std::io::Error) -> Self {
56        PcapError::IoError(err)
57    }
58}