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