-
Notifications
You must be signed in to change notification settings - Fork 281
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
[FEATURE REQUEST] Macros for making Context have less boilerplate #861
Comments
This will probably be added in v2 |
@MarcusSanchez I wrote this very simple implementation for my personal projects: #[macro_export]
macro_rules! context {
(
$(
$key:ident $(=> $value:expr)? $(,)*
)*
) => {
{
let mut context = Context::new();
$(
context.insert(stringify!($key), $($value)?);
)*
context
}
};
} Using it this way: let ctx = context! {
name => "Jack",
age => 24
}; Expands to: let ctx = {
let mut context = Context::new();
context.insert("name", "Jack");
context.insert("age", 24);
context
}; |
We could also get inspiration from minijinja's implementation |
Made a pull request #887 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Make a macro that allows quick Context for easy drop in values. Something like this:
That allows this:
to turn into:
The exact syntax for the macro doesn't really matter, anything really will be nice, just not a context.insert() every line, this will also make it easier to pass these as parameters.
The text was updated successfully, but these errors were encountered: