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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
use proc_macro2::TokenStream;
use quote::{format_ident, quote, quote_spanned};
use syn::spanned::Spanned;
use syn::{Data, Fields, Ident};
use synstructure::Structure;
fn print_fields(name: &Ident, fields: &Fields) -> (TokenStream, TokenStream) {
let string_name = name.to_string();
let mut disps = vec![quote! {let mut __printed_anything = false;}];
match fields {
Fields::Named(fields_named) => {
let mut field_names = Vec::new();
for field in &fields_named.named {
let name = field.ident.as_ref().unwrap();
let string_name = name.to_string();
disps.push(quote! {
if #name.should_render() {
if __printed_anything {
__p.word_space(",");
}
__p.word(#string_name);
__p.word(":");
__p.nbsp();
__printed_anything = true;
}
#name.print_attribute(__p);
});
field_names.push(name);
}
(
quote! { {#(#field_names),*} },
quote! {
__p.word(#string_name);
if true #(&& !#field_names.should_render())* {
return;
}
__p.nbsp();
__p.word("{");
#(#disps)*
__p.word("}");
},
)
}
Fields::Unnamed(fields_unnamed) => {
let mut field_names = Vec::new();
for idx in 0..fields_unnamed.unnamed.len() {
let name = format_ident!("f{idx}");
disps.push(quote! {
if #name.should_render() {
if __printed_anything {
__p.word_space(",");
}
__printed_anything = true;
}
#name.print_attribute(__p);
});
field_names.push(name);
}
(
quote! { (#(#field_names),*) },
quote! {
__p.word(#string_name);
if true #(&& !#field_names.should_render())* {
return;
}
__p.popen();
#(#disps)*
__p.pclose();
},
)
}
Fields::Unit => (quote! {}, quote! { __p.word(#string_name) }),
}
}
pub(crate) fn print_attribute(input: Structure<'_>) -> TokenStream {
let span_error = |span, message: &str| {
quote_spanned! { span => const _: () = ::core::compile_error!(#message); }
};
// Must be applied to an enum type.
let code = match &input.ast().data {
Data::Enum(e) => {
let arms = e
.variants
.iter()
.map(|x| {
let ident = &x.ident;
let (pat, code) = print_fields(ident, &x.fields);
quote! {
Self::#ident #pat => {#code}
}
})
.collect::<Vec<_>>();
quote! {
match self {
#(#arms)*
}
}
}
Data::Struct(s) => {
let (pat, code) = print_fields(&input.ast().ident, &s.fields);
quote! {
let Self #pat = self;
#code
}
}
Data::Union(u) => {
return span_error(u.union_token.span(), "can't derive PrintAttribute on unions");
}
};
#[allow(keyword_idents_2024)]
input.gen_impl(quote! {
#[allow(unused)]
gen impl PrintAttribute for @Self {
fn should_render(&self) -> bool { true }
fn print_attribute(&self, __p: &mut rustc_ast_pretty::pp::Printer) { #code }
}
})
}
|