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

Add metadata to functions script side #925

Open
stargazing-dino opened this issue Oct 29, 2024 · 6 comments
Open

Add metadata to functions script side #925

stargazing-dino opened this issue Oct 29, 2024 · 6 comments

Comments

@stargazing-dino
Copy link

Hi, is there a way to add metadata to Rhai functions? Kind of similar to Python's decorator syntax.

@app.route("/users/<id>")
def get_user(id):
    return fetch_user(id)

Or even Rust's macros (example from Actix)

#[get("/")]
async fn hello() -> impl Responder {
    HttpResponse::Ok().body("Hello world!")
}

The examples I gave might be doing more than what I'm after or working in a completely different way. For my use case I don't need anything other than marking certain functions with with arbitrary attributes (perhaps with data).

One workaround I was thinking of was maybe forcing the user to make an object, defining an object with the function as a closure and my metadata on a field but it looks kinda odd:

let my_function = #{
    metadata: 42,
    function: || // do_something_cool
};

and I'd wish there was a way to just do something like:

@(my_data(42))
fn call_me() {
    return 3;
}

Is this possible through custom syntax perhaps or even now in some kind of work around way?

@schungx
Copy link
Collaborator

schungx commented Oct 29, 2024

You can always access metadata in Rust. So I suppose you want to access metadata in code?

https://rhai.rs/book/engine/metadata/index.html

If you'd like to put arbitrary data inside the metadata, currently there is no easy way to do this, other than put it inside a doc-comment.

@stargazing-dino
Copy link
Author

Yes, I meant specifically access metadata of a function in Rhai code.

On a complexity scale how hard would implement something like this directly into Rhai be?

In the meantime, I'll see if I can't get by with the doc comments alternative - thank you for the suggestion:)

@schungx
Copy link
Collaborator

schungx commented Oct 30, 2024

Quite complex as it would require extending the syntax. Then we'll need to design the syntax to make sure it is flexible enough.

@schungx
Copy link
Collaborator

schungx commented Oct 30, 2024

If you use doc comments, it is built in.

But you have to parse the MarkDown. You can use a crate for it.

@stargazing-dino
Copy link
Author

The markdown idea is definitely interesting! It might be more than what I want for my usecase though. I haven't tried it yet but I think I'll likely do something like this:

fn call_me() {
    return 3;
}

let extra_data = Route("/");

register(call_me, extra_data);

And the register function could do all the fancy logic. Its signature could take the Fn_Ptr and a Dynamic. A little more setup but I think it should cover my immediate needs. I wonder, would I be able to do something like this?

call_me.register(extra_data);

(anyways don't mind me too much, I'm going to play with all this this week)

@schungx
Copy link
Collaborator

schungx commented Oct 31, 2024

call_me.register(extra_data)

Yes you can. Just register a method for the FnPtr type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants