cbored_derive/
lib.rs

1use proc_macro::TokenStream;
2use syn::{Data, DeriveInput};
3
4mod attr;
5mod common;
6mod product;
7mod sum;
8mod ty;
9
10use attr::get_my_attributes;
11use product::derive_struct;
12use sum::derive_enum;
13
14#[proc_macro_derive(CborRepr, attributes(cborrepr))]
15pub fn derive_cbor_repr(input: TokenStream) -> TokenStream {
16    // Parse type (struct/enum)
17    let ast = syn::parse_macro_input!(input as DeriveInput);
18
19    let has_generics = ast.generics.params.len() > 0;
20    if has_generics {
21        panic!("cannot handle types with generics")
22    }
23
24    // future input'ing - unused for now
25    let _ = ty::parse(ast.clone());
26
27    // Gather the cborrepr attributes as Meta
28    let attrs = get_my_attributes(&ast.attrs).collect::<Vec<_>>();
29
30    // either do struct or enum handling
31    match ast.data {
32        Data::Struct(st) => derive_struct(ast.ident, &attrs, st),
33        Data::Enum(e) => derive_enum(ast.ident, &attrs, e),
34        Data::Union(_) => panic!("Union not supported"),
35    }
36}