fieldmask_derive 0.2.0

derive macros for the fieldmask crate
Documentation
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
#![allow(dead_code)]

use proc_macro2::TokenStream;
use quote::ToTokens;
use syn::{
    Attribute, Expr, Generics, Ident, Meta, Path, Token, Type, Visibility, braced, parenthesized,
    parse::{Parse, ParseStream},
    punctuated::Punctuated,
    token::{Brace, Paren},
};

struct Wrap<T>(pub T);

impl<T: Parse> Parse for Wrap<Punctuated<T, Token![,]>> {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        Ok(Self(input.parse_terminated(T::parse, Token![,])?))
    }
}

/// Represents the input.
pub enum Input {
    UnitEnum(ItemUnitEnum),
    TupleEnum(ItemTupleEnum),
    Struct(ItemStruct),
}

impl Input {
    pub fn get_message_info(&self) -> MessageInfo {
        match &self {
            Input::UnitEnum(input) => input.get_info(),
            Input::TupleEnum(input) => input.get_info(),
            Input::Struct(input) => input.get_info(),
        }
    }
}

impl Parse for Input {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let attrs = input.call(Attribute::parse_outer)?;

        let vis = input.parse()?;

        let lookahead = input.lookahead1();
        if lookahead.peek(Token![struct]) {
            assert_no_attr(&attrs)?;

            let struct_token = input.parse()?;
            let ident = input.parse()?;
            let generics = {
                let mut generics: Generics = input.parse()?;
                generics.where_clause = input.parse()?;
                generics
            };

            let content;
            let brace_token = braced!(content in input);
            let fields = content.parse_terminated(NamedField::parse, Token![,])?;

            return Ok(Self::Struct(ItemStruct {
                attrs,
                vis,
                struct_token,
                ident,
                generics,
                brace_token,
                fields,
            }));
        }
        if lookahead.peek(Token![enum]) {
            let enum_token = input.parse()?;
            let ident = input.parse()?;
            let generics = {
                let mut generics: Generics = input.parse()?;
                generics.where_clause = input.parse()?;
                generics
            };

            let content;
            let brace_token = braced!(content in input);
            let first_variant: EnumVariant = content.parse()?;
            let _: Option<Token![,]> = content.parse()?;
            match first_variant {
                EnumVariant::Unit(first_variant) => {
                    let attr_iter = attrs
                        .iter()
                        .filter(|attr| attr.path().is_ident("fieldmask"))
                        .map(|attr| attr.parse_args())
                        .collect::<syn::Result<Vec<_>>>()?
                        .into_iter()
                        .flat_map(|attrs: Wrap<Punctuated<UnitEnumAttribute, Token![,]>>| attrs.0);

                    let mut variants =
                        content.parse_terminated(UnitEnumVariant::parse, Token![,])?;
                    variants.insert(0, first_variant);

                    let mut normalize_some_default = false;
                    for attr in attr_iter {
                        match attr {
                            UnitEnumAttribute::NormalizeSomeDefault { .. } => {
                                if normalize_some_default {
                                    return Err(syn::Error::new_spanned(
                                        attr,
                                        "duplicated normalize_some_default attribute",
                                    ));
                                }
                                normalize_some_default = true;
                            }
                        }
                    }

                    return Ok(Self::UnitEnum(ItemUnitEnum {
                        attrs,
                        normalize_some_default,
                        vis,
                        enum_token,
                        ident,
                        generics,
                        brace_token,
                        variants,
                    }));
                }
                EnumVariant::Tuple(first_variant) => {
                    assert_no_attr(&attrs)?;

                    let mut variants =
                        content.parse_terminated(TupleEnumVariant::parse, Token![,])?;
                    variants.insert(0, first_variant);
                    return Ok(Self::TupleEnum(ItemTupleEnum {
                        attrs,
                        vis,
                        enum_token,
                        ident,
                        generics,
                        brace_token,
                        variants,
                    }));
                }
            }
        }

        Err(lookahead.error())
    }
}

/// Represents the declaration of a unit enum.
pub struct ItemUnitEnum {
    pub attrs: Vec<Attribute>,
    pub normalize_some_default: bool,
    pub vis: Visibility,
    pub enum_token: Token![enum],
    pub ident: Ident,
    pub generics: Generics,
    pub brace_token: Brace,
    pub variants: Punctuated<UnitEnumVariant, Token![,]>,
}

impl ItemUnitEnum {
    pub fn get_info(&self) -> MessageInfo {
        let ident = &self.ident;
        let generics = &self.generics;

        MessageInfo {
            ident,
            generics,
            fields: vec![],
        }
    }
}

