Integrated exists and not exists containers
This commit is contained in:
parent
5f297eca78
commit
fff229df90
@ -22,7 +22,9 @@ enum DustTag<'a> {
|
|||||||
DTSpecial(Special),
|
DTSpecial(Special),
|
||||||
DTComment(Comment<'a>),
|
DTComment(Comment<'a>),
|
||||||
DTReference(Reference<'a>),
|
DTReference(Reference<'a>),
|
||||||
DTSection(Section<'a>),
|
DTSection(Container<'a>),
|
||||||
|
DTExists(Container<'a>),
|
||||||
|
DTNotExists(Container<'a>),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
@ -67,7 +69,7 @@ struct Span<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
struct Section<'a> {
|
struct Container<'a> {
|
||||||
path: Path<'a>,
|
path: Path<'a>,
|
||||||
contents: Option<Block<'a>>,
|
contents: Option<Block<'a>>,
|
||||||
}
|
}
|
||||||
@ -88,21 +90,14 @@ enum TemplateElement<'a> {
|
|||||||
TETag(DustTag<'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> {
|
fn dust_tag(i: &str) -> IResult<&str, DustTag> {
|
||||||
alt((
|
alt((
|
||||||
map(special, DustTag::DTSpecial),
|
map(special, DustTag::DTSpecial),
|
||||||
map(comment, DustTag::DTComment),
|
map(comment, DustTag::DTComment),
|
||||||
map(reference, DustTag::DTReference),
|
map(reference, DustTag::DTReference),
|
||||||
container("{#", Section::new),
|
container("{#", DustTag::DTSection),
|
||||||
|
container("{?", DustTag::DTExists),
|
||||||
|
container("{^", DustTag::DTNotExists),
|
||||||
))(i)
|
))(i)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -154,10 +149,10 @@ fn reference(i: &str) -> IResult<&str, Reference> {
|
|||||||
|
|
||||||
fn container<'a, F>(
|
fn container<'a, F>(
|
||||||
open_matcher: &'static str,
|
open_matcher: &'static str,
|
||||||
foo: F,
|
constructor: F,
|
||||||
) -> impl Fn(&'a str) -> IResult<&'a str, DustTag<'a>>
|
) -> impl Fn(&'a str) -> IResult<&'a str, DustTag<'a>>
|
||||||
where
|
where
|
||||||
F: Fn(Path<'a>, Option<Block<'a>>) -> DustTag<'a>,
|
F: Fn(Container<'a>) -> DustTag<'a>,
|
||||||
{
|
{
|
||||||
move |i: &'a str| {
|
move |i: &'a str| {
|
||||||
let (i, (opening_name, inner, _closing_name)) = verify(
|
let (i, (opening_name, inner, _closing_name)) = verify(
|
||||||
@ -169,27 +164,14 @@ where
|
|||||||
|(open, _inn, close)| open == close,
|
|(open, _inn, close)| open == close,
|
||||||
)(i)?;
|
)(i)?;
|
||||||
|
|
||||||
Ok((i, foo(opening_name, inner)))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn section(i: &str) -> IResult<&str, Section> {
|
|
||||||
let (i, (opening_name, inner, _closing_name)) = verify(
|
|
||||||
tuple((
|
|
||||||
delimited(tag("{#"), path, tag("}")),
|
|
||||||
opt(block),
|
|
||||||
delimited(tag("{/"), path, tag("}")),
|
|
||||||
)),
|
|
||||||
|(open, inn, close)| open == close,
|
|
||||||
)(i)?;
|
|
||||||
|
|
||||||
Ok((
|
Ok((
|
||||||
i,
|
i,
|
||||||
Section {
|
constructor(Container {
|
||||||
path: opening_name,
|
path: opening_name,
|
||||||
contents: inner,
|
contents: inner,
|
||||||
},
|
}),
|
||||||
))
|
))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn filter(i: &str) -> IResult<&str, Filter> {
|
fn filter(i: &str) -> IResult<&str, Filter> {
|
||||||
@ -336,23 +318,23 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_section_mismatched_paths() {
|
fn test_section_mismatched_paths() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
super::section("{#foo.bar}{/baz}"),
|
super::dust_tag("{#foo.bar}{/baz}"),
|
||||||
Err(Error(("{#foo.bar}{/baz}", ErrorKind::Verify)))
|
Err(Error(("{#foo.bar}{/baz}", ErrorKind::Tag)))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_empty_section() {
|
fn test_empty_section() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
super::section("{#foo.bar}{/foo.bar}"),
|
super::dust_tag("{#foo.bar}{/foo.bar}"),
|
||||||
Ok((
|
Ok((
|
||||||
"",
|
"",
|
||||||
Section {
|
DustTag::DTSection(Container {
|
||||||
path: Path {
|
path: Path {
|
||||||
keys: vec!["foo", "bar"]
|
keys: vec!["foo", "bar"]
|
||||||
},
|
},
|
||||||
contents: None
|
contents: None
|
||||||
}
|
})
|
||||||
))
|
))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -360,10 +342,10 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_section_with_body() {
|
fn test_section_with_body() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
super::section("{#foo.bar}hello {name}{/foo.bar}"),
|
super::dust_tag("{#foo.bar}hello {name}{/foo.bar}"),
|
||||||
Ok((
|
Ok((
|
||||||
"",
|
"",
|
||||||
Section {
|
DustTag::DTSection(Container {
|
||||||
path: Path {
|
path: Path {
|
||||||
keys: vec!["foo", "bar"]
|
keys: vec!["foo", "bar"]
|
||||||
},
|
},
|
||||||
@ -376,7 +358,7 @@ mod tests {
|
|||||||
}))
|
}))
|
||||||
]
|
]
|
||||||
})
|
})
|
||||||
}
|
})
|
||||||
))
|
))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user