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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use proc_macro2::Span;
use syn::parse::{Parse, ParseStream, Result};
use syn::punctuated::Punctuated;
use syn::{braced, token, Attribute, Ident, Path, Token, Type};

#[allow(non_camel_case_types)]
mod kw {
    syn::custom_keyword!(accessor_type);
    syn::custom_keyword!(gen_accessors);
    syn::custom_keyword!(gen_types);
}

pub struct MacroInput {
    pub type_def: RootTypeDef,
    pub gen_accessors: Ident,
    pub accessor_type: Path,
}

enum MacroArg {
    GenAccessors(ArgInner<kw::gen_accessors, Ident>),
    AccessorType(ArgInner<kw::accessor_type, Path>),
    Types(ArgInner<kw::gen_types, RootTypeDef>),
}

struct ArgInner<K, V> {
    _field_kw: K,
    _equals: Token![=],
    value: V,
}

pub struct Field {
    pub attributes: Vec<Attribute>,
    pub name: Ident,
    _colon: Token![:],
    pub field_type: FieldType,
}

pub enum FieldType {
    Existing(Type),
    NewTypeDef(NewTypeDef),
}

pub struct NewTypeDef {
    _braces: token::Brace,
    pub fields: Punctuated<Field, Token![, ]>,
}

pub struct RootTypeDef {
    pub type_name: Ident,
    pub type_def: NewTypeDef,
}

impl Parse for MacroInput {
    fn parse(input: ParseStream<'_>) -> Result<Self> {
        let fields: Punctuated<MacroArg, Token![, ]> =
            Punctuated::parse_terminated_with(input, MacroArg::parse)?;
        let mut gen_accessors = None;
        let mut type_def = None;
        let mut accessor_type = None;
        for arg in fields.into_iter() {
            match arg {
                MacroArg::GenAccessors(ArgInner { value, .. }) => gen_accessors = Some(value),
                MacroArg::AccessorType(ArgInner { value, .. }) => accessor_type = Some(value),
                MacroArg::Types(ArgInner { value, .. }) => type_def = Some(value),
            }
        }

        fn missing_attr(att_name: &str) -> syn::Error {
            syn::Error::new(
                Span::call_site(),
                format!("Expected `{}` attribute", att_name),
            )
        }

        Ok(MacroInput {
            type_def: type_def.ok_or_else(|| missing_attr("gen_types"))?,
            gen_accessors: gen_accessors.ok_or_else(|| missing_attr("gen_accessors"))?,
            accessor_type: accessor_type.ok_or_else(|| missing_attr("accessor_type"))?,
        })
    }
}

impl Parse for MacroArg {
    fn parse(input: ParseStream<'_>) -> Result<Self> {
        let lookahead = input.lookahead1();
        if lookahead.peek(kw::gen_types) {
            Ok(MacroArg::Types(input.parse()?))
        } else if lookahead.peek(kw::gen_accessors) {
            Ok(MacroArg::GenAccessors(input.parse()?))
        } else if lookahead.peek(kw::accessor_type) {
            Ok(MacroArg::AccessorType(input.parse()?))
        } else {
            Err(lookahead.error())
        }
    }
}

impl<K: Parse, V: Parse> Parse for ArgInner<K, V> {
    fn parse(input: ParseStream<'_>) -> Result<Self> {
        Ok(ArgInner {
            _field_kw: input.parse()?,
            _equals: input.parse()?,
            value: input.parse()?,
        })
    }
}

impl Parse for Field {
    fn parse(input: ParseStream<'_>) -> Result<Self> {
        Ok(Field {
            attributes: input.call(Attribute::parse_outer)?,
            name: input.parse()?,
            _colon: input.parse()?,
            field_type: input.parse()?,
        })
    }
}

impl Parse for RootTypeDef {
    fn parse(input: ParseStream<'_>) -> Result<Self> {
        Ok(RootTypeDef {
            type_name: input.parse()?,
            type_def: input.parse()?,
        })
    }
}

impl Parse for NewTypeDef {
    fn parse(input: ParseStream<'_>) -> Result<Self> {
        let content;
        #[allow(clippy::mixed_read_write_in_expression)]
        Ok(NewTypeDef {
            _braces: braced!(content in input),
            fields: Punctuated::parse_terminated_with(&content, Field::parse)?,
        })
    }
}

impl Parse for FieldType {
    fn parse(input: ParseStream<'_>) -> Result<Self> {
        if input.peek(token::Brace) {
            Ok(FieldType::NewTypeDef(input.parse()?))
        } else {
            Ok(FieldType::Existing(input.parse()?))
        }
    }
}