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