1 | use static_assertions::assert_impl_all; |
2 | use std::{convert::Infallible, error, fmt}; |
3 | use 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 | #[allow (clippy::upper_case_acronyms)] |
10 | #[non_exhaustive ] |
11 | pub enum Error { |
12 | Variant(VariantError), |
13 | /// Invalid bus name. The strings describe why the bus name is neither a valid unique nor |
14 | /// well-known name, respectively. |
15 | InvalidBusName(String, String), |
16 | /// Invalid well-known bus name. |
17 | InvalidWellKnownName(String), |
18 | /// Invalid unique bus name. |
19 | InvalidUniqueName(String), |
20 | /// Invalid interface name. |
21 | InvalidInterfaceName(String), |
22 | /// Invalid member (method or signal) name. |
23 | InvalidMemberName(String), |
24 | /// Invalid error name. |
25 | InvalidErrorName(String), |
26 | } |
27 | |
28 | assert_impl_all!(Error: Send, Sync, Unpin); |
29 | |
30 | impl PartialEq for Error { |
31 | fn eq(&self, other: &Self) -> bool { |
32 | match (self, other) { |
33 | (Self::InvalidBusName(_, _), Self::InvalidBusName(_, _)) => true, |
34 | (Self::InvalidWellKnownName(_), Self::InvalidWellKnownName(_)) => true, |
35 | (Self::InvalidUniqueName(_), Self::InvalidUniqueName(_)) => true, |
36 | (Self::InvalidInterfaceName(_), Self::InvalidInterfaceName(_)) => true, |
37 | (Self::InvalidMemberName(_), Self::InvalidMemberName(_)) => true, |
38 | (Self::InvalidErrorName(_), Self::InvalidErrorName(_)) => true, |
39 | (Self::Variant(s: &Error), Self::Variant(o: &Error)) => s == o, |
40 | (_, _) => false, |
41 | } |
42 | } |
43 | } |
44 | |
45 | impl error::Error for Error { |
46 | fn source(&self) -> Option<&(dyn error::Error + 'static)> { |
47 | match self { |
48 | Error::InvalidBusName(_, _) => None, |
49 | Error::InvalidWellKnownName(_) => None, |
50 | Error::InvalidUniqueName(_) => None, |
51 | Error::InvalidInterfaceName(_) => None, |
52 | Error::InvalidErrorName(_) => None, |
53 | Error::InvalidMemberName(_) => None, |
54 | Error::Variant(e: &Error) => Some(e), |
55 | } |
56 | } |
57 | } |
58 | |
59 | impl fmt::Display for Error { |
60 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
61 | match self { |
62 | Error::Variant(e: &Error) => write!(f, " {e}" ), |
63 | Error::InvalidBusName(unique_err: &String, well_known_err: &String) => { |
64 | write!( |
65 | f, |
66 | "Neither a valid unique ( {unique_err}) nor a well-known ( {well_known_err}) bus name" |
67 | ) |
68 | } |
69 | Error::InvalidWellKnownName(s: &String) => write!(f, "Invalid well-known bus name: {s}" ), |
70 | Error::InvalidUniqueName(s: &String) => write!(f, "Invalid unique bus name: {s}" ), |
71 | Error::InvalidInterfaceName(s: &String) => write!(f, "Invalid interface or error name: {s}" ), |
72 | Error::InvalidErrorName(s: &String) => write!(f, "Invalid interface or error name: {s}" ), |
73 | Error::InvalidMemberName(s: &String) => write!(f, "Invalid method or signal name: {s}" ), |
74 | } |
75 | } |
76 | } |
77 | |
78 | impl From<VariantError> for Error { |
79 | fn from(val: VariantError) -> Self { |
80 | Error::Variant(val) |
81 | } |
82 | } |
83 | |
84 | impl From<Infallible> for Error { |
85 | fn from(i: Infallible) -> Self { |
86 | match i {} |
87 | } |
88 | } |
89 | |
90 | /// Alias for a `Result` with the error type `zbus_names::Error`. |
91 | pub type Result<T> = std::result::Result<T, Error>; |
92 | |