/// Represents an attribute for a unit enum.
#[derive(PartialEq)]
enum UnitEnumAttribute {
    NormalizeSomeDefault { repr: Path },
}

impl Parse for UnitEnumAttribute {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let meta: Meta = input.parse()?;
        match meta {
            Meta::Path(p) if p.is_ident("normalize_some_default") => {
                Ok(Self::NormalizeSomeDefault { repr: p })
            }
            _ => Err(syn::Error::new_spanned(meta, "invalid meta")),
        }
    }
}

impl ToTokens for UnitEnumAttribute {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        match self {
            Self::NormalizeSomeDefault { repr } => repr.to_tokens(tokens),
        }
    }
}

/// Represents the declaration of a tuple enum.
pub struct ItemTupleEnum {
    pub attrs: Vec<Attribute>,
    pub vis: Visibility,
    pub enum_token: Token![enum],
    pub ident: Ident,
    pub generics: Generics,
    pub brace_token: Brace,
    pub variants: Punctuated<TupleEnumVariant, Token![,]>,
}

impl ItemTupleEnum {
    pub fn get_info(&self) -> MessageInfo {
        let ident = &self.ident;
        let generics = &self.generics;

        MessageInfo {
            ident,
            generics,
            fields: self
                .variants
                .iter()
                .map(|v| MessageField {
                    ident: &v.ident,
                    ty: &v.ty,
                    is_flatten: false,
                })
                .collect::<Vec<_>>(),
        }
    }
}

/// Represents the declaration of a struct.
pub struct ItemStruct {
    pub attrs: Vec<Attribute>,
    pub vis: Visibility,
    pub struct_token: Token![struct],
    pub ident: Ident,
    pub generics: Generics,
    pub brace_token: Brace,
    pub fields: Punctuated<NamedField, Token![,]>,
}

impl ItemStruct {
    pub fn get_info(&self) -> MessageInfo {
        let ident = &self.ident;
        let generics = &self.generics;
        let fields = self
            .fields
            .iter()
            .map(|f| MessageField {
                ident: &f.ident,
                ty: &f.ty,
                is_flatten: f.is_flatten,
            })
            .collect::<Vec<_>>();
        MessageInfo {
            ident,
            generics,
            fields,
        }
    }
}

/// Represents the declaration of a variant in an enum.
pub enum EnumVariant {
    Unit(UnitEnumVariant),
    Tuple(TupleEnumVariant),
}

impl Parse for EnumVariant {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let attrs = input.call(Attribute::parse_outer)?;
        let ident: Ident = input.parse()?;

        if input.peek(Paren) {
            Ok(Self::Tuple(TupleEnumVariant::parse_content(
                attrs, ident, input,
            )?))
        } else {
            Ok(Self::Unit(UnitEnumVariant::parse_content(
                attrs, ident, input,
            )?))
        }
    }
}

/// Represents the declaration of a variant in a unit enum.
pub struct UnitEnumVariant {
    pub attrs: Vec<Attribute>,
    pub ident: Ident,
    pub discriminant: Option<(Token![=], Expr)>,
}

impl UnitEnumVariant {
    fn parse_content(attrs: Vec<Attribute>, ident: Ident, input: ParseStream) -> syn::Result<Self> {
        assert_no_attr(&attrs)?;

        let discriminant = if input.peek(Token![=]) {
            let eq_token = input.parse()?;
            let discriminant = input.parse()?;
            Some((eq_token, discriminant))
        } else {
            None
        };

        Ok(UnitEnumVariant {
            attrs,
            ident,
            discriminant,
        })
    }
}

impl Parse for UnitEnumVariant {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let attrs = input.call(Attribute::parse_outer)?;
        let ident: Ident = input.parse()?;
        Self::parse_content(attrs, ident, input)
    }
}

/// Represents the declaration of a variant in a tuple enum.
pub struct TupleEnumVariant {
    pub attrs: Vec<Attribute>,
    pub ident: Ident,
    pub paren_token: Paren,
    pub tuple_attrs: Vec<Attribute>,
    pub ty: Type,
}

impl TupleEnumVariant {
    fn parse_content(attrs: Vec<Attribute>, ident: Ident, input: ParseStream) -> syn::Result<Self> {
        assert_no_attr(&attrs)?;

        let content;
        let paren_token = parenthesized!(content in input);
        let tuple_attrs = content.call(Attribute::parse_outer)?;
        let ty = content.parse()?;

        if !content.is_empty() {
            let _punt: Token![,] = content.parse()?;
            if !content.is_empty() {
                return Err(content.error("there can be at most one item in the tuple variant"));
            }
        }

        Ok(TupleEnumVariant {
            attrs,
            ident,
            paren_token,
            tuple_attrs,
            ty,
        })
    }
}

