interface_rs/
error.rs

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
use std::error::Error;
use std::fmt;
use std::io;

/// The main error type for the `NetworkInterfaces` library.
///
/// This enum encapsulates all possible errors that can occur within the library.
#[derive(Debug)]
pub enum NetworkInterfacesError {
    /// An I/O error occurred.
    Io(io::Error),
    /// An error occurred while parsing the interfaces file.
    Parser(ParserError),
    /// An error occurred while parsing the `Family` enum.
    FamilyParse(FamilyParseError),
    /// An error occurred while parsing the `Method` enum.
    MethodParse(MethodParseError),
    /// The interfaces file has been modified on disk since it was last loaded.
    FileModified,
    /// A catch-all for other errors.
    Other(String),
}

impl fmt::Display for NetworkInterfacesError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            NetworkInterfacesError::Io(err) => write!(f, "I/O error: {}", err),
            NetworkInterfacesError::Parser(err) => write!(f, "Parser error: {}", err),
            NetworkInterfacesError::FamilyParse(err) => write!(f, "Family parse error: {}", err),
            NetworkInterfacesError::MethodParse(err) => write!(f, "Method parse error: {}", err),
            NetworkInterfacesError::FileModified => write!(
                f,
                "The interfaces file has been modified on disk since it was last loaded."
            ),
            NetworkInterfacesError::Other(msg) => write!(f, "Error: {}", msg),
        }
    }
}

impl Error for NetworkInterfacesError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            NetworkInterfacesError::Io(err) => Some(err),
            NetworkInterfacesError::Parser(err) => Some(err),
            NetworkInterfacesError::FamilyParse(err) => Some(err),
            NetworkInterfacesError::MethodParse(err) => Some(err),
            NetworkInterfacesError::FileModified => None,
            NetworkInterfacesError::Other(_) => None,
        }
    }
}

// Implement conversions from underlying error types to NetworkInterfacesError
impl From<io::Error> for NetworkInterfacesError {
    fn from(err: io::Error) -> Self {
        NetworkInterfacesError::Io(err)
    }
}

impl From<ParserError> for NetworkInterfacesError {
    fn from(err: ParserError) -> Self {
        NetworkInterfacesError::Parser(err)
    }
}

impl From<FamilyParseError> for NetworkInterfacesError {
    fn from(err: FamilyParseError) -> Self {
        NetworkInterfacesError::FamilyParse(err)
    }
}

impl From<MethodParseError> for NetworkInterfacesError {
    fn from(err: MethodParseError) -> Self {
        NetworkInterfacesError::MethodParse(err)
    }
}

/// Represents errors that can occur during parsing of the interfaces file.
#[derive(Debug, Clone)]
pub struct ParserError {
    /// A message describing the parsing error.
    pub message: String,
    /// Optional line number where the error occurred.
    pub line: Option<usize>,
}

impl fmt::Display for ParserError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if let Some(line) = self.line {
            write!(f, "Parser error on line {}: {}", line, self.message)
        } else {
            write!(f, "Parser error: {}", self.message)
        }
    }
}

impl Error for ParserError {}

/// Represents errors that can occur when parsing the `Family` enum.
#[derive(Debug, Clone)]
pub struct FamilyParseError(pub String);

impl fmt::Display for FamilyParseError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Invalid family: {}", self.0)
    }
}

impl Error for FamilyParseError {}

/// Represents errors that can occur when parsing the `Method` enum.
#[derive(Debug, Clone)]
pub struct MethodParseError(pub String);

impl fmt::Display for MethodParseError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Invalid method: {}", self.0)
    }
}

impl Error for MethodParseError {}