aboutsummaryrefslogtreecommitdiff
path: root/macros/src/declare_interface_args.rs
blob: 6556153eb42a7bbd6dcdf89047a68b6c887c1b8c (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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use syn::parse::{Parse, ParseStream};
use syn::punctuated::Punctuated;
use syn::{Token, TypePath};

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

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

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

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

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

        let interface: TypePath = 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_name = flag.name().to_string();

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

            if let Some((dupe_flag, _)) = flags.iter().find_duplicate() {
                return Err(input.error(format!("Duplicate flag '{}'", dupe_flag.name())));
            }

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

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

#[cfg(test)]
mod tests
{
    use proc_macro2::Span;
    use quote::{format_ident, quote};
    use syn::{parse2, Lit, LitBool};

    use super::*;
    use crate::macro_flag::MacroFlagValue;
    use crate::test_utils;

    #[test]
    fn can_parse_with_no_flags()
    {
        let input_args = quote! {
            Foo -> IFoo
        };

        let decl_interface_args = parse2::<DeclareInterfaceArgs>(input_args).unwrap();

        assert_eq!(
            decl_interface_args.implementation,
            TypePath {
                qself: None,
                path: test_utils::create_path(&[test_utils::create_path_segment(
                    format_ident!("Foo"),
                    &[]
                )])
            }
        );

        assert_eq!(
            decl_interface_args.interface,
            TypePath {
                qself: None,
                path: test_utils::create_path(&[test_utils::create_path_segment(
                    format_ident!("IFoo"),
                    &[]
                )])
            }
        );

        assert!(decl_interface_args.flags.is_empty());
    }

    #[test]
    fn can_parse_with_flags()
    {
        let input_args = quote! {
            Foobar -> IFoobar, threadsafe_sharable = true
        };

        let decl_interface_args = parse2::<DeclareInterfaceArgs>(input_args).unwrap();

        assert_eq!(
            decl_interface_args.implementation,
            TypePath {
                qself: None,
                path: test_utils::create_path(&[test_utils::create_path_segment(
                    format_ident!("Foobar"),
                    &[]
                )])
            }
        );

        assert_eq!(
            decl_interface_args.interface,
            TypePath {
                qself: None,
                path: test_utils::create_path(&[test_utils::create_path_segment(
                    format_ident!("IFoobar"),
                    &[]
                )])
            }
        );

        assert_eq!(
            decl_interface_args.flags,
            Punctuated::from_iter(vec![MacroFlag {
                name: format_ident!("threadsafe_sharable"),
                value: MacroFlagValue::Literal(Lit::Bool(LitBool::new(
                    true,
                    Span::call_site()
                )))
            }])
        );
    }

    #[test]
    fn cannot_parse_with_invalid_flag()
    {
        let input_args = quote! {
            Foobar -> IFoobar, xyz = false, threadsafe_sharable = true
        };

        assert!(parse2::<DeclareInterfaceArgs>(input_args).is_err());
    }

    #[test]
    fn cannot_parse_with_duplicate_flag()
    {
        assert!(
            // Formatting is weird without this comment
            parse2::<DeclareInterfaceArgs>(quote! {
                Foobar -> IFoobar, threadsafe_sharable = true, threadsafe_sharable = true
            })
            .is_err()
        );

        assert!(
            // Formatting is weird without this comment
            parse2::<DeclareInterfaceArgs>(quote! {
                Foobar -> IFoobar, threadsafe_sharable = true, threadsafe_sharable = false
            })
            .is_err()
        );
    }
}