pyspark_arrow_rs_impl/
lib.rs

1use proc_macro::TokenStream;
2use quote::quote;
3use syn;
4
5#[proc_macro_derive(HasArrowSparkSchema)]
6pub fn macro_derive(input: TokenStream) -> TokenStream {
7    // Construct a representation of Rust code as a syntax tree
8    // that we can manipulate
9    let ast = syn::parse(input).unwrap();
10
11    // Build the trait implementation
12    impl_macro(&ast)
13}
14
15fn impl_macro(ast: &syn::DeriveInput) -> TokenStream {
16    let name = &ast.ident;
17    let gen = quote! {
18        impl HasArrowSparkSchema for #name {
19            fn get_arrow_schema()  -> Vec<std::sync::Arc<arrow::datatypes::Field>> {
20                pyspark_arrow_rs::get_arrow_schema::<Self>()
21            }
22
23            fn get_spark_ddl() -> anyhow::Result<String> {
24                let fields = Self::get_arrow_schema();
25                pyspark_arrow_rs::get_spark_ddl(fields)
26            }
27        }
28    };
29    gen.into()
30}