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