1#[allow(clippy::module_name_repetitions)]
2#[derive(Debug)]
3#[non_exhaustive]
4/// An error type that can describe atspi and `std` and different `zbus` errors.
5pub enum AtspiError {
6 /// Converting one type into another failure
7 Conversion(&'static str),
8
9 /// When testing on either variant, we might find the we are not interested in.
10 CacheVariantMismatch,
11
12 /// On specific types, if the event / message member does not match the Event's name.
13 MemberMatch(String),
14
15 /// On specific types, if the event / message member does not match the Event's name.
16 InterfaceMatch(String),
17
18 /// To indicate a match or equality test on a signal body signature failed.
19 UnknownBusSignature(String),
20
21 /// When matching on an unknown interface
22 UnknownInterface,
23
24 /// No interface on event.
25 MissingInterface,
26
27 /// No member on event.
28 MissingMember,
29
30 /// No Signature.
31 MissingSignature,
32
33 /// When matching on an unknown role
34 UnknownRole(u32),
35
36 /// No name on bus.
37 MissingName,
38
39 /// The signal that was encountered is unknown.
40 UnknownSignal,
41
42 /// Other errors.
43 Owned(String),
44
45 /// A `zbus` or `zbus::Fdo` error. variant.
46 Zbus(String),
47
48 /// A `zbus_names` error variant
49 ZBusNames(zbus_names::Error),
50
51 /// A `zbus_names` error variant
52 Zvariant(zvariant::Error),
53
54 /// Failed to parse a string into an enum variant
55 ParseError(&'static str),
56
57 /// Failed to get the ID of a path.
58 PathConversionError(ObjectPathConversionError),
59
60 /// Std i/o error variant.
61 IO(std::io::Error),
62
63 /// Failed to convert an integer into another type of integer (usually i32 -> usize).
64 IntConversionError(std::num::TryFromIntError),
65
66 /// An infallible error; this is just something to satisfy the compiler.
67 Infallible,
68}
69
70impl std::error::Error for AtspiError {}
71
72impl std::fmt::Display for AtspiError {
73 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
74 match self {
75 Self::Conversion(e) => f.write_str(&format!("atspi: conversion failure: {e}")),
76 Self::MemberMatch(e) => {
77 f.write_str(format!("atspi: member mismatch in conversion: {e}").as_str())
78 }
79 Self::InterfaceMatch(e) => {
80 f.write_str(format!("atspi: interface mismatch in conversion: {e}").as_str())
81 }
82 Self::UnknownBusSignature(e) => {
83 f.write_str(format!("atspi: Unknown bus body signature: {e:?}").as_str())
84 }
85 Self::UnknownInterface => f.write_str("Unknown interface."),
86 Self::MissingInterface => f.write_str("Missing interface."),
87 Self::MissingMember => f.write_str("Missing member."),
88 Self::MissingSignature => f.write_str("Missing signature."),
89 Self::UnknownRole(e) => f.write_str(&format!("atspi: Unknown role: {e}")),
90 Self::UnknownSignal => f.write_str("atspi: Unknown signal"),
91 Self::CacheVariantMismatch => f.write_str("atspi: Cache variant mismatch"),
92 Self::Owned(e) => f.write_str(&format!("atspi: other error: {e}")),
93 Self::Zbus(e) => f.write_str(&format!("ZBus Error: {e}")),
94 Self::Zvariant(e) => f.write_str(&format!("Zvariant error: {e}")),
95 Self::ZBusNames(e) => f.write_str(&format!("ZBus_names Error: {e}")),
96 Self::ParseError(e) => f.write_str(e),
97 Self::PathConversionError(e) => {
98 f.write_str(&format!("ID cannot be extracted from the path: {e}"))
99 }
100 Self::IO(e) => f.write_str(&format!("std IO Error: {e}")),
101 Self::IntConversionError(e) => f.write_str(&format!("Integer conversion error: {e}")),
102 Self::MissingName => f.write_str("Missing name for a bus."),
103 Self::Infallible => {
104 f.write_str("Infallible; only to trick the compiler. This should never happen.")
105 }
106 }
107 }
108}
109
110impl From<std::convert::Infallible> for AtspiError {
111 fn from(_e: std::convert::Infallible) -> Self {
112 Self::Infallible
113 }
114}
115impl From<std::num::TryFromIntError> for AtspiError {
116 fn from(e: std::num::TryFromIntError) -> Self {
117 Self::IntConversionError(e)
118 }
119}
120
121#[cfg(feature = "zbus")]
122impl From<zbus::fdo::Error> for AtspiError {
123 fn from(e: zbus::fdo::Error) -> Self {
124 Self::Zbus(format!("{e:?}"))
125 }
126}
127
128#[cfg(feature = "zbus")]
129impl From<zbus::Error> for AtspiError {
130 fn from(e: zbus::Error) -> Self {
131 Self::Zbus(format!("{e:?}"))
132 }
133}
134
135impl From<zbus_names::Error> for AtspiError {
136 fn from(e: zbus_names::Error) -> Self {
137 Self::ZBusNames(e)
138 }
139}
140
141impl From<zvariant::Error> for AtspiError {
142 fn from(e: zvariant::Error) -> Self {
143 Self::Zvariant(e)
144 }
145}
146
147impl From<std::io::Error> for AtspiError {
148 fn from(e: std::io::Error) -> Self {
149 Self::IO(e)
150 }
151}
152
153impl From<ObjectPathConversionError> for AtspiError {
154 fn from(e: ObjectPathConversionError) -> AtspiError {
155 Self::PathConversionError(e)
156 }
157}
158
159#[allow(clippy::module_name_repetitions)]
160#[derive(Clone, Debug)]
161pub enum ObjectPathConversionError {
162 NoIdAvailable,
163 ParseError(<i64 as std::str::FromStr>::Err),
164}
165impl std::fmt::Display for ObjectPathConversionError {
166 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
167 match self {
168 Self::NoIdAvailable => f.write_str(data:"No ID available in the path."),
169 Self::ParseError(e: &ParseIntError) => f.write_str(&format!("Failure to parse: {e}")),
170 }
171 }
172}
173impl std::error::Error for ObjectPathConversionError {}
174