interface_rs/
error.rs

1use std::error::Error;
2use std::fmt;
3use std::io;
4
5/// The main error type for the `NetworkInterfaces` library.
6///
7/// This enum encapsulates all possible errors that can occur within the library.
8#[derive(Debug)]
9pub enum NetworkInterfacesError {
10    /// An I/O error occurred.
11    Io(io::Error),
12    /// An error occurred while parsing the interfaces file.
13    Parser(ParserError),
14    /// An error occurred while parsing the `Family` enum.
15    FamilyParse(FamilyParseError),
16    /// An error occurred while parsing the `Method` enum.
17    MethodParse(MethodParseError),
18    /// The interfaces file has been modified on disk since it was last loaded.
19    FileModified,
20    /// A catch-all for other errors.
21    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
53// Implement conversions from underlying error types to NetworkInterfacesError
54impl 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/// Represents errors that can occur during parsing of the interfaces file.
79#[derive(Debug, Clone)]
80pub struct ParserError {
81    /// A message describing the parsing error.
82    pub message: String,
83    /// Optional line number where the error occurred.
84    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/// Represents errors that can occur when parsing the `Family` enum.
100#[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/// Represents errors that can occur when parsing the `Method` enum.
112#[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 {}