Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interoperable plugins, ie make plugin functions public so they can be used by rust, not just python. #129

Open
deanm0000 opened this issue Feb 20, 2025 · 0 comments

Comments

@deanm0000
Copy link

deanm0000 commented Feb 20, 2025

It'd be nice if plugins were available from rust not just python. I think the simplest way would be to have the polars_expr macro also emit a pub fn version of the function. It'd fall short if there were kwarg structs that aren't also public but that's a lot easier for plugin authors to patch for interoperability. ChatGPT suggested this but I don't know enough about macros to tell if it's good enough to put in a PR or even if that's really all it would take.

#[proc_macro_attribute]
pub fn polars_expr(attr: TokenStream, input: TokenStream) -> TokenStream {
    let ast = parse_macro_input!(input as syn::ItemFn);
    let fn_name = &ast.sig.ident;

    let options = parse_macro_input!(attr as attr::ExprsFunctionOptions);
    let expanded_field_fn = if let Some(fn_name) = options.output_type_fn {
        create_field_function(fn_name, &fn_name, false)
    } else if let Some(fn_name) = options.output_type_fn_kwargs {
        create_field_function(fn_name, &fn_name, true)
    } else if let Some(dtype) = options.output_dtype {
        create_field_function_from_with_dtype(fn_name, dtype)
    } else {
        panic!("didn't understand polars_expr attribute")
    };

    let expanded_expr = create_expression_function(&ast);
    
    // Modify the function to ensure it's public
    let mut modified_ast = ast.clone();
    modified_ast.vis = syn::Visibility::Public(syn::token::Pub { span: proc_macro2::Span::call_site() });

    let expanded = quote! {
        #expanded_field_fn
        #expanded_expr

        // Keep the original function as `pub fn`
        #modified_ast
    };

    TokenStream::from(expanded)
}
@deanm0000 deanm0000 changed the title make the polars_expr macro create a pub fn version of the function Interoperable plugins, ie make plugin functions public so they can be used by rust, not just python. Feb 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant