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
pub(crate) mod restructure;
mod validation;

use std::fmt::Formatter;
use syn::braced;
use syn::parse::{Parse, ParseStream, Result};
use syn::punctuated::Punctuated;

/// An ordered list of attribute arguments, which consists of (id, param-args) pairs.
#[derive(Clone)]
pub(crate) struct AttributeArgList {
    pub(crate) args: Punctuated<IdentifiedArgList, Token![,]>,
}

impl Parse for AttributeArgList {
    /// This part parses
    /// It uses IdentifiedArgList.parse() for each inner argument.
    ///
    /// ['IdentifiedArgList.parse ']: struct.IdentifiedArgList
    fn parse(input: ParseStream) -> Result<Self> {
        Ok(AttributeArgList {
            args: Punctuated::parse_terminated(input)?,
        })
    }
}

/// A single (id, param-args) pair which consists of:
///   id: identifier for the list
///   param_args: ordered list arguments formatted using curly-braced list syntax, i.e. "{ 3, 4, 5 }"
#[derive(Clone)]
pub(crate) struct IdentifiedArgList {
    pub(crate) id: syn::Ident,
    assignment: Token![=],
    braces: syn::token::Brace,
    pub(crate) param_args: Punctuated<syn::Expr, Token![,]>,
}

impl std::fmt::Debug for IdentifiedArgList {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.write_str(&format!("IdentifiedArgList(id = {:?})", self.id))
    }
}

impl Parse for IdentifiedArgList {
    // parts:
    //
    // v = { a, b, c }
    // $ident $Token![=] ${ $expr, ... }
    fn parse(input: ParseStream) -> Result<Self> {
        let content;

        Ok(IdentifiedArgList {
            id: input.parse()?,
            assignment: input.parse()?,
            braces: braced!(content in input),
            param_args: Punctuated::parse_terminated(&content)?,
        })
    }
}