1use std::error::Error;
2use std::fmt;
3use std::io;
4
5#[derive(Debug)]
6enum ExtcapErrorKind {
7 Io,
8 Clap,
9 Pcap,
10 MissingInterface,
11 InvalidInterface,
12 UnknownStepRequested,
13 UserError,
14}
15
16#[derive(Debug)]
18pub struct ExtcapError {
19 kind: ExtcapErrorKind,
20 message: String,
21}
22
23impl ExtcapError {
24 pub(crate) fn missing_interface() -> Self {
25 ExtcapError {
26 kind: ExtcapErrorKind::MissingInterface,
27 message: "Missing interface".to_string(),
28 }
29 }
30
31 pub(crate) fn invalid_interface(interface: &str) -> Self {
32 ExtcapError {
33 kind: ExtcapErrorKind::InvalidInterface,
34 message: format!("Invalid interface: {}", interface),
35 }
36 }
37
38 pub(crate) fn unknown_step() -> Self {
39 ExtcapError {
40 kind: ExtcapErrorKind::UnknownStepRequested,
41 message: "Unknown step requested".to_string(),
42 }
43 }
44
45 pub fn user_error<T: ToString>(msg: T) -> Self {
47 ExtcapError {
48 kind: ExtcapErrorKind::UserError,
49 message: msg.to_string(),
50 }
51 }
52}
53
54impl Error for ExtcapError {}
55
56impl fmt::Display for ExtcapError {
57 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
58 write!(f, "{:?}:{}", self.kind, self.message)
59 }
60}
61
62impl From<io::Error> for ExtcapError {
63 fn from(error: io::Error) -> Self {
64 ExtcapError {
65 kind: ExtcapErrorKind::Io,
66 message: error.to_string(),
67 }
68 }
69}
70
71impl From<clap::Error> for ExtcapError {
72 fn from(error: clap::Error) -> Self {
73 ExtcapError {
74 kind: ExtcapErrorKind::Clap,
75 message: format!("{:?}: {}", error.kind(), error),
76 }
77 }
78}
79
80impl From<pcap_file::PcapError> for ExtcapError {
81 fn from(error: pcap_file::PcapError) -> Self {
82 ExtcapError {
83 kind: ExtcapErrorKind::Pcap,
84 message: error.to_string(),
85 }
86 }
87}