about summary refs log tree commit diff
path: root/tvix/nix-compat-derive/src/internal/inputs.rs
blob: 097a141a5d7c4c163885041900c634102db59837 (plain) (blame)
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
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RemoteInput {
    pub attrs: Vec<syn::Attribute>,
    pub ident: syn::Type,
}

impl syn::parse::Parse for RemoteInput {
    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
        let attrs = input.call(syn::Attribute::parse_outer)?;

        let ident = input.parse::<syn::Type>()?;
        Ok(RemoteInput { attrs, ident })
    }
}

impl quote::ToTokens for RemoteInput {
    fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
        fn is_outer(attr: &&syn::Attribute) -> bool {
            match attr.style {
                syn::AttrStyle::Outer => true,
                syn::AttrStyle::Inner(_) => false,
            }
        }
        for attr in self.attrs.iter().filter(is_outer) {
            attr.to_tokens(tokens);
        }
        self.ident.to_tokens(tokens);
    }
}

#[cfg(test)]
mod test {
    use syn::parse_quote;
    //use syn::parse::Parse;

    use super::*;

    #[test]
    fn test_input() {
        let p: RemoteInput = parse_quote!(u64);
        assert_eq!(
            p,
            RemoteInput {
                attrs: vec![],
                ident: parse_quote!(u64),
            }
        );
    }

    #[test]
    fn test_input_attr() {
        let p: RemoteInput = parse_quote!(
            #[nix]
            u64
        );
        assert_eq!(
            p,
            RemoteInput {
                attrs: vec![parse_quote!(#[nix])],
                ident: parse_quote!(u64),
            }
        );
    }

    #[test]
    fn test_input_attr_multiple() {
        let p: RemoteInput = parse_quote!(
            #[nix]
            #[hello]
            u64
        );
        assert_eq!(
            p,
            RemoteInput {
                attrs: vec![parse_quote!(#[nix]), parse_quote!(#[hello])],
                ident: parse_quote!(u64),
            }
        );
    }

    #[test]
    fn test_input_attr_full() {
        let p: RemoteInput = parse_quote!(
            #[nix(try_from = "u64")]
            usize
        );
        assert_eq!(
            p,
            RemoteInput {
                attrs: vec![parse_quote!(#[nix(try_from="u64")])],
                ident: parse_quote!(usize),
            }
        );
    }

    #[test]
    fn test_input_attr_other() {
        let p: RemoteInput = parse_quote!(
            #[muh]
            u64
        );
        assert_eq!(
            p,
            RemoteInput {
                attrs: vec![parse_quote!(#[muh])],
                ident: parse_quote!(u64),
            }
        );
    }
}