aboutsummaryrefslogtreecommitdiff
path: root/macros/src/declare_interface_args.rs
blob: bd2f24e2ff3ac61453dbb1517c55296c09f83968 (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
57
58
59
60
61
62
63
64
use syn::parse::{Parse, ParseStream, Result};
use syn::punctuated::Punctuated;
use syn::{Path, Token, Type};

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

pub const DECLARE_INTERFACE_FLAGS: &[&str] = &["async"];

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

impl Parse for DeclareInterfaceArgs
{
    fn parse(input: ParseStream) -> Result<Self>
    {
        let implementation: Type = input.parse()?;

        input.parse::<Token![->]>()?;

        let interface: Path = input.parse()?;

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

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

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

                if !DECLARE_INTERFACE_FLAGS.contains(&flag_str.as_str()) {
                    return Err(input.error(format!(
                        "Unknown flag '{}'. Expected one of [ {} ]",
                        flag_str,
                        DECLARE_INTERFACE_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)));
            }

            flags
        } else {
            Punctuated::new()
        };

        Ok(Self {
            implementation,
            interface,
            flags,
        })
    }
}