diff --git a/src/parser/parser.rs b/src/parser/parser.rs index e9e2683..295117b 100644 --- a/src/parser/parser.rs +++ b/src/parser/parser.rs @@ -19,6 +19,7 @@ use nom::multi::separated_nonempty_list; use nom::sequence::delimited; use nom::sequence::preceded; use nom::sequence::separated_pair; +use nom::sequence::terminated; use nom::sequence::tuple; use nom::IResult; @@ -32,7 +33,13 @@ enum DustTag<'a> { DTNotExists(Container<'a>), DTBlock(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)] @@ -91,6 +98,14 @@ struct NamedBlock<'a> { #[derive(Clone, Debug, PartialEq)] struct ParameterizedBlock<'a> { + name: &'a str, + params: Vec>, + contents: Option>, + else_contents: Option>, +} + +#[derive(Clone, Debug, PartialEq)] +struct Partial<'a> { name: String, params: Vec>, } @@ -136,7 +151,13 @@ fn dust_tag(i: &str) -> IResult<&str, DustTag> { conditional("{^", DustTag::DTNotExists), named_block("{+", DustTag::DTBlock), 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) } @@ -330,12 +351,88 @@ where } } -fn parameterized_self_closing_block<'a, F>( +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>( + 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| { let (i, (name, params)) = delimited( @@ -349,7 +446,7 @@ where Ok(( i, - constructor(ParameterizedBlock { + constructor(Partial { name: name, params: params.unwrap_or(Vec::new()), }), @@ -694,7 +791,7 @@ mod tests { dust_tag(r#"{>foo bar=baz animal="cat"/}"#), Ok(( "", - DustTag::DTPartial(ParameterizedBlock { + DustTag::DTPartial(Partial { name: "foo".to_owned(), params: vec![ KVPair { @@ -717,7 +814,7 @@ mod tests { dust_tag(r#"{>"template name * with * special \" characters" bar=baz animal="cat"/}"#), Ok(( "", - DustTag::DTPartial(ParameterizedBlock { + DustTag::DTPartial(Partial { name: r#"template name * with * special " characters"#.to_owned(), params: vec![ 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 + }) + )) + ); + } }