nessus_parser/
error.rs

1#[derive(Debug, Clone)]
2pub enum FormatError {
3    MissingAttribute(&'static str),
4    MissingTag(&'static str),
5    RepeatedTag(&'static str),
6    UnexpectedFormat(&'static str),
7    UnexpectedNodeKind,
8    UnexpectedText(Box<str>),
9    UnexpectedXmlAttribute(Box<str>),
10    UnsupportedVersion,
11    ParseInt(std::num::ParseIntError),
12    Xml(Box<roxmltree::Error>),
13    Jiff(jiff::Error),
14    IpAddrParse(std::net::AddrParseError),
15    BoolParse(std::str::ParseBoolError),
16    UnexpectedNode(Box<str>),
17    UnexpectedPingMethod(Box<str>),
18    UnexpectedDeadHostReason(Box<str>),
19    UnexpectedPingTcpResponse(Box<str>),
20    UnexpectedPingFormat(Box<str>),
21    UnexpectedProtocol(Box<str>),
22    MacAdressParse,
23    UnexpectedPluginType(Box<str>),
24    UnexpectedLevel(Box<str>),
25    MissingPluginOutput,
26}
27
28impl From<std::str::ParseBoolError> for FormatError {
29    fn from(err: std::str::ParseBoolError) -> Self {
30        Self::BoolParse(err)
31    }
32}
33
34impl From<std::net::AddrParseError> for FormatError {
35    fn from(err: std::net::AddrParseError) -> Self {
36        Self::IpAddrParse(err)
37    }
38}
39
40impl From<std::num::ParseIntError> for FormatError {
41    fn from(err: std::num::ParseIntError) -> Self {
42        Self::ParseInt(err)
43    }
44}
45
46impl From<roxmltree::Error> for FormatError {
47    fn from(err: roxmltree::Error) -> Self {
48        Self::Xml(Box::from(err))
49    }
50}
51
52impl From<jiff::Error> for FormatError {
53    fn from(err: jiff::Error) -> Self {
54        Self::Jiff(err)
55    }
56}
57
58impl std::fmt::Display for FormatError {
59    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
60        match self {
61            Self::MissingAttribute(attr) => {
62                write!(f, "Missing attribute: {attr}")
63            }
64            Self::MissingTag(tag) => {
65                write!(f, "Missing tag: {tag}")
66            }
67            Self::RepeatedTag(tag) => {
68                write!(f, "Repeated tag: {tag}")
69            }
70            Self::UnexpectedFormat(s) => {
71                write!(f, "Unexpected format: {s}")
72            }
73            Self::UnexpectedNodeKind => {
74                write!(f, "Unexpected NodeKind")
75            }
76            Self::UnexpectedText(s) => {
77                write!(f, "Unexpected text: {s}")
78            }
79            Self::UnexpectedNode(s) => {
80                write!(f, "Unexpected node: {s}")
81            }
82            Self::UnexpectedXmlAttribute(s) => {
83                write!(f, "Unexpected XML attributes: {s}")
84            }
85            Self::UnsupportedVersion => write!(f, "Unsupported version"),
86            Self::UnexpectedPingMethod(s) => write!(f, "Unexpected ping method: {s}"),
87            Self::UnexpectedDeadHostReason(s) => write!(f, "Unexpected dead host reason: {s}"),
88            Self::UnexpectedPingTcpResponse(s) => write!(f, "Unexpected ping TCP response: {s}"),
89            Self::UnexpectedPingFormat(s) => write!(f, "Unexpected ping format: {s}"),
90            Self::UnexpectedProtocol(s) => write!(f, "Unexpected protocol: {s}"),
91            Self::UnexpectedPluginType(s) => write!(f, "Unexpected plugin type: {s}"),
92            Self::UnexpectedLevel(s) => write!(f, "Unexpected level: {s}"),
93            Self::MissingPluginOutput => write!(f, "Missing plugin output"),
94            Self::MacAdressParse => write!(f, "Can't parse MAC address"),
95            Self::ParseInt(err) => write!(f, "{err}"),
96            Self::Xml(err) => write!(f, "{err}"),
97            Self::Jiff(err) => write!(f, "{err}"),
98            Self::IpAddrParse(err) => write!(f, "{err}"),
99            Self::BoolParse(err) => write!(f, "{err}"),
100        }
101    }
102}
103
104impl std::error::Error for FormatError {
105    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
106        match self {
107            Self::ParseInt(err) => Some(err),
108            Self::Xml(err) => Some(err),
109            Self::Jiff(err) => Some(err),
110            Self::IpAddrParse(err) => Some(err),
111            Self::BoolParse(err) => Some(err),
112            Self::MissingAttribute(_)
113            | Self::MissingTag(_)
114            | Self::RepeatedTag(_)
115            | Self::UnexpectedFormat(_)
116            | Self::UnexpectedNodeKind
117            | Self::UnexpectedNode(_)
118            | Self::UnexpectedText(_)
119            | Self::UnexpectedXmlAttribute(_)
120            | Self::UnsupportedVersion
121            | Self::UnexpectedPingMethod(_)
122            | Self::UnexpectedDeadHostReason(_)
123            | Self::UnexpectedPingTcpResponse(_)
124            | Self::UnexpectedPingFormat(_)
125            | Self::UnexpectedProtocol(_)
126            | Self::UnexpectedPluginType(_)
127            | Self::UnexpectedLevel(_)
128            | Self::MissingPluginOutput
129            | Self::MacAdressParse => None,
130        }
131    }
132}