Beginning of creating a generic Container parser

This commit is contained in:
Tom Alexander 2020-04-05 16:53:15 -04:00
parent 7d51e500d8
commit 5f297eca78
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE

View File

@ -88,12 +88,21 @@ enum TemplateElement<'a> {
TETag(DustTag<'a>),
}
impl<'a> Section<'a> {
fn new(path: Path<'a>, contents: std::option::Option<Block<'a>>) -> 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<Block<'a>>) -> 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((