Add some helpful macros
This commit is contained in:
parent
482f06ee25
commit
5278f3580c
46
src/lib.rs
46
src/lib.rs
|
|
@ -198,16 +198,38 @@ pub struct Newt {
|
||||||
ast: Ast,
|
ast: Ast,
|
||||||
}
|
}
|
||||||
impl Newt {
|
impl Newt {
|
||||||
fn build(tmpl: &str) -> Self {
|
pub fn build(tmpl: &str) -> Self {
|
||||||
let ast = parse(tmpl);
|
let ast = parse(tmpl);
|
||||||
Self { ast }
|
Self { ast }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn execute(&self, value: &dyn Value) -> String {
|
pub fn execute(&self, value: &dyn Value) -> String {
|
||||||
execute(&self.ast, value)
|
execute(&self.ast, value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! values_list_map {
|
||||||
|
($($key:literal: $val:expr),* $(,)?) => {{
|
||||||
|
&[
|
||||||
|
$(
|
||||||
|
($key, $val as &dyn Value)
|
||||||
|
)*,
|
||||||
|
]
|
||||||
|
}}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! values {
|
||||||
|
($($val:expr),* $(,)?) => {{
|
||||||
|
&[
|
||||||
|
$(
|
||||||
|
&$val as &dyn Value,
|
||||||
|
)*
|
||||||
|
]
|
||||||
|
}}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use crate::{Newt, Value};
|
use crate::{Newt, Value};
|
||||||
|
|
@ -225,7 +247,9 @@ mod test {
|
||||||
fn print_lookup() {
|
fn print_lookup() {
|
||||||
go(
|
go(
|
||||||
"Hello, {.name}!",
|
"Hello, {.name}!",
|
||||||
&[("name", &"Ted" as &dyn Value)],
|
values_list_map! {
|
||||||
|
"name": &"Ted",
|
||||||
|
},
|
||||||
"Hello, Ted!",
|
"Hello, Ted!",
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
@ -234,7 +258,7 @@ mod test {
|
||||||
fn print_for() {
|
fn print_for() {
|
||||||
go(
|
go(
|
||||||
"{for .}{.}, {/for}",
|
"{for .}{.}, {/for}",
|
||||||
&[&"Bob" as &dyn Value, &"Larry"],
|
values!["Bob", "Larry"],
|
||||||
"Bob, Larry, ",
|
"Bob, Larry, ",
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
@ -243,15 +267,11 @@ mod test {
|
||||||
fn print_for_nested() {
|
fn print_for_nested() {
|
||||||
go(
|
go(
|
||||||
"{for .items}{for .names}{.}, {/for}{/for}",
|
"{for .items}{for .names}{.}, {/for}{/for}",
|
||||||
&[(
|
values_list_map! {
|
||||||
"items",
|
"items": values![values_list_map! {
|
||||||
&[
|
"names": values!["Bob", "Larry"],
|
||||||
&[(
|
}]
|
||||||
"names",
|
},
|
||||||
&[&"Bob" as &dyn Value, &"Larry"] as &dyn Value,
|
|
||||||
)] as &dyn Value,
|
|
||||||
] as &dyn Value,
|
|
||||||
)],
|
|
||||||
"Bob, Larry, ",
|
"Bob, Larry, ",
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue