Finish adding explicit context to the parser for Container.

master
Tom Alexander 4 years ago
parent 1152ff9974
commit dbee569931
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE

@ -95,6 +95,7 @@ pub struct Span<'a> {
#[derive(Clone, Debug, PartialEq)]
pub struct Container<'a> {
pub path: Path<'a>,
pub explicit_context: Option<Path<'a>>,
pub contents: Option<Body<'a>>,
pub else_contents: Option<Body<'a>>,
}
@ -357,6 +358,7 @@ where
i,
constructor(Container {
path: opening_name,
explicit_context: maybe_explicit_context,
contents: inner,
else_contents: maybe_else.flatten(),
}),
@ -372,12 +374,16 @@ where
F: Fn(Container<'a>) -> DustTag<'a>,
{
move |i: &'a str| {
let (i, path) = delimited(tag(open_matcher), path, tag("/}"))(i)?;
let (i, (opening_name, maybe_explicit_context)) = tuple((
preceded(tag(open_matcher), path),
terminated(opt(preceded(tag(":"), path)), tag("/}")),
))(i)?;
Ok((
i,
constructor(Container {
path: path,
path: opening_name,
explicit_context: maybe_explicit_context,
contents: None,
else_contents: None,
}),
@ -874,6 +880,7 @@ mod tests {
path: Path {
keys: vec!["foo", "bar"]
},
explicit_context: None,
contents: None,
else_contents: None,
})
@ -891,6 +898,7 @@ mod tests {
path: Path {
keys: vec!["foo", "bar"]
},
explicit_context: None,
contents: None,
else_contents: None,
})
@ -908,6 +916,7 @@ mod tests {
path: Path {
keys: vec!["foo", "bar"]
},
explicit_context: None,
contents: Some(Body {
elements: vec![
TemplateElement::TESpan(Span { contents: "hello " }),
@ -933,6 +942,7 @@ mod tests {
path: Path {
keys: vec!["greeting"]
},
explicit_context: None,
contents: Some(Body {
elements: vec![
TemplateElement::TESpan(Span { contents: "hello " }),
@ -958,6 +968,44 @@ mod tests {
);
}
#[test]
fn test_empty_section_with_explicit_context() {
assert_eq!(
super::dust_tag("{#foo.bar:baz.ipsum}{/foo.bar}"),
Ok((
"",
DustTag::DTSection(Container {
path: Path {
keys: vec!["foo", "bar"]
},
explicit_context: Some(Path {
keys: vec!["baz", "ipsum"]
}),
contents: None,
else_contents: None,
})
))
);
}
#[test]
fn test_self_closing_section_with_explicit_context() {
assert_eq!(
super::dust_tag("{#foo.bar:$idx/}"),
Ok((
"",
DustTag::DTSection(Container {
path: Path {
keys: vec!["foo", "bar"]
},
explicit_context: Some(Path { keys: vec!["$idx"] }),
contents: None,
else_contents: None,
})
))
);
}
#[test]
fn test_self_closing_block() {
assert_eq!(
@ -1236,6 +1284,7 @@ mod tests {
path: Path {
keys: vec!["names"]
},
explicit_context: None,
contents: Some(Body {
elements: vec![TemplateElement::TETag(DustTag::DTReference(
Reference {
@ -1261,6 +1310,7 @@ mod tests {
path: Path {
keys: vec!["names"]
},
explicit_context: None,
contents: Some(Body {
elements: vec![
TemplateElement::TEIgnoredWhitespace(
@ -1298,6 +1348,7 @@ mod tests {
path: Path {
keys: vec!["level3", "level4"]
},
explicit_context: None,
contents: Some(Body {
elements: vec![TemplateElement::TETag(DustTag::DTPartial(
Partial {

Loading…
Cancel
Save