Derive Macro include_js::JSTemplate [−][src]
#[derive(JSTemplate)]
{
// Attributes available to this derive:
#[include_js]
}
Expand description
Derives the JSTemplate
trait for a struct with named fields.
This is simmilar to plain include_js!
with the difference that
the Javascript is not yet fully filled in, so a template engine (in this case Handlebars
)
to fill in the values at runtime.
Note: Currently the only supported attribute is #[include_js(template = "SOME/PATH")]
and it is
required to specify it. The capabilities may be expanded in the future.
Warning: The ability of this macro to actually prove that the file contains valid Javascript once filled
in is kind of limited. It assumes that you will only fill-in expressions via the template engine; so to be able to
atleast do some kind of check it will use []
as a placeholder for every expression.
I might add the ability to disable the compiletime check or to enable an optional runtime check at some point, but this is
not implemented yet.
Examples
Let this be your JS template script.
src/js/move_window.js.handlebars
let w = global
.map(a => a.meta_window)
.filter(w => w.wm_class == "{{window_class}}")
.reduce((acc, x) => (acc && acc.id > x.id) ? acc : x, null);
w.move_resize_frame(true, {{x}}, {{y}}, {{width}}, {{height}});
You can then do the following.
use include_js::{JSString, JSTemplate};
#[derive(JSTemplate)]
#[include_js(template = "src/js/move_window.js.handlebars")]
struct MoveWindowCommand {
x: u32,
y: u32,
width: u32,
height: u32,
window_class: String,
}
let js: JSString = MoveWindowCommand {
x: 0,
y: 5,
width: 100,
height: 200,
window_class: "org.gnome.Nautilus".to_owned(),
}.render_template();
let expected = r#"
let w = global
.map(a => a.meta_window)
.filter(w => w.wm_class == "org.gnome.Nautilus")
.reduce((acc, x) => (acc && acc.id > x.id) ? acc : x, null);
w.move_resize_frame(true, 0, 5, 100, 200);
"#;
assert_eq!(expected, &*js);