Add all the helpers
This commit is contained in:
parent
15c732b3a3
commit
05d9e20a8f
@ -19,6 +19,7 @@ use nom::multi::separated_nonempty_list;
|
|||||||
use nom::sequence::delimited;
|
use nom::sequence::delimited;
|
||||||
use nom::sequence::preceded;
|
use nom::sequence::preceded;
|
||||||
use nom::sequence::separated_pair;
|
use nom::sequence::separated_pair;
|
||||||
|
use nom::sequence::terminated;
|
||||||
use nom::sequence::tuple;
|
use nom::sequence::tuple;
|
||||||
use nom::IResult;
|
use nom::IResult;
|
||||||
|
|
||||||
@ -33,6 +34,12 @@ enum DustTag<'a> {
|
|||||||
DTBlock(NamedBlock<'a>),
|
DTBlock(NamedBlock<'a>),
|
||||||
DTInlinePartial(NamedBlock<'a>),
|
DTInlinePartial(NamedBlock<'a>),
|
||||||
DTPartial(Partial<'a>),
|
DTPartial(Partial<'a>),
|
||||||
|
DTHelperEquals(ParameterizedBlock<'a>),
|
||||||
|
DTHelperNotEquals(ParameterizedBlock<'a>),
|
||||||
|
DTHelperGreaterThan(ParameterizedBlock<'a>),
|
||||||
|
DTHelperLessThan(ParameterizedBlock<'a>),
|
||||||
|
DTHelperGreaterThenOrEquals(ParameterizedBlock<'a>),
|
||||||
|
DTHelperLessThenOrEquals(ParameterizedBlock<'a>),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
@ -145,6 +152,12 @@ fn dust_tag(i: &str) -> IResult<&str, DustTag> {
|
|||||||
named_block("{+", DustTag::DTBlock),
|
named_block("{+", DustTag::DTBlock),
|
||||||
named_block("{<", DustTag::DTInlinePartial),
|
named_block("{<", DustTag::DTInlinePartial),
|
||||||
partial("{>", DustTag::DTPartial),
|
partial("{>", DustTag::DTPartial),
|
||||||
|
parameterized_block("{@", "gte", DustTag::DTHelperGreaterThenOrEquals),
|
||||||
|
parameterized_block("{@", "lte", DustTag::DTHelperLessThenOrEquals),
|
||||||
|
parameterized_block("{@", "eq", DustTag::DTHelperEquals),
|
||||||
|
parameterized_block("{@", "ne", DustTag::DTHelperNotEquals),
|
||||||
|
parameterized_block("{@", "gt", DustTag::DTHelperGreaterThan),
|
||||||
|
parameterized_block("{@", "lt", DustTag::DTHelperLessThan),
|
||||||
))(i)
|
))(i)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -338,6 +351,52 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn parameterized_block<'a, F>(
|
||||||
|
open_matcher: &'static str,
|
||||||
|
tag_name: &'static str,
|
||||||
|
constructor: F,
|
||||||
|
) -> impl Fn(&'a str) -> IResult<&'a str, DustTag<'a>>
|
||||||
|
where
|
||||||
|
F: Copy + Fn(ParameterizedBlock<'a>) -> DustTag<'a>,
|
||||||
|
{
|
||||||
|
alt((
|
||||||
|
parameterized_block_with_body(open_matcher, tag_name, constructor),
|
||||||
|
parameterized_self_closing_block(open_matcher, tag_name, constructor),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parameterized_block_with_body<'a, F>(
|
||||||
|
open_matcher: &'static str,
|
||||||
|
tag_name: &'static str,
|
||||||
|
constructor: F,
|
||||||
|
) -> impl Fn(&'a str) -> IResult<&'a str, DustTag<'a>>
|
||||||
|
where
|
||||||
|
F: Fn(ParameterizedBlock<'a>) -> DustTag<'a>,
|
||||||
|
{
|
||||||
|
move |i: &'a str| {
|
||||||
|
let (i, (name, params, inner, maybe_else, _closing_name)) = tuple((
|
||||||
|
preceded(tag(open_matcher), tag(tag_name)),
|
||||||
|
terminated(
|
||||||
|
opt(preceded(space1, separated_list(space1, key_value_pair))),
|
||||||
|
tag("}"),
|
||||||
|
),
|
||||||
|
opt(body),
|
||||||
|
opt(preceded(tag("{:else}"), opt(body))),
|
||||||
|
delimited(tag("{/"), tag(tag_name), tag("}")),
|
||||||
|
))(i)?;
|
||||||
|
|
||||||
|
Ok((
|
||||||
|
i,
|
||||||
|
constructor(ParameterizedBlock {
|
||||||
|
name: name,
|
||||||
|
params: params.unwrap_or(Vec::new()),
|
||||||
|
contents: inner,
|
||||||
|
else_contents: maybe_else.flatten(),
|
||||||
|
}),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn parameterized_self_closing_block<'a, F>(
|
fn parameterized_self_closing_block<'a, F>(
|
||||||
open_matcher: &'static str,
|
open_matcher: &'static str,
|
||||||
tag_name: &'static str,
|
tag_name: &'static str,
|
||||||
@ -771,4 +830,65 @@ mod tests {
|
|||||||
))
|
))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_helper() {
|
||||||
|
assert_eq!(
|
||||||
|
dust_tag(r#"{@eq key=name value="cat"}Pet the {name}!{/eq}"#),
|
||||||
|
Ok((
|
||||||
|
"",
|
||||||
|
DustTag::DTHelperEquals(ParameterizedBlock {
|
||||||
|
name: "eq",
|
||||||
|
params: vec![
|
||||||
|
KVPair {
|
||||||
|
key: "key",
|
||||||
|
value: RValue::RVPath(Path { keys: vec!["name"] })
|
||||||
|
},
|
||||||
|
KVPair {
|
||||||
|
key: "value",
|
||||||
|
value: RValue::RVString("cat".to_owned())
|
||||||
|
}
|
||||||
|
],
|
||||||
|
contents: Some(Body {
|
||||||
|
elements: vec![
|
||||||
|
TemplateElement::TESpan(Span {
|
||||||
|
contents: "Pet the "
|
||||||
|
}),
|
||||||
|
TemplateElement::TETag(DustTag::DTReference(Reference {
|
||||||
|
path: Path { keys: vec!["name"] },
|
||||||
|
filters: Vec::new()
|
||||||
|
})),
|
||||||
|
TemplateElement::TESpan(Span { contents: "!" })
|
||||||
|
]
|
||||||
|
}),
|
||||||
|
else_contents: None
|
||||||
|
})
|
||||||
|
))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_self_closing_helper() {
|
||||||
|
assert_eq!(
|
||||||
|
dust_tag(r#"{@eq key=name value="cat"/}"#),
|
||||||
|
Ok((
|
||||||
|
"",
|
||||||
|
DustTag::DTHelperEquals(ParameterizedBlock {
|
||||||
|
name: "eq",
|
||||||
|
params: vec![
|
||||||
|
KVPair {
|
||||||
|
key: "key",
|
||||||
|
value: RValue::RVPath(Path { keys: vec!["name"] })
|
||||||
|
},
|
||||||
|
KVPair {
|
||||||
|
key: "value",
|
||||||
|
value: RValue::RVString("cat".to_owned())
|
||||||
|
}
|
||||||
|
],
|
||||||
|
contents: None,
|
||||||
|
else_contents: None
|
||||||
|
})
|
||||||
|
))
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user