| 1 | // SPDX-License-Identifier: Apache-2.0 OR MIT |
| 2 | |
| 3 | use crate::derive::prelude::*; |
| 4 | |
| 5 | pub(crate) const NAME: &[&str] = &["Error" ]; |
| 6 | |
| 7 | pub(crate) fn derive(_cx: &Context, data: &Data) -> Result<TokenStream> { |
| 8 | let ident: &Ident = &data.ident; |
| 9 | let source: impl Iterator = |
| 10 | data.variant_idents().map(|v: &Ident| quote!(#ident::#v(x) => ::std::option::Option::Some(x))); |
| 11 | |
| 12 | let source: ImplItem = parse_quote! { |
| 13 | fn source(&self) -> ::std::option::Option<&(dyn (::std::error::Error) + 'static)> { |
| 14 | match self { #(#source,)* } |
| 15 | } |
| 16 | }; |
| 17 | |
| 18 | let mut impl_: EnumImpl<'_> = |
| 19 | EnumImpl::from_trait(data, &parse_quote!(::std::error::Error), supertraits_types:None, trait_def:parse_quote! { |
| 20 | trait Error { |
| 21 | #[allow(deprecated)] |
| 22 | fn description(&self) -> &str; |
| 23 | } |
| 24 | }); |
| 25 | |
| 26 | data.field_types().for_each(|f: &Type| impl_.push_where_predicate(parse_quote!(#f: 'static))); |
| 27 | impl_.push_item(source); |
| 28 | |
| 29 | Ok(impl_.build()) |
| 30 | } |
| 31 | |