1 | use quick_xml::de::DeError; |
2 | use static_assertions::assert_impl_all; |
3 | use std::{convert::Infallible, error, fmt}; |
4 | use 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 ] |
11 | pub enum Error { |
12 | Variant(VariantError), |
13 | /// An XML error from quick_xml |
14 | QuickXml(DeError), |
15 | } |
16 | |
17 | assert_impl_all!(Error: Send, Sync, Unpin); |
18 | |
19 | impl PartialEq for Error { |
20 | fn eq(&self, other: &Self) -> bool { |
21 | match (self, other) { |
22 | (Self::Variant(s: &Error), Self::Variant(o: &Error)) => s == o, |
23 | (Self::QuickXml(_), Self::QuickXml(_)) => false, |
24 | (_, _) => false, |
25 | } |
26 | } |
27 | } |
28 | |
29 | impl error::Error for Error { |
30 | fn source(&self) -> Option<&(dyn error::Error + 'static)> { |
31 | match self { |
32 | Error::Variant(e: &Error) => Some(e), |
33 | Error::QuickXml(e: &DeError) => Some(e), |
34 | } |
35 | } |
36 | } |
37 | |
38 | impl fmt::Display for Error { |
39 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
40 | match self { |
41 | Error::Variant(e: &Error) => write!(f, " {e}" ), |
42 | Error::QuickXml(e: &DeError) => write!(f, "XML error: {e}" ), |
43 | } |
44 | } |
45 | } |
46 | |
47 | impl From<VariantError> for Error { |
48 | fn from(val: VariantError) -> Self { |
49 | Error::Variant(val) |
50 | } |
51 | } |
52 | |
53 | impl From<DeError> for Error { |
54 | fn from(val: DeError) -> Self { |
55 | Error::QuickXml(val) |
56 | } |
57 | } |
58 | |
59 | impl 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`. |
66 | pub type Result<T> = std::result::Result<T, Error>; |
67 | |