atspi_common/
error.rs

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