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    MissingArgumentType,
61    UnsupportedArgumentDirection(Box<str>),
62    MissingArgumentDirection,
63}
64
65impl From<xmlparser::Error> for ErrorKind {
66    #[inline]
67    fn from(error: xmlparser::Error) -> Self {
68        ErrorKind::XmlParser(error)
69    }
70}
71
72impl From<SignatureError> for ErrorKind {
73    #[inline]
74    fn from(error: SignatureError) -> Self {
75        ErrorKind::Signature(error)
76    }
77}
78
79impl fmt::Display for ErrorKind {
80    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
81        match self {
82            ErrorKind::XmlParser(error) => error.fmt(f),
83            ErrorKind::Signature(error) => error.fmt(f),
84            ErrorKind::UnsupportedElementStart(element) => {
85                write!(f, "Unsupported element: {element}")
86            }
87            ErrorKind::UnsupportedElementEnd => {
88                write!(f, "Unsupported element end")
89            }
90            ErrorKind::UnsupportedAttribute(name) => {
91                write!(f, "Unsupported attribute: {name}")
92            }
93            ErrorKind::UnsupportedText => {
94                write!(f, "Unsupported text")
95            }
96            ErrorKind::MismatchingEnd { expected, actual } => {
97                write!(f, "Mismatching end: expected {expected}, found {actual}",)
98            }
99            ErrorKind::MissingMethodName => {
100                write!(f, "Missing method name")
101            }
102            ErrorKind::MissingInterfaceName => {
103                write!(f, "Missing interface name")
104            }
105            ErrorKind::MissingArgumentType => {
106                write!(f, "Missing argument type")
107            }
108            ErrorKind::UnsupportedArgumentDirection(value) => {
109                write!(f, "Unsupported argument direction `{value}`")
110            }
111            ErrorKind::MissingArgumentDirection => {
112                write!(f, "Missing argument direction")
113            }
114        }
115    }
116}