impl Parse for TupleEnumVariant {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let attrs = input.call(Attribute::parse_outer)?;
        let ident: Ident = input.parse()?;
        Self::parse_content(attrs, ident, input)
    }
}

/// Represents the declaration of a named field in a struct.
pub struct NamedField {
    pub attrs: Vec<Attribute>,
    pub vis: Visibility,
    pub ident: Ident,
    pub colon_token: Token![:],
    pub ty: Type,
    pub is_flatten: bool,
}

impl Parse for NamedField {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let attrs = input.call(Attribute::parse_outer)?;

        #[allow(unused_assignments)]
        let mut is_flatten = false;

        #[cfg(feature = "prost")]
        {
            is_flatten = attrs
                .iter()
                .filter(|attr| attr.path().is_ident("prost"))
                .map(|attr| attr.parse_args())
                .collect::<syn::Result<Vec<_>>>()?
                .iter()
                .flat_map(|attrs: &Wrap<Punctuated<ProstFieldAttribute, Token![,]>>| &attrs.0)
                .any(|meta| matches!(meta, ProstFieldAttribute::OneOf));
        }

        let attr_iter = attrs
            .iter()
            .filter(|attr| attr.path().is_ident("fieldmask"))
            .map(|attr| attr.parse_args())
            .collect::<syn::Result<Vec<_>>>()?
            .into_iter()
            .flat_map(|attrs: Wrap<Punctuated<NamedFieldAttribute, Token![,]>>| attrs.0);

        for attr in attr_iter {
            match attr {
                NamedFieldAttribute::Flatten { .. } => {
                    if is_flatten {
                        return Err(syn::Error::new_spanned(
                            attr,
                            "duplicated flatten attribute",
                        ));
                    }
                    is_flatten = true;
                }
            }
        }

        Ok(NamedField {
            attrs,
            vis: input.parse()?,
            ident: input.parse()?,
            colon_token: input.parse()?,
            ty: input.parse()?,
            is_flatten,
        })
    }
}

/// Represents an attribute for a named field in a struct.
#[derive(PartialEq)]
enum NamedFieldAttribute {
    Flatten { repr: Path },
}

impl Parse for NamedFieldAttribute {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let meta: Meta = input.parse()?;
        match meta {
            Meta::Path(p) if p.is_ident("flatten") => Ok(Self::Flatten { repr: p }),
            _ => Err(syn::Error::new_spanned(meta, "invalid meta")),
        }
    }
}

impl ToTokens for NamedFieldAttribute {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        match self {
            Self::Flatten { repr } => repr.to_tokens(tokens),
        }
    }
}

/// Represents a prost attribute for a named field in a struct.
#[derive(PartialEq)]
#[non_exhaustive]
#[cfg(feature = "prost")]
enum ProstFieldAttribute {
    OneOf,
    Other,
}

#[cfg(feature = "prost")]
impl Parse for ProstFieldAttribute {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let meta: Meta = input.parse()?;
        match meta {
            Meta::NameValue(m) if m.path.is_ident("oneof") => Ok(Self::OneOf),
            _ => Ok(Self::Other),
        }
    }
}

/// The metadata of a field in a message.
pub struct MessageField<'a> {
    pub ident: &'a Ident,
    pub ty: &'a Type,
    pub is_flatten: bool,
}

/// The metadata of a message.
pub struct MessageInfo<'a> {
    pub ident: &'a Ident,
    pub generics: &'a Generics,
    /// The fields of the message.
    /// Note that unit enum is considered a single value so it does not have any field.
    pub fields: Vec<MessageField<'a>>,
}

fn assert_no_attr(attrs: &[Attribute]) -> syn::Result<()> {
    let _attr_iter = attrs
        .iter()
        .filter(|attr| attr.path().is_ident("fieldmask"))
        .map(|attr| attr.parse_args())
        .collect::<syn::Result<Vec<_>>>()?
        .into_iter()
        .flat_map(|attrs: Wrap<Punctuated<NoAttribute, Token![,]>>| attrs.0);
    Ok(())
}

/// Represents an attribute for an item that should not have any attribute.
#[derive(PartialEq)]
enum NoAttribute {}

impl Parse for NoAttribute {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let meta: Meta = input.parse()?;
        Err(syn::Error::new_spanned(meta, "invalid meta"))
    }
}

impl ToTokens for NoAttribute {
    fn to_tokens(&self, _tokens: &mut TokenStream) {}
}