Open MarcusSanchez opened 11 months ago
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
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.