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
#![warn(
clippy::all,
clippy::cargo,
clippy::nursery,
clippy::pedantic,
rust_2018_idioms
)]
#![forbid(unsafe_code)]
#![doc = include_str!("../README.md")]
use proc_macro::TokenStream;
use quote::quote;
use syn::fold::Fold;
use syn::{
parse_macro_input, parse_quote, token, Attribute, Data, DataStruct, DeriveInput, Fields,
FieldsNamed, VisPublic, Visibility,
};
struct PokeAPIFields;
impl Fold for PokeAPIFields {
fn fold_fields_named(&mut self, fields: FieldsNamed) -> FieldsNamed {
let serde_skip: Attribute = parse_quote!(#[serde(skip)]);
let brace_token = fields.brace_token;
let named = fields
.named
.into_iter()
.map(|mut field| {
let ident = field.ident.as_ref().unwrap();
if ident.to_string().starts_with('_') {
if !field.attrs.iter().any(|attr| attr == &serde_skip) {
field.attrs.push(serde_skip.clone());
}
}
if !field.attrs.iter().any(|attr| attr == &serde_skip) {
field.vis = Visibility::Public(VisPublic {
pub_token: token::Pub::default(),
});
}
field
})
.collect();
FieldsNamed { brace_token, named }
}
}
#[allow(clippy::doc_markdown)]
#[proc_macro_attribute]
pub fn pokeapi_struct(_attr: TokenStream, item: TokenStream) -> TokenStream {
let DeriveInput {
attrs,
ident,
generics,
data,
..
} = parse_macro_input!(item as DeriveInput);
let ident_lower = ident.to_string().to_ascii_lowercase();
let doc_comment = format!(
"[{url}{ident_lower}]({url}{ident_lower})",
url = "https://pokeapi.co/docs/v2#"
);
let doc_attr: Attribute = parse_quote!(#[doc = #doc_comment]);
let fields = match data {
Data::Struct(DataStruct {
fields: Fields::Named(named_fields),
..
}) => PokeAPIFields
.fold_fields_named(named_fields)
.named
.into_iter(),
_ => panic!("This attribute requires a struct with named fields."),
};
TokenStream::from(quote! {
#doc_attr
#(#attrs)*
#[derive(Debug, Clone, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
#[non_exhaustive]
pub struct #ident #generics {
#(#fields),*
}
})
}