arrow2_convert_derive/
lib.rs

1use proc_macro_error::{abort, proc_macro_error};
2
3mod derive_enum;
4mod derive_struct;
5mod input;
6
7use input::*;
8
9/// Derive macro for arrow fields
10#[proc_macro_error]
11#[proc_macro_derive(ArrowField, attributes(arrow_field))]
12pub fn arrow2_convert_derive_field(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
13    let ast: syn::DeriveInput = syn::parse(input).unwrap();
14
15    match &ast.data {
16        syn::Data::Enum(e) => derive_enum::expand_field(DeriveEnum::from_ast(&ast, e)).into(),
17        syn::Data::Struct(s) => derive_struct::expand_field(DeriveStruct::from_ast(&ast, s)).into(),
18        _ => {
19            abort!(ast.ident.span(), "Only structs and enums supported");
20        }
21    }
22}
23
24/// Derive macro for arrow serialize
25#[proc_macro_error]
26#[proc_macro_derive(ArrowSerialize, attributes(arrow_field))]
27pub fn arrow2_convert_derive_serialize(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
28    let ast: syn::DeriveInput = syn::parse(input).unwrap();
29
30    match &ast.data {
31        syn::Data::Enum(e) => derive_enum::expand_serialize(DeriveEnum::from_ast(&ast, e)).into(),
32        syn::Data::Struct(s) => {
33            derive_struct::expand_serialize(DeriveStruct::from_ast(&ast, s)).into()
34        }
35        _ => {
36            abort!(ast.ident.span(), "Only structs and enums supported");
37        }
38    }
39}
40
41/// Derive macro for arrow deserialize
42#[proc_macro_error]
43#[proc_macro_derive(ArrowDeserialize, attributes(arrow_field))]
44pub fn arrow2_convert_derive_deserialize(
45    input: proc_macro::TokenStream,
46) -> proc_macro::TokenStream {
47    let ast: syn::DeriveInput = syn::parse(input).unwrap();
48
49    match &ast.data {
50        syn::Data::Enum(e) => derive_enum::expand_deserialize(DeriveEnum::from_ast(&ast, e)).into(),
51        syn::Data::Struct(s) => {
52            derive_struct::expand_deserialize(DeriveStruct::from_ast(&ast, s)).into()
53        }
54        _ => {
55            abort!(ast.ident.span(), "Only structs and enums supported");
56        }
57    }
58}