1use std::error::Error;
2use std::fmt;
3use std::io;
4
5#[derive(Debug)]
9pub enum NetworkInterfacesError {
10 Io(io::Error),
12 Parser(ParserError),
14 FamilyParse(FamilyParseError),
16 MethodParse(MethodParseError),
18 FileModified,
20 Other(String),
22}
23
24impl fmt::Display for NetworkInterfacesError {
25 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26 match self {
27 NetworkInterfacesError::Io(err) => write!(f, "I/O error: {}", err),
28 NetworkInterfacesError::Parser(err) => write!(f, "Parser error: {}", err),
29 NetworkInterfacesError::FamilyParse(err) => write!(f, "Family parse error: {}", err),
30 NetworkInterfacesError::MethodParse(err) => write!(f, "Method parse error: {}", err),
31 NetworkInterfacesError::FileModified => write!(
32 f,
33 "The interfaces file has been modified on disk since it was last loaded."
34 ),
35 NetworkInterfacesError::Other(msg) => write!(f, "Error: {}", msg),
36 }
37 }
38}
39
40impl Error for NetworkInterfacesError {
41 fn source(&self) -> Option<&(dyn Error + 'static)> {
42 match self {
43 NetworkInterfacesError::Io(err) => Some(err),
44 NetworkInterfacesError::Parser(err) => Some(err),
45 NetworkInterfacesError::FamilyParse(err) => Some(err),
46 NetworkInterfacesError::MethodParse(err) => Some(err),
47 NetworkInterfacesError::FileModified => None,
48 NetworkInterfacesError::Other(_) => None,
49 }
50 }
51}
52
53impl From<io::Error> for NetworkInterfacesError {
55 fn from(err: io::Error) -> Self {
56 NetworkInterfacesError::Io(err)
57 }
58}
59
60impl From<ParserError> for NetworkInterfacesError {
61 fn from(err: ParserError) -> Self {
62 NetworkInterfacesError::Parser(err)
63 }
64}
65
66impl From<FamilyParseError> for NetworkInterfacesError {
67 fn from(err: FamilyParseError) -> Self {
68 NetworkInterfacesError::FamilyParse(err)
69 }
70}
71
72impl From<MethodParseError> for NetworkInterfacesError {
73 fn from(err: MethodParseError) -> Self {
74 NetworkInterfacesError::MethodParse(err)
75 }
76}
77
78#[derive(Debug, Clone)]
80pub struct ParserError {
81 pub message: String,
83 pub line: Option<usize>,
85}
86
87impl fmt::Display for ParserError {
88 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
89 if let Some(line) = self.line {
90 write!(f, "Parser error on line {}: {}", line, self.message)
91 } else {
92 write!(f, "Parser error: {}", self.message)
93 }
94 }
95}
96
97impl Error for ParserError {}
98
99#[derive(Debug, Clone)]
101pub struct FamilyParseError(pub String);
102
103impl fmt::Display for FamilyParseError {
104 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
105 write!(f, "Invalid family: {}", self.0)
106 }
107}
108
109impl Error for FamilyParseError {}
110
111#[derive(Debug, Clone)]
113pub struct MethodParseError(pub String);
114
115impl fmt::Display for MethodParseError {
116 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
117 write!(f, "Invalid method: {}", self.0)
118 }
119}
120
121impl Error for MethodParseError {}