Skip to main content

zbus_xml/
error.rs

1use quick_xml::{de::DeError, se::SeError};
2use std::{convert::Infallible, error, fmt};
3use zvariant::Error as VariantError;
4
5/// The error type for `zbus_names`.
6///
7/// The various errors that can be reported by this crate.
8#[derive(Clone, Debug)]
9#[non_exhaustive]
10pub enum Error {
11    Variant(VariantError),
12    /// An XML error from quick_xml
13    QuickXml(DeError),
14    /// An XML serialization error from quick_xml
15    QuickXmlSer(SeError),
16}
17
18impl PartialEq for Error {
19    fn eq(&self, other: &Self) -> bool {
20        match (self, other) {
21            (Self::Variant(s), Self::Variant(o)) => s == o,
22            (Self::QuickXml(_), Self::QuickXml(_)) => false,
23            (Self::QuickXmlSer(_), Self::QuickXmlSer(_)) => 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            Error::QuickXmlSer(e) => Some(e),
35        }
36    }
37}
38
39impl fmt::Display for Error {
40    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41        match self {
42            Error::Variant(e) => write!(f, "{e}"),
43            Error::QuickXml(e) => write!(f, "XML error: {e}"),
44            Error::QuickXmlSer(e) => write!(f, "XML serialization error: {e}"),
45        }
46    }
47}
48
49impl From<VariantError> for Error {
50    fn from(val: VariantError) -> Self {
51        Error::Variant(val)
52    }
53}
54
55impl From<DeError> for Error {
56    fn from(val: DeError) -> Self {
57        Error::QuickXml(val)
58    }
59}
60
61impl From<SeError> for Error {
62    fn from(val: SeError) -> Self {
63        Error::QuickXmlSer(val)
64    }
65}
66
67impl From<Infallible> for Error {
68    fn from(i: Infallible) -> Self {
69        match i {}
70    }
71}
72
73/// Alias for a `Result` with the error type `zbus_xml::Error`.
74pub type Result<T> = std::result::Result<T, Error>;