diff --git a/src/duster/parser.rs b/src/duster/parser.rs index b0c8b4d..d49a9c5 100644 --- a/src/duster/parser.rs +++ b/src/duster/parser.rs @@ -88,12 +88,21 @@ enum TemplateElement<'a> { TETag(DustTag<'a>), } +impl<'a> Section<'a> { + fn new(path: Path<'a>, contents: std::option::Option>) -> DustTag<'a> { + DustTag::DTSection(Section { + path: path, + contents: contents, + }) + } +} + fn dust_tag(i: &str) -> IResult<&str, DustTag> { alt(( map(special, DustTag::DTSpecial), map(comment, DustTag::DTComment), map(reference, DustTag::DTReference), - map(section, DustTag::DTSection), + container("{#", Section::new), ))(i) } @@ -143,6 +152,27 @@ fn reference(i: &str) -> IResult<&str, Reference> { )) } +fn container<'a, F>( + open_matcher: &'static str, + foo: F, +) -> impl Fn(&'a str) -> IResult<&'a str, DustTag<'a>> +where + F: Fn(Path<'a>, Option>) -> DustTag<'a>, +{ + move |i: &'a str| { + let (i, (opening_name, inner, _closing_name)) = verify( + tuple(( + delimited(tag(open_matcher), path, tag("}")), + opt(block), + delimited(tag("{/"), path, tag("}")), + )), + |(open, _inn, close)| open == close, + )(i)?; + + Ok((i, foo(opening_name, inner))) + } +} + fn section(i: &str) -> IResult<&str, Section> { let (i, (opening_name, inner, _closing_name)) = verify( tuple((