Switch the parser over to a new partial implementation for dynamic partials.
This commit is contained in:
parent
52ba4fd0a3
commit
402d8679e2
@ -34,6 +34,7 @@ pub enum DustTag<'a> {
|
|||||||
DTBlock(NamedBlock<'a>),
|
DTBlock(NamedBlock<'a>),
|
||||||
DTInlinePartial(NamedBlock<'a>),
|
DTInlinePartial(NamedBlock<'a>),
|
||||||
DTPartial(Partial<'a>),
|
DTPartial(Partial<'a>),
|
||||||
|
DTNewPartial(NewPartial<'a>),
|
||||||
DTHelperEquals(ParameterizedBlock<'a>),
|
DTHelperEquals(ParameterizedBlock<'a>),
|
||||||
DTHelperNotEquals(ParameterizedBlock<'a>),
|
DTHelperNotEquals(ParameterizedBlock<'a>),
|
||||||
DTHelperGreaterThan(ParameterizedBlock<'a>),
|
DTHelperGreaterThan(ParameterizedBlock<'a>),
|
||||||
@ -112,6 +113,12 @@ pub struct ParameterizedBlock<'a> {
|
|||||||
pub else_contents: Option<Body<'a>>,
|
pub else_contents: Option<Body<'a>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
|
pub struct NewPartial<'a> {
|
||||||
|
pub name: Vec<PartialNameElement>,
|
||||||
|
pub params: Vec<KVPair<'a>>,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
pub struct Partial<'a> {
|
pub struct Partial<'a> {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
@ -136,6 +143,17 @@ pub struct KVPair<'a> {
|
|||||||
pub value: RValue<'a>,
|
pub value: RValue<'a>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
|
pub enum PartialNameElement {
|
||||||
|
PNSpan {
|
||||||
|
contents: String,
|
||||||
|
},
|
||||||
|
PNReference {
|
||||||
|
path: Vec<String>,
|
||||||
|
filters: Vec<Filter>,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
pub struct Body<'a> {
|
pub struct Body<'a> {
|
||||||
pub elements: Vec<TemplateElement<'a>>,
|
pub elements: Vec<TemplateElement<'a>>,
|
||||||
@ -153,6 +171,28 @@ pub enum TemplateElement<'a> {
|
|||||||
TEIgnoredWhitespace(IgnoredWhitespace<'a>),
|
TEIgnoredWhitespace(IgnoredWhitespace<'a>),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<TemplateElement<'_>> for PartialNameElement {
|
||||||
|
fn from(original: TemplateElement) -> Self {
|
||||||
|
match original {
|
||||||
|
TemplateElement::TESpan(span) => PartialNameElement::PNSpan {
|
||||||
|
contents: span.contents.to_owned(),
|
||||||
|
},
|
||||||
|
TemplateElement::TETag(DustTag::DTReference(reference)) => {
|
||||||
|
PartialNameElement::PNReference {
|
||||||
|
path: reference
|
||||||
|
.path
|
||||||
|
.keys
|
||||||
|
.into_iter()
|
||||||
|
.map(|s| s.to_owned())
|
||||||
|
.collect(),
|
||||||
|
filters: reference.filters,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => panic!("Only spans and references can be used in partial names."),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Any element significant to dust that isn't plain text
|
/// Any element significant to dust that isn't plain text
|
||||||
///
|
///
|
||||||
/// These elements are always wrapped in curly braces
|
/// These elements are always wrapped in curly braces
|
||||||
@ -166,7 +206,7 @@ fn dust_tag(i: &str) -> IResult<&str, DustTag> {
|
|||||||
conditional("{^", DustTag::DTNotExists),
|
conditional("{^", DustTag::DTNotExists),
|
||||||
named_block("{+", DustTag::DTBlock),
|
named_block("{+", DustTag::DTBlock),
|
||||||
named_block("{<", DustTag::DTInlinePartial),
|
named_block("{<", DustTag::DTInlinePartial),
|
||||||
partial("{>", DustTag::DTPartial),
|
new_partial("{>", DustTag::DTNewPartial),
|
||||||
parameterized_block("{@", "gte", DustTag::DTHelperGreaterThanOrEquals),
|
parameterized_block("{@", "gte", DustTag::DTHelperGreaterThanOrEquals),
|
||||||
parameterized_block("{@", "lte", DustTag::DTHelperLessThanOrEquals),
|
parameterized_block("{@", "lte", DustTag::DTHelperLessThanOrEquals),
|
||||||
parameterized_block("{@", "eq", DustTag::DTHelperEquals),
|
parameterized_block("{@", "eq", DustTag::DTHelperEquals),
|
||||||
@ -467,6 +507,91 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn new_partial_with_plain_tag<'a>(
|
||||||
|
open_matcher: &'static str,
|
||||||
|
) -> impl Fn(&'a str) -> IResult<&'a str, NewPartial<'a>> {
|
||||||
|
move |i: &'a str| {
|
||||||
|
let (i, (name, params)) = delimited(
|
||||||
|
tag(open_matcher),
|
||||||
|
tuple((
|
||||||
|
key,
|
||||||
|
opt(delimited(
|
||||||
|
space1,
|
||||||
|
separated_list1(space1, key_value_pair),
|
||||||
|
space0,
|
||||||
|
)),
|
||||||
|
)),
|
||||||
|
tag("/}"),
|
||||||
|
)(i)?;
|
||||||
|
|
||||||
|
Ok((
|
||||||
|
i,
|
||||||
|
NewPartial {
|
||||||
|
name: vec![PartialNameElement::PNSpan {
|
||||||
|
contents: name.to_owned(),
|
||||||
|
}],
|
||||||
|
params: params.unwrap_or(Vec::new()),
|
||||||
|
},
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn new_partial_quoted_tag(i: &str) -> IResult<&str, Vec<TemplateElement>> {
|
||||||
|
// TODO: make all consuming
|
||||||
|
many1(alt((
|
||||||
|
map(span, TemplateElement::TESpan),
|
||||||
|
map(map(reference, DustTag::DTReference), TemplateElement::TETag),
|
||||||
|
)))(i)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn new_partial_with_quoted_tag<'a>(
|
||||||
|
open_matcher: &'static str,
|
||||||
|
) -> impl Fn(&'a str) -> IResult<&'a str, NewPartial<'a>> {
|
||||||
|
move |i: &'a str| {
|
||||||
|
let (i, (name, params)) = delimited(
|
||||||
|
tag(open_matcher),
|
||||||
|
tuple((
|
||||||
|
verify(quoted_string, |s: &String| {
|
||||||
|
new_partial_quoted_tag(s.as_str()).is_ok()
|
||||||
|
}),
|
||||||
|
opt(delimited(
|
||||||
|
space1,
|
||||||
|
separated_list1(space1, key_value_pair),
|
||||||
|
space0,
|
||||||
|
)),
|
||||||
|
)),
|
||||||
|
tag("/}"),
|
||||||
|
)(i)?;
|
||||||
|
|
||||||
|
let (_remaining, template_name_elements) = new_partial_quoted_tag(name.as_str())
|
||||||
|
.expect("A successful parse was verified earlier with a call to verify()");
|
||||||
|
let partial_name_elements = template_name_elements
|
||||||
|
.into_iter()
|
||||||
|
.map(|e| e.into())
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
Ok((
|
||||||
|
i,
|
||||||
|
NewPartial {
|
||||||
|
name: partial_name_elements,
|
||||||
|
params: params.unwrap_or(Vec::new()),
|
||||||
|
},
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn new_partial<'a, F>(
|
||||||
|
open_matcher: &'static str,
|
||||||
|
constructor: F,
|
||||||
|
) -> impl Fn(&'a str) -> IResult<&'a str, DustTag<'a>>
|
||||||
|
where
|
||||||
|
F: Fn(NewPartial<'a>) -> DustTag<'a>,
|
||||||
|
{
|
||||||
|
let plain = new_partial_with_plain_tag(open_matcher);
|
||||||
|
let quoted = new_partial_with_quoted_tag(open_matcher);
|
||||||
|
move |i: &'a str| map(alt((&plain, "ed)), &constructor)(i)
|
||||||
|
}
|
||||||
|
|
||||||
fn partial<'a, F>(
|
fn partial<'a, F>(
|
||||||
open_matcher: &'static str,
|
open_matcher: &'static str,
|
||||||
constructor: F,
|
constructor: F,
|
||||||
@ -911,8 +1036,11 @@ mod tests {
|
|||||||
dust_tag(r#"{>foo bar=baz animal="cat"/}"#),
|
dust_tag(r#"{>foo bar=baz animal="cat"/}"#),
|
||||||
Ok((
|
Ok((
|
||||||
"",
|
"",
|
||||||
DustTag::DTPartial(Partial {
|
DustTag::DTNewPartial(NewPartial {
|
||||||
name: "foo".to_owned(),
|
name: vec![PartialNameElement::PNSpan {
|
||||||
|
contents: "foo".to_owned()
|
||||||
|
},],
|
||||||
|
|
||||||
params: vec![
|
params: vec![
|
||||||
KVPair {
|
KVPair {
|
||||||
key: "bar",
|
key: "bar",
|
||||||
@ -934,8 +1062,44 @@ mod tests {
|
|||||||
dust_tag(r#"{>"template name * with * special \" characters" bar=baz animal="cat"/}"#),
|
dust_tag(r#"{>"template name * with * special \" characters" bar=baz animal="cat"/}"#),
|
||||||
Ok((
|
Ok((
|
||||||
"",
|
"",
|
||||||
DustTag::DTPartial(Partial {
|
DustTag::DTNewPartial(NewPartial {
|
||||||
name: r#"template name * with * special " characters"#.to_owned(),
|
name: vec![PartialNameElement::PNSpan {
|
||||||
|
contents: r#"template name * with * special " characters"#.to_owned()
|
||||||
|
},],
|
||||||
|
params: vec![
|
||||||
|
KVPair {
|
||||||
|
key: "bar",
|
||||||
|
value: RValue::RVPath(Path { keys: vec!["baz"] })
|
||||||
|
},
|
||||||
|
KVPair {
|
||||||
|
key: "animal",
|
||||||
|
value: RValue::RVLiteral(OwnedLiteral::LString("cat".to_owned()))
|
||||||
|
}
|
||||||
|
]
|
||||||
|
})
|
||||||
|
))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_dynamic_partial() {
|
||||||
|
assert_eq!(
|
||||||
|
dust_tag(r#"{>"dynamic{ref}template" bar=baz animal="cat"/}"#),
|
||||||
|
Ok((
|
||||||
|
"",
|
||||||
|
DustTag::DTNewPartial(NewPartial {
|
||||||
|
name: vec![
|
||||||
|
PartialNameElement::PNSpan {
|
||||||
|
contents: "dynamic".to_owned()
|
||||||
|
},
|
||||||
|
PartialNameElement::PNReference {
|
||||||
|
path: vec!["ref".to_owned()],
|
||||||
|
filters: Vec::new()
|
||||||
|
},
|
||||||
|
PartialNameElement::PNSpan {
|
||||||
|
contents: "template".to_owned()
|
||||||
|
}
|
||||||
|
],
|
||||||
params: vec![
|
params: vec![
|
||||||
KVPair {
|
KVPair {
|
||||||
key: "bar",
|
key: "bar",
|
||||||
@ -957,8 +1121,10 @@ mod tests {
|
|||||||
dust_tag(r#"{>foo a="foo" b=179/}"#),
|
dust_tag(r#"{>foo a="foo" b=179/}"#),
|
||||||
Ok((
|
Ok((
|
||||||
"",
|
"",
|
||||||
DustTag::DTPartial(Partial {
|
DustTag::DTNewPartial(NewPartial {
|
||||||
name: "foo".to_owned(),
|
name: vec![PartialNameElement::PNSpan {
|
||||||
|
contents: "foo".to_owned()
|
||||||
|
},],
|
||||||
params: vec![
|
params: vec![
|
||||||
KVPair {
|
KVPair {
|
||||||
key: "a",
|
key: "a",
|
||||||
@ -1125,9 +1291,11 @@ mod tests {
|
|||||||
keys: vec!["level3", "level4"]
|
keys: vec!["level3", "level4"]
|
||||||
},
|
},
|
||||||
contents: Some(Body {
|
contents: Some(Body {
|
||||||
elements: vec![TemplateElement::TETag(DustTag::DTPartial(
|
elements: vec![TemplateElement::TETag(DustTag::DTNewPartial(
|
||||||
Partial {
|
NewPartial {
|
||||||
name: "partialtwo".to_owned(),
|
name: vec![PartialNameElement::PNSpan {
|
||||||
|
contents: "partialtwo".to_owned()
|
||||||
|
},],
|
||||||
params: vec![
|
params: vec![
|
||||||
KVPair {
|
KVPair {
|
||||||
key: "v1",
|
key: "v1",
|
||||||
|
@ -95,6 +95,7 @@ fn extract_inline_partials_from_tag<'a, 'b>(
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
DustTag::DTPartial(..) => (),
|
DustTag::DTPartial(..) => (),
|
||||||
|
DustTag::DTNewPartial(..) => (),
|
||||||
DustTag::DTInlinePartial(named_block) => {
|
DustTag::DTInlinePartial(named_block) => {
|
||||||
blocks.insert(&named_block.name, &named_block.contents);
|
blocks.insert(&named_block.name, &named_block.contents);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user