zbus_xml/
error.rs

1use quick_xml::de::DeError;
2use static_assertions::assert_impl_all;
3use std::{convert::Infallible, error, fmt};
4use zvariant::Error as VariantError;
5
6/// The error type for `zbus_names`.
7///
8/// The various errors that can be reported by this crate.
9#[derive(Clone, Debug)]
10#[non_exhaustive]
11pub enum Error {
12    Variant(VariantError),
13    /// An XML error from quick_xml
14    QuickXml(DeError),
15}
16
17assert_impl_all!(Error: Send, Sync, Unpin);
18
19impl PartialEq for Error {
20    fn eq(&self, other: &Self) -> bool {
21        match (self, other) {
22            (Self::Variant(s), Self::Variant(o)) => s == o,
23            (Self::QuickXml(_), Self::QuickXml(_)) => false,
24            (_, _) => false,
25        }
26    }
27}
28
29impl error::Error for Error {
30    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
31        match self {
32            Error::Variant(e) => Some(e),
33            Error::QuickXml(e) => Some(e),
34        }
35    }
36}
37
38impl fmt::Display for Error {
39    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40        match self {
41            Error::Variant(e) => write!(f, "{e}"),
42            Error::QuickXml(e) => write!(f, "XML error: {e}"),
43        }
44    }
45}
46
47impl From<VariantError> for Error {
48    fn from(val: VariantError) -> Self {
49        Error::Variant(val)
50    }
51}
52
53impl From<DeError> for Error {
54    fn from(val: DeError) -> Self {
55        Error::QuickXml(val)
56    }
57}
58
59impl From<Infallible> for Error {
60    fn from(i: Infallible) -> Self {
61        match i {}
62    }
63}
64
65/// Alias for a `Result` with the error type `zbus_xml::Error`.
66pub type Result<T> = std::result::Result<T, Error>;