-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathfields.rs
More file actions
295 lines (256 loc) · 8.42 KB
/
fields.rs
File metadata and controls
295 lines (256 loc) · 8.42 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
use proc_macro2::{Span, TokenStream};
use quote::ToTokens;
use syn::{
Data, DeriveInput, Error, Expr, Field as SynField, Fields as SynFields, Ident, Index, Lit,
LitBool, LitStr, Result, spanned::Spanned,
};
use crate::utils::get_attributes;
pub struct Fields {
fields: Vec<Field>,
}
impl Fields {
pub fn new(input: &DeriveInput) -> Result<Self> {
match input.data {
Data::Struct(ref data_struct) => Self::from_fields(&data_struct.fields),
_ => Err(Error::new_spanned(
input,
"`cli_table` derive macros can only be used on structs",
)),
}
}
pub fn into_iter(self) -> impl Iterator<Item = Field> {
self.fields.into_iter()
}
fn from_fields(syn_fields: &SynFields) -> Result<Self> {
let mut fields = Vec::new();
for (index, syn_field) in syn_fields.into_iter().enumerate() {
let field = Field::new(syn_field, index)?;
if let Some(field) = field {
fields.push(field);
}
}
fields.sort_by(|left, right| left.order.cmp(&right.order));
Ok(Fields { fields })
}
}
pub struct Field {
pub ident: TokenStream,
pub title: LitStr,
pub justify: Option<Expr>,
pub align: Option<Expr>,
pub color: Option<Expr>,
pub bold: Option<LitBool>,
pub order: usize,
pub display_fn: Option<Ident>,
pub customize_fn: Option<Ident>,
pub span: Span,
}
impl Field {
pub fn new(field: &SynField, index: usize) -> Result<Option<Self>> {
let ident = field
.ident
.as_ref()
.map(ToTokens::into_token_stream)
.unwrap_or_else(|| Index::from(index).into_token_stream());
let span = field.span();
let mut title = None;
let mut justify = None;
let mut align = None;
let mut color = None;
let mut bold = None;
let mut order = None;
let mut display_fn = None;
let mut customize_fn = None;
let mut skip = None;
let field_attributes = get_attributes(&field.attrs)?;
for (key, value) in field_attributes {
if key.is_ident("name") || key.is_ident("title") {
title = Some(match value {
Lit::Str(lit_str) => Ok(lit_str),
bad => Err(Error::new_spanned(
bad,
"Invalid value for #[table(title = \"field_name\")]",
)),
}?);
} else if key.is_ident("justify") {
justify = Some(match value {
Lit::Str(lit_str) => lit_str.parse::<Expr>(),
bad => Err(Error::new_spanned(
bad,
"Invalid value for #[table(justify = \"value\")]",
)),
}?);
} else if key.is_ident("align") {
align = Some(match value {
Lit::Str(lit_str) => lit_str.parse::<Expr>(),
bad => Err(Error::new_spanned(
bad,
"Invalid value for #[table(align = \"value\")]",
)),
}?);
} else if key.is_ident("color") {
color = Some(match value {
Lit::Str(lit_str) => lit_str.parse::<Expr>(),
bad => Err(Error::new_spanned(
bad,
"Invalid value for #[table(color = \"value\")]",
)),
}?);
} else if key.is_ident("bold") {
bold = Some(match value {
Lit::Bool(lit_bool) => Ok(lit_bool),
bad => Err(Error::new_spanned(bad, "Invalid value for #[table(bold)]")),
}?);
} else if key.is_ident("order") {
order = Some(match value {
Lit::Int(lit_int) => lit_int.base10_parse::<usize>(),
bad => Err(Error::new_spanned(
bad,
"Invalid value for #[table(order = <usize>)]",
)),
}?);
} else if key.is_ident("display_fn") {
display_fn = Some(match value {
Lit::Str(lit_str) => lit_str.parse::<Ident>(),
bad => Err(Error::new_spanned(
bad,
"Invalid value for #[table(display_fn = \"value\")]",
)),
}?);
} else if key.is_ident("customize_fn") {
customize_fn = Some(match value {
Lit::Str(lit_str) => lit_str.parse::<Ident>(),
bad => Err(Error::new_spanned(
bad,
"Invalid value for #[table(display_fn = \"value\")]",
)),
}?);
} else if key.is_ident("skip") {
skip = Some(match value {
Lit::Bool(lit_bool) => Ok(lit_bool),
bad => Err(Error::new_spanned(bad, "Invalid value for #[table(bold)]")),
}?);
}
}
if let Some(skip) = skip {
if skip.value {
return Ok(None);
}
}
let mut field_builder = Self::builder(ident, span);
if let Some(title) = title {
field_builder.title(title);
}
if let Some(justify) = justify {
field_builder.justify(justify);
}
if let Some(align) = align {
field_builder.align(align);
}
if let Some(color) = color {
field_builder.color(color);
}
if let Some(bold) = bold {
field_builder.bold(bold);
}
if let Some(order) = order {
field_builder.order(order);
}
if let Some(display_fn) = display_fn {
field_builder.display_fn(display_fn);
}
if let Some(customize_fn) = customize_fn {
field_builder.customize_fn(customize_fn);
}
Ok(Some(field_builder.build()))
}
fn builder(ident: TokenStream, span: Span) -> FieldBuilder {
FieldBuilder::new(ident, span)
}
}
struct FieldBuilder {
ident: TokenStream,
title: Option<LitStr>,
justify: Option<Expr>,
align: Option<Expr>,
color: Option<Expr>,
bold: Option<LitBool>,
order: Option<usize>,
display_fn: Option<Ident>,
customize_fn: Option<Ident>,
span: Span,
}
impl FieldBuilder {
fn new(ident: TokenStream, span: Span) -> Self {
Self {
ident,
title: None,
justify: None,
align: None,
color: None,
bold: None,
order: None,
display_fn: None,
customize_fn: None,
span,
}
}
fn title(&mut self, title: LitStr) -> &mut Self {
self.title = Some(title);
self
}
fn justify(&mut self, justify: Expr) -> &mut Self {
self.justify = Some(justify);
self
}
fn align(&mut self, align: Expr) -> &mut Self {
self.align = Some(align);
self
}
fn color(&mut self, color: Expr) -> &mut Self {
self.color = Some(color);
self
}
fn bold(&mut self, bold: LitBool) -> &mut Self {
self.bold = Some(bold);
self
}
fn order(&mut self, order: usize) -> &mut Self {
self.order = Some(order);
self
}
fn display_fn(&mut self, display_fn: Ident) -> &mut Self {
self.display_fn = Some(display_fn);
self
}
fn customize_fn(&mut self, customize_fn: Ident) -> &mut Self {
self.customize_fn = Some(customize_fn);
self
}
fn build(self) -> Field {
let ident = self.ident;
let justify = self.justify;
let align = self.align;
let color = self.color;
let bold = self.bold;
let order = self.order.unwrap_or(usize::MAX);
let display_fn = self.display_fn;
let customize_fn = self.customize_fn;
let span = self.span;
let title = self
.title
.unwrap_or_else(|| LitStr::new(&ident.to_string(), span));
Field {
ident,
title,
justify,
align,
color,
bold,
order,
display_fn,
customize_fn,
span,
}
}
}