| 1 | //! [`Debug`](trait@std::fmt::Debug) implementation. |
| 2 | |
| 3 | use proc_macro2::TokenStream; |
| 4 | use quote::quote; |
| 5 | |
| 6 | use crate::{Data, DeriveTrait, Item, SimpleType, SplitGenerics, TraitImpl}; |
| 7 | |
| 8 | /// Dummy-struct implement [`Trait`](crate::Trait) for |
| 9 | /// [`Debug`](trait@std::fmt::Debug). |
| 10 | pub struct Debug; |
| 11 | |
| 12 | impl TraitImpl for Debug { |
| 13 | fn as_str(&self) -> &'static str { |
| 14 | "Debug" |
| 15 | } |
| 16 | |
| 17 | fn default_derive_trait(&self) -> DeriveTrait { |
| 18 | DeriveTrait::Debug |
| 19 | } |
| 20 | |
| 21 | fn build_signature( |
| 22 | &self, |
| 23 | _any_bound: bool, |
| 24 | _item: &Item, |
| 25 | _generics: &SplitGenerics<'_>, |
| 26 | _traits: &[DeriveTrait], |
| 27 | _trait_: &DeriveTrait, |
| 28 | body: &TokenStream, |
| 29 | ) -> TokenStream { |
| 30 | quote! { |
| 31 | fn fmt(&self, __f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result { |
| 32 | match self { |
| 33 | #body |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | fn build_body( |
| 40 | &self, |
| 41 | _any_bound: bool, |
| 42 | _traits: &[DeriveTrait], |
| 43 | trait_: &DeriveTrait, |
| 44 | data: &Data, |
| 45 | ) -> TokenStream { |
| 46 | let self_pattern = &data.self_pattern(); |
| 47 | let debug_name = data.ident.to_string(); |
| 48 | |
| 49 | match data.simple_type() { |
| 50 | SimpleType::Struct(_) => { |
| 51 | let self_ident = data.iter_self_ident(**trait_); |
| 52 | let debug_fields = data |
| 53 | .iter_field_ident(**trait_) |
| 54 | .map(|field| field.to_string()); |
| 55 | |
| 56 | let finish = if data.any_skip_trait(**trait_) { |
| 57 | quote! { finish_non_exhaustive } |
| 58 | } else { |
| 59 | quote! { finish } |
| 60 | }; |
| 61 | |
| 62 | quote! { |
| 63 | #self_pattern => { |
| 64 | let mut __builder = ::core::fmt::Formatter::debug_struct(__f, #debug_name); |
| 65 | #(::core::fmt::DebugStruct::field(&mut __builder, #debug_fields, #self_ident);)* |
| 66 | ::core::fmt::DebugStruct::#finish(&mut __builder) |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | SimpleType::Tuple(_) => { |
| 71 | let self_ident = data.iter_self_ident(**trait_); |
| 72 | |
| 73 | quote! { |
| 74 | #self_pattern => { |
| 75 | let mut __builder = ::core::fmt::Formatter::debug_tuple(__f, #debug_name); |
| 76 | #(::core::fmt::DebugTuple::field(&mut __builder, #self_ident);)* |
| 77 | ::core::fmt::DebugTuple::finish(&mut __builder) |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | SimpleType::Unit(_) => { |
| 82 | quote! { #self_pattern => ::core::fmt::Formatter::write_str(__f, #debug_name), } |
| 83 | } |
| 84 | SimpleType::Union(_) => unreachable!("unexpected trait for union" ), |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |