Merge branch 'helpers'
This commit is contained in:
commit
608c55575e
@ -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;
|
||||||
|
|
||||||
@ -32,7 +33,13 @@ enum DustTag<'a> {
|
|||||||
DTNotExists(Container<'a>),
|
DTNotExists(Container<'a>),
|
||||||
DTBlock(NamedBlock<'a>),
|
DTBlock(NamedBlock<'a>),
|
||||||
DTInlinePartial(NamedBlock<'a>),
|
DTInlinePartial(NamedBlock<'a>),
|
||||||
DTPartial(ParameterizedBlock<'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)]
|
||||||
@ -91,6 +98,14 @@ struct NamedBlock<'a> {
|
|||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
struct ParameterizedBlock<'a> {
|
struct ParameterizedBlock<'a> {
|
||||||
|
name: &'a str,
|
||||||
|
params: Vec<KVPair<'a>>,
|
||||||
|
contents: Option<Body<'a>>,
|
||||||
|
else_contents: Option<Body<'a>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
|
struct Partial<'a> {
|
||||||
name: String,
|
name: String,
|
||||||
params: Vec<KVPair<'a>>,
|
params: Vec<KVPair<'a>>,
|
||||||
}
|
}
|
||||||
@ -136,7 +151,13 @@ fn dust_tag(i: &str) -> IResult<&str, DustTag> {
|
|||||||
conditional("{^", DustTag::DTNotExists),
|
conditional("{^", DustTag::DTNotExists),
|
||||||
named_block("{+", DustTag::DTBlock),
|
named_block("{+", DustTag::DTBlock),
|
||||||
named_block("{<", DustTag::DTInlinePartial),
|
named_block("{<", DustTag::DTInlinePartial),
|
||||||
parameterized_self_closing_block("{>", 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)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -330,12 +351,88 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parameterized_self_closing_block<'a, F>(
|
fn parameterized_block<'a, F>(
|
||||||
open_matcher: &'static str,
|
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,
|
constructor: F,
|
||||||
) -> impl Fn(&'a str) -> IResult<&'a str, DustTag<'a>>
|
) -> impl Fn(&'a str) -> IResult<&'a str, DustTag<'a>>
|
||||||
where
|
where
|
||||||
F: Fn(ParameterizedBlock<'a>) -> DustTag<'a>,
|
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>(
|
||||||
|
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)) = delimited(
|
||||||
|
tag(open_matcher),
|
||||||
|
tuple((
|
||||||
|
tag(tag_name),
|
||||||
|
opt(preceded(space1, separated_list(space1, key_value_pair))),
|
||||||
|
)),
|
||||||
|
tag("/}"),
|
||||||
|
)(i)?;
|
||||||
|
|
||||||
|
Ok((
|
||||||
|
i,
|
||||||
|
constructor(ParameterizedBlock {
|
||||||
|
name: name,
|
||||||
|
params: params.unwrap_or(Vec::new()),
|
||||||
|
contents: None,
|
||||||
|
else_contents: None,
|
||||||
|
}),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn partial<'a, F>(
|
||||||
|
open_matcher: &'static str,
|
||||||
|
constructor: F,
|
||||||
|
) -> impl Fn(&'a str) -> IResult<&'a str, DustTag<'a>>
|
||||||
|
where
|
||||||
|
F: Fn(Partial<'a>) -> DustTag<'a>,
|
||||||
{
|
{
|
||||||
move |i: &'a str| {
|
move |i: &'a str| {
|
||||||
let (i, (name, params)) = delimited(
|
let (i, (name, params)) = delimited(
|
||||||
@ -349,7 +446,7 @@ where
|
|||||||
|
|
||||||
Ok((
|
Ok((
|
||||||
i,
|
i,
|
||||||
constructor(ParameterizedBlock {
|
constructor(Partial {
|
||||||
name: name,
|
name: name,
|
||||||
params: params.unwrap_or(Vec::new()),
|
params: params.unwrap_or(Vec::new()),
|
||||||
}),
|
}),
|
||||||
@ -694,7 +791,7 @@ mod tests {
|
|||||||
dust_tag(r#"{>foo bar=baz animal="cat"/}"#),
|
dust_tag(r#"{>foo bar=baz animal="cat"/}"#),
|
||||||
Ok((
|
Ok((
|
||||||
"",
|
"",
|
||||||
DustTag::DTPartial(ParameterizedBlock {
|
DustTag::DTPartial(Partial {
|
||||||
name: "foo".to_owned(),
|
name: "foo".to_owned(),
|
||||||
params: vec![
|
params: vec![
|
||||||
KVPair {
|
KVPair {
|
||||||
@ -717,7 +814,7 @@ mod tests {
|
|||||||
dust_tag(r#"{>"template name * with * special \" characters" bar=baz animal="cat"/}"#),
|
dust_tag(r#"{>"template name * with * special \" characters" bar=baz animal="cat"/}"#),
|
||||||
Ok((
|
Ok((
|
||||||
"",
|
"",
|
||||||
DustTag::DTPartial(ParameterizedBlock {
|
DustTag::DTPartial(Partial {
|
||||||
name: r#"template name * with * special " characters"#.to_owned(),
|
name: r#"template name * with * special " characters"#.to_owned(),
|
||||||
params: vec![
|
params: vec![
|
||||||
KVPair {
|
KVPair {
|
||||||
@ -733,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…
x
Reference in New Issue
Block a user