neli_proc_macros/
neli_enum.rs

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
use proc_macro::TokenStream;
use proc_macro2::{Span, TokenStream as TokenStream2};
use quote::quote;
use syn::{
    parse, parse_str, Arm, Attribute, Expr, Ident, ItemEnum, Lit, Meta, Path, Token, Type, Variant,
};

use crate::shared::remove_bad_attrs;

fn parse_type_attr(attr: Meta) -> Type {
    if let Meta::NameValue(nv) = attr {
        if nv.path == parse_str::<Path>("serialized_type").unwrap() {
            if let Lit::Str(ls) = nv.lit {
                return parse_str::<Type>(&ls.value())
                    .unwrap_or_else(|_| panic!("Invalid type supplied: {}", ls.value()));
            }
        }
    }

    panic!("Attribute in the form #[neli(serialized_type = \"TYPE_LITERAL_STR\")] required")
}

fn parse_enum(enm: &mut ItemEnum, ty: &Type) -> Vec<(Vec<Attribute>, Ident, Expr)> {
    let exprs = enm
        .variants
        .iter_mut()
        .map(|var| {
            if let Some((_, expr)) = var.discriminant.take() {
                (var.attrs.clone(), var.ident.clone(), expr)
            } else {
                panic!("All variants in the provided enum require an expression assignment")
            }
        })
        .collect();
    if !enm.variants.trailing_punct() {
        enm.variants.push_punct(Token![,](Span::call_site()));
    }
    enm.variants.push_value(
        parse::<Variant>(TokenStream::from(quote! {
            UnrecognizedConst(#ty)
        }))
        .expect("Could not parse tokens as a variant"),
    );
    exprs
}

fn parse_from_info(
    enum_name: Ident,
    var_info: Vec<(Vec<Attribute>, Ident, Expr)>,
) -> (Vec<Arm>, Vec<Arm>) {
    let mut from_const_info = Vec::new();
    let mut from_type_info = Vec::new();
    for (mut attributes, ident, expr) in var_info {
        attributes = remove_bad_attrs(attributes);
        let mut from_const_arm = parse::<Arm>(TokenStream::from(quote! {
            #(
                #attributes
            )*
            i if i == #expr => #enum_name::#ident,
        }))
        .expect("Failed to parse tokens as a match arm");
        from_const_arm.attrs = attributes.clone();
        from_const_info.push(from_const_arm);

        let mut from_type_arm = parse::<Arm>(TokenStream::from(quote! {
            #(
                #attributes
            )*
            #enum_name::#ident => #expr,
        }))
        .expect("Failed to parse tokens as a match arm");
        from_type_arm.attrs = attributes.clone();
        from_type_info.push(from_type_arm);
    }
    (from_const_info, from_type_info)
}

pub fn generate_neli_enum(mut enm: ItemEnum, meta: Meta) -> TokenStream2 {
    let enum_name = enm.ident.clone();
    let ty = parse_type_attr(meta);

    let variant_info = parse_enum(&mut enm, &ty);
    let (from_const_info, from_type_info) = parse_from_info(enum_name.clone(), variant_info);

    quote! {
        #[derive(Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
        #[allow(missing_docs)]
        #enm

        impl #enum_name {
            /// Check whether a given method is an unrecognized
            /// constant for the set of possible constants
            /// associated with the current type.
            pub fn is_unrecognized(&self) -> bool {
                match *self {
                    #enum_name::UnrecognizedConst(_) => true,
                    _ => false,
                }
            }
        }

        impl neli::Size for #enum_name {
            fn unpadded_size(&self) -> usize {
                std::mem::size_of::<#ty>()
            }
        }

        impl neli::TypeSize for #enum_name {
            fn type_size() -> usize {
                std::mem::size_of::<#ty>()
            }
        }

        impl neli::ToBytes for #enum_name {
            fn to_bytes(&self, buffer: &mut std::io::Cursor<Vec<u8>>) -> Result<(), neli::err::SerError> {
                let bin_rep: #ty = self.into();
                bin_rep.to_bytes(buffer)
            }
        }

        impl<'lt> neli::FromBytes<'lt> for #enum_name {
            fn from_bytes(buffer: &mut std::io::Cursor<&'lt [u8]>) -> Result<Self, neli::err::DeError> {
                Ok(#enum_name::from(<#ty as neli::FromBytes>::from_bytes(
                    buffer
                )?))
            }
        }

        impl From<#ty> for #enum_name {
            fn from(cnst: #ty) -> Self {
                match cnst {
                    #(
                        #from_const_info
                    )*
                    i => #enum_name::UnrecognizedConst(i),
                }
            }
        }

        impl From<#enum_name> for #ty {
            fn from(enm: #enum_name) -> Self {
                match enm {
                    #(
                        #from_type_info
                    )*
                    #enum_name::UnrecognizedConst(i) => i,
                }
            }
        }

        impl From<&#enum_name> for #ty {
            fn from(enm: &#enum_name) -> Self {
                match *enm {
                    #(
                        #from_type_info
                    )*
                    #enum_name::UnrecognizedConst(i) => i,
                }
            }
        }
    }
}