Finish adding explicit context to the parser for Container.
This commit is contained in:
parent
1152ff9974
commit
dbee569931
@ -95,6 +95,7 @@ pub struct Span<'a> {
|
|||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
pub struct Container<'a> {
|
pub struct Container<'a> {
|
||||||
pub path: Path<'a>,
|
pub path: Path<'a>,
|
||||||
|
pub explicit_context: Option<Path<'a>>,
|
||||||
pub contents: Option<Body<'a>>,
|
pub contents: Option<Body<'a>>,
|
||||||
pub else_contents: Option<Body<'a>>,
|
pub else_contents: Option<Body<'a>>,
|
||||||
}
|
}
|
||||||
@ -357,6 +358,7 @@ where
|
|||||||
i,
|
i,
|
||||||
constructor(Container {
|
constructor(Container {
|
||||||
path: opening_name,
|
path: opening_name,
|
||||||
|
explicit_context: maybe_explicit_context,
|
||||||
contents: inner,
|
contents: inner,
|
||||||
else_contents: maybe_else.flatten(),
|
else_contents: maybe_else.flatten(),
|
||||||
}),
|
}),
|
||||||
@ -372,12 +374,16 @@ where
|
|||||||
F: Fn(Container<'a>) -> DustTag<'a>,
|
F: Fn(Container<'a>) -> DustTag<'a>,
|
||||||
{
|
{
|
||||||
move |i: &'a str| {
|
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((
|
Ok((
|
||||||
i,
|
i,
|
||||||
constructor(Container {
|
constructor(Container {
|
||||||
path: path,
|
path: opening_name,
|
||||||
|
explicit_context: maybe_explicit_context,
|
||||||
contents: None,
|
contents: None,
|
||||||
else_contents: None,
|
else_contents: None,
|
||||||
}),
|
}),
|
||||||
@ -874,6 +880,7 @@ mod tests {
|
|||||||
path: Path {
|
path: Path {
|
||||||
keys: vec!["foo", "bar"]
|
keys: vec!["foo", "bar"]
|
||||||
},
|
},
|
||||||
|
explicit_context: None,
|
||||||
contents: None,
|
contents: None,
|
||||||
else_contents: None,
|
else_contents: None,
|
||||||
})
|
})
|
||||||
@ -891,6 +898,7 @@ mod tests {
|
|||||||
path: Path {
|
path: Path {
|
||||||
keys: vec!["foo", "bar"]
|
keys: vec!["foo", "bar"]
|
||||||
},
|
},
|
||||||
|
explicit_context: None,
|
||||||
contents: None,
|
contents: None,
|
||||||
else_contents: None,
|
else_contents: None,
|
||||||
})
|
})
|
||||||
@ -908,6 +916,7 @@ mod tests {
|
|||||||
path: Path {
|
path: Path {
|
||||||
keys: vec!["foo", "bar"]
|
keys: vec!["foo", "bar"]
|
||||||
},
|
},
|
||||||
|
explicit_context: None,
|
||||||
contents: Some(Body {
|
contents: Some(Body {
|
||||||
elements: vec![
|
elements: vec![
|
||||||
TemplateElement::TESpan(Span { contents: "hello " }),
|
TemplateElement::TESpan(Span { contents: "hello " }),
|
||||||
@ -933,6 +942,7 @@ mod tests {
|
|||||||
path: Path {
|
path: Path {
|
||||||
keys: vec!["greeting"]
|
keys: vec!["greeting"]
|
||||||
},
|
},
|
||||||
|
explicit_context: None,
|
||||||
contents: Some(Body {
|
contents: Some(Body {
|
||||||
elements: vec![
|
elements: vec![
|
||||||
TemplateElement::TESpan(Span { contents: "hello " }),
|
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]
|
#[test]
|
||||||
fn test_self_closing_block() {
|
fn test_self_closing_block() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
@ -1236,6 +1284,7 @@ mod tests {
|
|||||||
path: Path {
|
path: Path {
|
||||||
keys: vec!["names"]
|
keys: vec!["names"]
|
||||||
},
|
},
|
||||||
|
explicit_context: None,
|
||||||
contents: Some(Body {
|
contents: Some(Body {
|
||||||
elements: vec![TemplateElement::TETag(DustTag::DTReference(
|
elements: vec![TemplateElement::TETag(DustTag::DTReference(
|
||||||
Reference {
|
Reference {
|
||||||
@ -1261,6 +1310,7 @@ mod tests {
|
|||||||
path: Path {
|
path: Path {
|
||||||
keys: vec!["names"]
|
keys: vec!["names"]
|
||||||
},
|
},
|
||||||
|
explicit_context: None,
|
||||||
contents: Some(Body {
|
contents: Some(Body {
|
||||||
elements: vec![
|
elements: vec![
|
||||||
TemplateElement::TEIgnoredWhitespace(
|
TemplateElement::TEIgnoredWhitespace(
|
||||||
@ -1298,6 +1348,7 @@ mod tests {
|
|||||||
path: Path {
|
path: Path {
|
||||||
keys: vec!["level3", "level4"]
|
keys: vec!["level3", "level4"]
|
||||||
},
|
},
|
||||||
|
explicit_context: None,
|
||||||
contents: Some(Body {
|
contents: Some(Body {
|
||||||
elements: vec![TemplateElement::TETag(DustTag::DTPartial(
|
elements: vec![TemplateElement::TETag(DustTag::DTPartial(
|
||||||
Partial {
|
Partial {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user