From b7120a34de0224b0810caaf26d74db21b0c89778 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Tue, 7 Apr 2020 20:21:26 -0400 Subject: [PATCH 1/3] Forking the partial code for reuse as helper code --- src/parser/parser.rs | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/parser/parser.rs b/src/parser/parser.rs index e9e2683..3233178 100644 --- a/src/parser/parser.rs +++ b/src/parser/parser.rs @@ -136,7 +136,7 @@ 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), ))(i) } @@ -357,6 +357,33 @@ where } } +fn partial<'a, F>( + open_matcher: &'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(( + alt((map(key, String::from), quoted_string)), + opt(preceded(space1, separated_list(space1, key_value_pair))), + )), + tag("/}"), + )(i)?; + + Ok(( + i, + constructor(ParameterizedBlock { + name: name, + params: params.unwrap_or(Vec::new()), + }), + )) + } +} + fn filter(i: &str) -> IResult<&str, Filter> { preceded( tag("|"), From 15c732b3a3d6f3d5e53e1e49806c1f3ba280de85 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Tue, 7 Apr 2020 20:37:15 -0400 Subject: [PATCH 2/3] Also stealing the ParameterizedBlock struct --- src/parser/parser.rs | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/parser/parser.rs b/src/parser/parser.rs index 3233178..98c5ec9 100644 --- a/src/parser/parser.rs +++ b/src/parser/parser.rs @@ -32,7 +32,7 @@ enum DustTag<'a> { DTNotExists(Container<'a>), DTBlock(NamedBlock<'a>), DTInlinePartial(NamedBlock<'a>), - DTPartial(ParameterizedBlock<'a>), + DTPartial(Partial<'a>), } #[derive(Clone, Debug, PartialEq)] @@ -91,6 +91,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>, } @@ -332,6 +340,7 @@ where 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 @@ -341,7 +350,7 @@ where let (i, (name, params)) = delimited( tag(open_matcher), tuple(( - alt((map(key, String::from), quoted_string)), + tag(tag_name), opt(preceded(space1, separated_list(space1, key_value_pair))), )), tag("/}"), @@ -352,6 +361,8 @@ where constructor(ParameterizedBlock { name: name, params: params.unwrap_or(Vec::new()), + contents: None, + else_contents: None, }), )) } @@ -362,7 +373,7 @@ fn partial<'a, F>( constructor: F, ) -> impl Fn(&'a str) -> IResult<&'a str, DustTag<'a>> where - F: Fn(ParameterizedBlock<'a>) -> DustTag<'a>, + F: Fn(Partial<'a>) -> DustTag<'a>, { move |i: &'a str| { let (i, (name, params)) = delimited( @@ -376,7 +387,7 @@ where Ok(( i, - constructor(ParameterizedBlock { + constructor(Partial { name: name, params: params.unwrap_or(Vec::new()), }), @@ -721,7 +732,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 { @@ -744,7 +755,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 { From 05d9e20a8fad262ed108f14f91bccae301ed6cfd Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Tue, 7 Apr 2020 21:49:28 -0400 Subject: [PATCH 3/3] Add all the helpers --- src/parser/parser.rs | 120 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) diff --git a/src/parser/parser.rs b/src/parser/parser.rs index 98c5ec9..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; @@ -33,6 +34,12 @@ enum DustTag<'a> { DTBlock(NamedBlock<'a>), DTInlinePartial(NamedBlock<'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)] @@ -145,6 +152,12 @@ fn dust_tag(i: &str) -> IResult<&str, DustTag> { named_block("{+", DustTag::DTBlock), named_block("{<", DustTag::DTInlinePartial), 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) } @@ -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>( open_matcher: &'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 + }) + )) + ); + } }