1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
use super::*; pub(crate) struct DisplayImpl<'a> { ty: &'a ErrorType, } impl<'a> ToTokens for DisplayImpl<'a> { fn to_tokens(&self, tokens: &mut TokenStream) { let body = { let mut tokens = TokenStream::new(); match &self.ty.data { ErrorData::Struct(struct_data) => struct_data.description.to_tokens(&mut tokens), ErrorData::Enum(variants) => { let ty = &self.ty.ident; let cases = variants.iter().map(|v| { let ident = &v.ident; let destruct = v.destruct(); let desc = &v.description; quote! { #ty::#ident #destruct => #desc } }); tokens.extend(quote! { match self { #(#cases,)* } }); } } tokens }; let ty = &self.ty.ident; tokens.extend(quote! { #[automatically_derived] #[allow(unused_qualifications)] impl ::std::fmt::Display for #ty { fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { #body } } #[automatically_derived] #[allow(unused_qualifications)] impl ::std::fmt::Debug for #ty { fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { #body } } }) } } impl<'a> DisplayImpl<'a> { pub fn for_type(ty: &'a ErrorType) -> Self { Self { ty } } }