Skip to main content

tokio_dbus_xml/
error.rs

1use std::error;
2use std::fmt;
3
4use tokio_dbus_core::signature::SignatureError;
5
6/// Result alias defaulting to the error type of this alias.
7pub type Result<T, E = Error> = std::result::Result<T, E>;
8
9/// An error raised by this crate.
10#[derive(Debug)]
11pub struct Error {
12    path: Box<str>,
13    kind: ErrorKind,
14}
15
16impl Error {
17    pub(crate) fn new<P, K>(path: P, kind: K) -> Self
18    where
19        Box<str>: From<P>,
20        ErrorKind: From<K>,
21    {
22        Self {
23            path: path.into(),
24            kind: kind.into(),
25        }
26    }
27}
28
29impl fmt::Display for Error {
30    #[inline]
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        write!(f, "{}: {}", self.path, self.kind)
33    }
34}
35
36impl error::Error for Error {
37    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
38        match &self.kind {
39            ErrorKind::XmlParser(error) => Some(error),
40            ErrorKind::Signature(error) => Some(error),
41            _ => None,
42        }
43    }
44}
45
46#[derive(Debug, PartialEq, Eq)]
47pub(crate) enum ErrorKind {
48    XmlParser(xmlparser::Error),
49    Signature(SignatureError),
50    UnsupportedElementStart(Box<str>),
51    UnsupportedElementEnd,
52    UnsupportedAttribute(Box<str>),
53    UnsupportedText,
54    MismatchingEnd {
55        expected: Box<str>,
56        actual: Box<str>,
57    },
58    MissingMethodName,
59    MissingInterfaceName,
60    MissingSignalName,
61    MissingPropertyName,
62    MissingPropertyType,
63    MissingPropertyAccess,
64    UnsupportedPropertyAccess(Box<str>),
65    MissingAnnotationName,
66    MissingAnnotationValue,
67    MissingArgumentType,
68    UnsupportedArgumentDirection(Box<str>),
69}
70
71impl From<xmlparser::Error> for ErrorKind {
72    #[inline]
73    fn from(error: xmlparser::Error) -> Self {
74        ErrorKind::XmlParser(error)
75    }
76}
77
78impl From<SignatureError> for ErrorKind {
79    #[inline]
80    fn from(error: SignatureError) -> Self {
81        ErrorKind::Signature(error)
82    }
83}
84
85impl fmt::Display for ErrorKind {
86    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
87        match self {
88            ErrorKind::XmlParser(error) => error.fmt(f),
89            ErrorKind::Signature(error) => error.fmt(f),
90            ErrorKind::UnsupportedElementStart(element) => {
91                write!(f, "Unsupported element: {element}")
92            }
93            ErrorKind::UnsupportedElementEnd => {
94                write!(f, "Unsupported element end")
95            }
96            ErrorKind::UnsupportedAttribute(name) => {
97                write!(f, "Unsupported attribute: {name}")
98            }
99            ErrorKind::UnsupportedText => {
100                write!(f, "Unsupported text")
101            }
102            ErrorKind::MismatchingEnd { expected, actual } => {
103                write!(f, "Mismatching end: expected {expected}, found {actual}",)
104            }
105            ErrorKind::MissingMethodName => {
106                write!(f, "Missing method name")
107            }
108            ErrorKind::MissingInterfaceName => {
109                write!(f, "Missing interface name")
110            }
111            ErrorKind::MissingSignalName => {
112                write!(f, "Missing signal name")
113            }
114            ErrorKind::MissingPropertyName => {
115                write!(f, "Missing property name")
116            }
117            ErrorKind::MissingPropertyType => {
118                write!(f, "Missing property type")
119            }
120            ErrorKind::MissingPropertyAccess => {
121                write!(f, "Missing property access")
122            }
123            ErrorKind::UnsupportedPropertyAccess(value) => {
124                write!(f, "Unsupported property access `{value}`")
125            }
126            ErrorKind::MissingAnnotationName => {
127                write!(f, "Missing annotation name")
128            }
129            ErrorKind::MissingAnnotationValue => {
130                write!(f, "Missing annotation value")
131            }
132            ErrorKind::MissingArgumentType => {
133                write!(f, "Missing argument type")
134            }
135            ErrorKind::UnsupportedArgumentDirection(value) => {
136                write!(f, "Unsupported argument direction `{value}`")
137            }
138        }
139    }
140}