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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use std::error::Error;
use std::fmt;
use std::io;

#[derive(Debug)]
enum ExtcapErrorKind {
    Io,
    Clap,
    Pcap,
    MissingInterface,
    InvalidInterface,
    UnknownStepRequested,
    UserError,
}

/// Extcap specific error
#[derive(Debug)]
pub struct ExtcapError {
    kind: ExtcapErrorKind,
    message: String,
}

impl ExtcapError {
    pub(crate) fn missing_interface() -> Self {
        ExtcapError {
            kind: ExtcapErrorKind::MissingInterface,
            message: "Missing interface".to_string(),
        }
    }

    pub(crate) fn invalid_interface(interface: &str) -> Self {
        ExtcapError {
            kind: ExtcapErrorKind::InvalidInterface,
            message: format!("Invalid interface: {}", interface),
        }
    }

    pub(crate) fn unknown_step() -> Self {
        ExtcapError {
            kind: ExtcapErrorKind::UnknownStepRequested,
            message: "Unknown step requested".to_string(),
        }
    }

    /// Create user error
    pub fn user_error<T: ToString>(msg: T) -> Self {
        ExtcapError {
            kind: ExtcapErrorKind::UserError,
            message: msg.to_string(),
        }
    }
}

impl Error for ExtcapError {}

impl fmt::Display for ExtcapError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.message)
    }
}

impl From<io::Error> for ExtcapError {
    fn from(error: io::Error) -> Self {
        ExtcapError {
            kind: ExtcapErrorKind::Io,
            message: error.to_string(),
        }
    }
}

impl From<clap::Error> for ExtcapError {
    fn from(error: clap::Error) -> Self {
        ExtcapError {
            kind: ExtcapErrorKind::Clap,
            message: error.to_string(),
        }
    }
}

impl From<pcap_file::PcapError> for ExtcapError {
    fn from(error: pcap_file::PcapError) -> Self {
        ExtcapError {
            kind: ExtcapErrorKind::Pcap,
            message: error.to_string(),
        }
    }
}