aboutsummaryrefslogtreecommitdiff
path: root/macros/src/factory/declare_default_args.rs
blob: 6450583d314cd42bbbd71cb3319f3c6fdd41b4b3 (plain)
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
use syn::parse::Parse;
use syn::punctuated::Punctuated;
use syn::{Token, Type};

use crate::macro_flag::MacroFlag;
use crate::util::iterator_ext::IteratorExt;

pub const FACTORY_MACRO_FLAGS: &[&str] = &["threadsafe"];

pub struct DeclareDefaultFactoryMacroArgs
{
    pub interface: Type,
    pub flags: Punctuated<MacroFlag, Token![,]>,
}

impl Parse for DeclareDefaultFactoryMacroArgs
{
    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self>
    {
        let interface = input.parse().unwrap();

        if !input.peek(Token![,]) {
            return Ok(Self {
                interface,
                flags: Punctuated::new(),
            });
        }

        input.parse::<Token![,]>().unwrap();

        let flags = Punctuated::<MacroFlag, Token![,]>::parse_terminated(input)?;

        for flag in &flags {
            let flag_str = flag.flag.to_string();

            if !FACTORY_MACRO_FLAGS.contains(&flag_str.as_str()) {
                return Err(input.error(format!(
                    "Unknown flag '{}'. Expected one of [ {} ]",
                    flag_str,
                    FACTORY_MACRO_FLAGS.join(",")
                )));
            }
        }

        let flag_names = flags
            .iter()
            .map(|flag| flag.flag.to_string())
            .collect::<Vec<_>>();

        if let Some(dupe_flag_name) = flag_names.iter().find_duplicate() {
            return Err(input.error(format!("Duplicate flag '{}'", dupe_flag_name)));
        }

        Ok(Self { interface, flags })
    }
}