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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
use proc_macro2::TokenStream as TokenStream2;
use proc_macro2::{Ident, Span};
use proc_macro_error::{abort, ResultExt};
use syn::{self, spanned::Spanned, Field, Lit, Meta, MetaNameValue, Visibility};
use self::GenMode::*;
use super::parse_attr;
pub struct GenParams {
pub mode: GenMode,
pub global_attr: Option<Meta>,
}
#[derive(PartialEq, Eq, Copy, Clone)]
pub enum GenMode {
Get,
GetCopy,
Set,
GetMut,
}
impl GenMode {
pub fn name(self) -> &'static str {
match self {
Get => "get",
GetCopy => "get_copy",
Set => "set",
GetMut => "get_mut",
}
}
pub fn prefix(self) -> &'static str {
match self {
Get | GetCopy | GetMut => "",
Set => "set_",
}
}
pub fn suffix(self) -> &'static str {
match self {
Get | GetCopy | Set => "",
GetMut => "_mut",
}
}
fn is_get(self) -> bool {
match self {
GenMode::Get | GenMode::GetCopy | GenMode::GetMut => true,
GenMode::Set => false,
}
}
}
pub fn parse_visibility(attr: Option<&Meta>, meta_name: &str) -> Option<Visibility> {
match attr {
Some(Meta::NameValue(MetaNameValue {
lit: Lit::Str(ref s),
path,
..
})) => {
if path.is_ident(meta_name) {
s.value().split(' ').find(|v| *v != "with_prefix").map(|v| {
syn::parse_str(v)
.map_err(|e| syn::Error::new(s.span(), e))
.expect_or_abort("invalid visibility found")
})
} else {
None
}
}
_ => None,
}
}
fn has_prefix_attr(f: &Field, mode: GenMode) -> bool {
let inner = f
.attrs
.iter()
.filter_map(|v| parse_attr(v, mode))
.filter(|meta| {
["get", "get_copy"]
.iter()
.any(|ident| meta.path().is_ident(ident))
})
.last();
match inner {
Some(Meta::NameValue(meta)) => {
if let Lit::Str(lit) = meta.lit {
lit.value().split(' ').any(|v| v == "with_prefix")
} else {
false
}
}
_ => false,
}
}
pub fn implement(field: &Field, params: &GenParams) -> TokenStream2 {
let field_name = field
.clone()
.ident
.unwrap_or_else(|| abort!(field.span(), "Expected the field to have a name"));
let fn_name = Ident::new(
&format!(
"{}{}{}{}",
if has_prefix_attr(field, params.mode) && (params.mode.is_get()) {
"get_"
} else {
""
},
params.mode.prefix(),
field_name,
params.mode.suffix()
),
Span::call_site(),
);
let ty = field.ty.clone();
let doc = field.attrs.iter().filter(|v| {
v.parse_meta()
.map(|meta| meta.path().is_ident("doc"))
.unwrap_or(false)
});
let attr = field
.attrs
.iter()
.filter_map(|v| parse_attr(v, params.mode))
.last()
.or_else(|| params.global_attr.clone());
let visibility = parse_visibility(attr.as_ref(), params.mode.name());
match attr {
Some(_) => match params.mode {
GenMode::Get => {
quote! {
#(#doc)*
#[inline(always)]
#visibility fn #fn_name(&self) -> &#ty {
&self.#field_name
}
}
}
GenMode::GetCopy => {
quote! {
#(#doc)*
#[inline(always)]
#visibility fn #fn_name(&self) -> #ty {
self.#field_name
}
}
}
GenMode::Set => {
quote! {
#(#doc)*
#[inline(always)]
#visibility fn #fn_name(&mut self, val: #ty) -> &mut Self {
self.#field_name = val;
self
}
}
}
GenMode::GetMut => {
quote! {
#(#doc)*
#[inline(always)]
#visibility fn #fn_name(&mut self) -> &mut #ty {
&mut self.#field_name
}
}
}
},
None => quote! {},
}
}