1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
use nom::error::{ErrorKind, ParseError};
use std::error::Error;
use std::fmt;

#[derive(Debug)]
pub enum PcapError {
    Eof,
    ReadError,
    Incomplete,

    HeaderNotRecognized,

    NomError(ErrorKind),
}

impl<I> ParseError<I> for PcapError {
    fn from_error_kind(_input: I, kind: ErrorKind) -> Self {
        PcapError::NomError(kind)
    }
    fn append(_input: I, kind: ErrorKind, _other: Self) -> Self {
        PcapError::NomError(kind)
    }
}

impl fmt::Display for PcapError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            PcapError::Eof => write!(f, "End of file"),
            PcapError::ReadError => write!(f, "Read error"),
            PcapError::Incomplete => write!(f, "Incomplete read"),
            PcapError::HeaderNotRecognized => write!(f, "Header not recognized as PCAP or PCAPNG"),
            PcapError::NomError(e) => write!(f, "Internal parser error {:?}", e),
        }
    }
}

impl Error for PcapError {}