1#[macro_export]
45macro_rules! forward_error {
46 ($ty:ty) => {
47 impl ::core::error::Error for $ty {
48 fn source(&self) -> Option<&(dyn ::core::error::Error + 'static)> {
49 Some(::core::ops::Deref::deref(&self.0))
50 }
51 }
52 };
53
54 ($ty:ty => $field:ident) => {
55 impl ::core::error::Error for $ty {
56 fn source(&self) -> Option<&(dyn ::core::error::Error + 'static)> {
57 Some(::core::ops::Deref::deref(&self.$field))
58 }
59 }
60 };
61}
62
63#[macro_export]
89macro_rules! impl_error_enum {
90 ($ty:ty: $($variant:ident ($($inner:ident),+) => $source:expr),+ ,) => {
91 impl ::core::error::Error for $ty {
92 fn source(&self) -> ::core::option::Option<&(dyn ::core::error::Error + 'static)> {
93 match self {
94 $(
95 Self::$variant($($inner),+) => ::core::option::Option::Some($source),
96 )*
97 _ => ::core::option::Option::None,
98 }
99 }
100 }
101 };
102
103 ($ty:ty: $($variant:ident ($($inner:ident),+) => $source:expr),+) => {
104 $crate::impl_error_enum!($ty: $($variant ($($inner),+) => $source),+ ,);
105 };
106
107 ($ty:ty: $($variant:ident { $($inner:ident),+ } => $source:expr),+ ,) => {
108 impl ::core::error::Error for $ty {
109 fn source(&self) -> ::core::option::Option<&(dyn ::core::error::Error + 'static)> {
110 match self {
111 $(
112 Self::$variant { $($inner),+ } => ::core::option::Option::Some($source),
113 )*
114 _ => ::core::option::Option::None,
115 }
116 }
117 }
118 };
119
120 ($ty:ty: $($variant:ident { $($inner:ident),+ } => $source:expr),+) => {
121 $crate::impl_error_enum!($ty: $($variant { $($inner),+ } => $source),+ ,);
122 };
123
124 ($ty:ty) => {
125 impl ::core::error::Error for $ty {}
126 };
127}
128
129#[macro_export]
147macro_rules! impl_leaf_error {
148 ($ty:ty) => {
149 impl ::core::error::Error for $ty {}
150 };
151}
152
153#[cfg(test)]
154#[rustversion::since(1.81)]
155mod tests {
156 use alloc::string::String;
157 use core::error::Error as _;
158
159 #[test]
160 fn with_trailing_comma() {
161 #![allow(unused)]
162
163 #[derive(Debug)]
164 enum Foo {
165 Bar,
166 }
167
168 impl_display_enum!(Foo: Bar => "bar");
169 impl_leaf_error!(Foo);
170 }
171
172 #[test]
173 fn no_inner_data() {
174 #[derive(Debug)]
175 enum Foo {
176 Bar,
177 Baz,
178 }
179
180 impl_display_enum!(Foo: Bar => "bar", Baz => "qux");
181 impl_error_enum!(Foo);
182
183 assert!(Foo::Bar.source().is_none());
184 assert!(Foo::Baz.source().is_none());
185 }
186
187 #[test]
188 fn named_variant() {
189 #[derive(Debug)]
190 enum Foo {
191 Bar { source: std::io::Error },
192 Baz,
193 }
194
195 impl_display!(Foo: "foo");
196 impl_error_enum!(Foo: Bar { source } => source);
197
198 let io_err = std::io::Error::new(std::io::ErrorKind::Other, "test");
199 assert!(Foo::Bar { source: io_err }.source().is_some());
200 assert!(Foo::Baz.source().is_none());
201 }
202
203 #[test]
204 fn named_variant_with_trailing_comma() {
205 #[derive(Debug)]
206 enum Foo {
207 Bar { source: std::io::Error },
208 Baz,
209 }
210
211 impl_display!(Foo: "foo");
212 impl_error_enum!(Foo: Bar { source } => source,);
213
214 let io_err = std::io::Error::new(std::io::ErrorKind::Other, "test");
215 assert!(Foo::Bar { source: io_err }.source().is_some());
216 assert!(Foo::Baz.source().is_none());
217 }
218
219 #[test]
220 fn uniform_enum() {
221 #[derive(Debug)]
222 enum Foo {
223 Bar(String),
224 Baz(std::io::Error),
225 Qux(String, std::io::Error),
226 }
227
228 impl_display_enum! {
229 Foo:
230 Bar(desc) => "{desc}",
231 Baz(err) => "{err}",
232 Qux(desc, err) => "{desc}: {err}"
233 };
234 impl_error_enum! {
235 Foo:
236 Baz(err) => err,
237 Qux(_desc, err) => err
238 };
239
240 assert!(Foo::Bar(String::new()).source().is_none());
241
242 let io_err = std::io::Error::new(std::io::ErrorKind::Other, "test");
243 assert!(Foo::Baz(io_err).source().is_some());
244
245 let io_err = std::io::Error::new(std::io::ErrorKind::Other, "test");
246 assert!(Foo::Qux(String::new(), io_err).source().is_some());
247 }
248}