David Tolnay | 3c3c7d1 | 2019-05-08 14:54:12 -0700 | [diff] [blame] | 1 | use crate::error::Result; |
| 2 | use crate::file; |
| 3 | use proc_macro2::{Ident, Span, TokenStream}; |
| 4 | use quote::quote; |
| 5 | use syn::Index; |
| 6 | use syn_codegen::{Data, Definitions, Node, Type}; |
| 7 | |
| 8 | const DEBUG_SRC: &str = "../tests/debug/gen.rs"; |
| 9 | |
| 10 | fn rust_type(ty: &Type) -> TokenStream { |
| 11 | match ty { |
| 12 | Type::Syn(ty) => { |
| 13 | let ident = Ident::new(ty, Span::call_site()); |
| 14 | quote!(syn::#ident) |
| 15 | } |
| 16 | Type::Std(ty) => { |
| 17 | let ident = Ident::new(ty, Span::call_site()); |
| 18 | quote!(#ident) |
| 19 | } |
| 20 | Type::Ext(ty) => { |
| 21 | let ident = Ident::new(ty, Span::call_site()); |
| 22 | quote!(proc_macro2::#ident) |
| 23 | } |
| 24 | Type::Token(ty) | Type::Group(ty) => { |
| 25 | let ident = Ident::new(ty, Span::call_site()); |
| 26 | quote!(syn::token::#ident) |
| 27 | } |
| 28 | Type::Punctuated(ty) => { |
| 29 | let element = rust_type(&ty.element); |
| 30 | let punct = Ident::new(&ty.punct, Span::call_site()); |
| 31 | quote!(syn::punctuated::Punctuated<#element, #punct>) |
| 32 | } |
| 33 | Type::Option(ty) => { |
| 34 | let inner = rust_type(ty); |
| 35 | quote!(Option<#inner>) |
| 36 | } |
| 37 | Type::Box(ty) => { |
| 38 | let inner = rust_type(ty); |
| 39 | quote!(Box<#inner>) |
| 40 | } |
| 41 | Type::Vec(ty) => { |
| 42 | let inner = rust_type(ty); |
| 43 | quote!(Vec<#inner>) |
| 44 | } |
| 45 | Type::Tuple(ty) => { |
| 46 | let inner = ty.iter().map(rust_type); |
| 47 | quote!((#(#inner,)*)) |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | fn is_printable(ty: &Type) -> bool { |
| 53 | match ty { |
| 54 | Type::Ext(name) => name != "Span", |
| 55 | Type::Box(ty) => is_printable(ty), |
| 56 | Type::Tuple(ty) => ty.iter().any(is_printable), |
| 57 | Type::Token(_) | Type::Group(_) => false, |
| 58 | Type::Syn(_) | Type::Std(_) | Type::Punctuated(_) | Type::Option(_) | Type::Vec(_) => true, |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | fn format_field(val: &TokenStream, ty: &Type) -> Option<TokenStream> { |
| 63 | if !is_printable(ty) { |
| 64 | return None; |
| 65 | } |
| 66 | let format = match ty { |
| 67 | Type::Option(ty) => { |
| 68 | let inner = quote!(_val); |
| 69 | let format = format_field(&inner, ty).map(|format| { |
| 70 | quote! { |
| 71 | formatter.write_str("(")?; |
| 72 | Debug::fmt(#format, formatter)?; |
| 73 | formatter.write_str(")")?; |
| 74 | } |
| 75 | }); |
| 76 | let ty = rust_type(ty); |
| 77 | quote!({ |
| 78 | #[derive(RefCast)] |
| 79 | #[repr(transparent)] |
| 80 | struct Print(Option<#ty>); |
| 81 | impl Debug for Print { |
| 82 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 83 | match &self.0 { |
| 84 | Some(#inner) => { |
| 85 | formatter.write_str("Some")?; |
| 86 | #format |
| 87 | Ok(()) |
| 88 | } |
| 89 | None => formatter.write_str("None"), |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | Print::ref_cast(#val) |
| 94 | }) |
| 95 | } |
| 96 | Type::Tuple(ty) => { |
| 97 | let printable: Vec<TokenStream> = ty |
| 98 | .iter() |
| 99 | .enumerate() |
| 100 | .filter_map(|(i, ty)| { |
| 101 | let index = Index::from(i); |
| 102 | let val = quote!(&#val.#index); |
| 103 | format_field(&val, ty) |
| 104 | }) |
| 105 | .collect(); |
| 106 | if printable.len() == 1 { |
| 107 | printable.into_iter().next().unwrap() |
| 108 | } else { |
| 109 | quote! { |
| 110 | &(#(#printable),*) |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | _ => quote! { Lite(#val) }, |
| 115 | }; |
| 116 | Some(format) |
| 117 | } |
| 118 | |
| 119 | fn expand_impl(node: &Node) -> TokenStream { |
| 120 | let name = &node.ident; |
| 121 | let ident = Ident::new(&node.ident, Span::call_site()); |
| 122 | |
| 123 | let body = match &node.data { |
| 124 | Data::Enum(variants) => { |
| 125 | let arms = variants.iter().map(|(v, fields)| { |
| 126 | let variant = Ident::new(v, Span::call_site()); |
| 127 | if fields.is_empty() { |
| 128 | quote! { |
| 129 | syn::#ident::#variant => formatter.write_str(#v), |
| 130 | } |
| 131 | } else { |
| 132 | let pats = (0..fields.len()) |
| 133 | .map(|i| Ident::new(&format!("_v{}", i), Span::call_site())); |
| 134 | let fields = fields.iter().enumerate().filter_map(|(i, ty)| { |
| 135 | let index = Ident::new(&format!("_v{}", i), Span::call_site()); |
| 136 | let val = quote!(#index); |
| 137 | let format = format_field(&val, ty)?; |
| 138 | Some(quote! { |
| 139 | formatter.field(#format); |
| 140 | }) |
| 141 | }); |
| 142 | quote! { |
| 143 | syn::#ident::#variant(#(#pats),*) => { |
| 144 | let mut formatter = formatter.debug_tuple(#v); |
| 145 | #(#fields)* |
| 146 | formatter.finish() |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | }); |
| 151 | quote! { |
| 152 | match &self.value { |
| 153 | #(#arms)* |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | Data::Struct(fields) => { |
| 158 | let fields = fields.iter().filter_map(|(f, ty)| { |
| 159 | let ident = Ident::new(f, Span::call_site()); |
David Tolnay | 644246b | 2019-05-08 23:02:46 -0700 | [diff] [blame^] | 160 | if let Type::Option(ty) = ty { |
| 161 | let inner = quote!(_val); |
| 162 | let format = format_field(&inner, ty).map(|format| { |
| 163 | quote! { |
| 164 | let #inner = &self.0; |
| 165 | formatter.write_str("(")?; |
| 166 | Debug::fmt(#format, formatter)?; |
| 167 | formatter.write_str(")")?; |
| 168 | } |
| 169 | }); |
| 170 | let ty = rust_type(ty); |
| 171 | Some(quote! { |
| 172 | if let Some(val) = &self.value.#ident { |
| 173 | #[derive(RefCast)] |
| 174 | #[repr(transparent)] |
| 175 | struct Print(#ty); |
| 176 | impl Debug for Print { |
| 177 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 178 | formatter.write_str("Some")?; |
| 179 | #format |
| 180 | Ok(()) |
| 181 | } |
| 182 | } |
| 183 | formatter.field(#f, Print::ref_cast(val)); |
| 184 | } |
| 185 | }) |
| 186 | } else { |
| 187 | let val = quote!(&self.value.#ident); |
| 188 | let format = format_field(&val, ty)?; |
| 189 | Some(quote! { |
| 190 | formatter.field(#f, #format); |
| 191 | }) |
| 192 | } |
David Tolnay | 3c3c7d1 | 2019-05-08 14:54:12 -0700 | [diff] [blame] | 193 | }); |
| 194 | quote! { |
| 195 | let mut formatter = formatter.debug_struct(#name); |
| 196 | #(#fields)* |
| 197 | formatter.finish() |
| 198 | } |
| 199 | } |
| 200 | Data::Private => { |
| 201 | quote! { |
| 202 | write!(formatter, "{:?}", self.value()) |
| 203 | } |
| 204 | } |
| 205 | }; |
| 206 | |
| 207 | quote! { |
| 208 | impl Debug for Lite<syn::#ident> { |
| 209 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 210 | #body |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | pub fn generate(defs: &Definitions) -> Result<()> { |
| 217 | let mut impls = TokenStream::new(); |
| 218 | for node in &defs.types { |
| 219 | impls.extend(expand_impl(node)); |
| 220 | } |
| 221 | |
| 222 | file::write( |
| 223 | DEBUG_SRC, |
| 224 | quote! { |
| 225 | use super::{Lite, RefCast}; |
| 226 | use std::fmt::{self, Debug}; |
| 227 | |
| 228 | #impls |
| 229 | }, |
| 230 | )?; |
| 231 | |
| 232 | Ok(()) |
| 233 | } |