Skip to content
This repository was archived by the owner on Jul 28, 2025. It is now read-only.

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

@deanm0000

Description

@deanm0000

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)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions