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