assert_type_match/
variants.rs

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
use crate::flags::{flag_to_bool, merge_flags, Flag, ParseFlag};
use syn::parse::{Parse, ParseStream};
use syn::Token;

#[derive(Default)]
pub(crate) struct VariantArgs {
    skip: Flag,
}

impl VariantArgs {
    /// Controls whether the annotated variant should be skipped.
    pub fn skip(&self) -> bool {
        flag_to_bool(&self.skip)
    }

    /// Merges two sets of [variant arguments].
    ///
    /// [variant arguments]: Self
    pub fn merge(self, other: Self) -> syn::Result<Self> {
        let skip = merge_flags::<kw::skip>(self.skip, other.skip)?;

        Ok(Self { skip })
    }
}

impl Parse for VariantArgs {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let mut this = Self { skip: None };

        loop {
            let lookahead = input.lookahead1();
            if lookahead.peek(kw::skip) {
                this.skip = input.parse_flag::<kw::skip>()?;
            } else {
                return Err(lookahead.error());
            }

            if input.peek(Token![,]) {
                input.parse::<Token![,]>()?;

                if input.is_empty() {
                    break;
                }
            } else {
                break;
            }
        }

        Ok(this)
    }
}

mod kw {
    syn::custom_keyword!(skip);
}