pcap_parser/
error.rs

1use nom::error::{ErrorKind, ParseError};
2use std::fmt;
3
4/// The error type which is returned when reading a pcap file
5#[derive(Debug, PartialEq)]
6pub enum PcapError<I: Sized> {
7    /// No more data available
8    Eof,
9    /// Buffer capacity is too small, and some full frame cannot be stored
10    BufferTooSmall,
11    /// Expected more data but got EOF
12    UnexpectedEof,
13    /// An error happened during a `read` operation
14    ReadError,
15    /// Last block is incomplete, and no more data available
16    Incomplete(usize),
17
18    /// File could not be recognized as Pcap nor Pcap-NG
19    HeaderNotRecognized,
20
21    /// An error encountered during parsing
22    NomError(I, ErrorKind),
23    /// An error encountered during parsing (owned version)
24    OwnedNomError(Vec<u8>, ErrorKind),
25}
26
27impl<I> PcapError<I> {
28    /// Creates a `PcapError` from input and error kind.
29    pub fn from_data(input: I, errorkind: ErrorKind) -> Self {
30        Self::NomError(input, errorkind)
31    }
32}
33
34impl<I> PcapError<I>
35where
36    I: AsRef<[u8]> + Sized,
37{
38    /// Creates an owned `PcapError` object from borrowed data, cloning object.
39    /// Owned object has `'static` lifetime.
40    pub fn to_owned_vec(&self) -> PcapError<&'static [u8]> {
41        match self {
42            PcapError::Eof => PcapError::Eof,
43            PcapError::BufferTooSmall => PcapError::BufferTooSmall,
44            PcapError::UnexpectedEof => PcapError::UnexpectedEof,
45            PcapError::ReadError => PcapError::ReadError,
46            PcapError::Incomplete(n) => PcapError::Incomplete(*n),
47            PcapError::HeaderNotRecognized => PcapError::HeaderNotRecognized,
48            PcapError::NomError(i, errorkind) => {
49                PcapError::OwnedNomError(i.as_ref().to_vec(), *errorkind)
50            }
51            PcapError::OwnedNomError(v, e) => PcapError::OwnedNomError(v.clone(), *e),
52        }
53    }
54}
55
56impl<I> ParseError<I> for PcapError<I> {
57    fn from_error_kind(input: I, kind: ErrorKind) -> Self {
58        PcapError::NomError(input, kind)
59    }
60    fn append(input: I, kind: ErrorKind, _other: Self) -> Self {
61        PcapError::NomError(input, kind)
62    }
63}
64
65impl<I> fmt::Display for PcapError<I>
66where
67    I: fmt::Debug,
68{
69    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70        match self {
71            PcapError::Eof => write!(f, "End of file"),
72            PcapError::BufferTooSmall => write!(f, "Buffer is too small"),
73            PcapError::UnexpectedEof => write!(f, "Unexpected end of file"),
74            PcapError::ReadError => write!(f, "Read error"),
75            PcapError::Incomplete(n) => write!(f, "Incomplete read: {}", n),
76            PcapError::HeaderNotRecognized => write!(f, "Header not recognized as PCAP or PCAPNG"),
77            PcapError::NomError(i, e) => write!(f, "Internal parser error {:?}, input {:?}", e, i),
78            PcapError::OwnedNomError(i, e) => {
79                write!(f, "Internal parser error {:?}, input {:?}", e, &i)
80            }
81        }
82    }
83}
84
85impl<I> std::error::Error for PcapError<I> where I: fmt::Debug {}