From 15c732b3a3d6f3d5e53e1e49806c1f3ba280de85 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Tue, 7 Apr 2020 20:37:15 -0400 Subject: [PATCH] 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 {