|
|
|
@ -114,7 +114,7 @@ pub struct ParameterizedBlock<'a> {
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
|
|
|
pub struct Partial<'a> {
|
|
|
|
|
pub name: String,
|
|
|
|
|
pub name: Vec<PartialNameElement>,
|
|
|
|
|
pub params: Vec<KVPair<'a>>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -136,6 +136,17 @@ pub struct KVPair<'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)]
|
|
|
|
|
pub struct Body<'a> {
|
|
|
|
|
pub elements: Vec<TemplateElement<'a>>,
|
|
|
|
@ -153,6 +164,46 @@ pub enum TemplateElement<'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."),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> From<&'a PartialNameElement> for TemplateElement<'a> {
|
|
|
|
|
fn from(original: &'a PartialNameElement) -> Self {
|
|
|
|
|
match original {
|
|
|
|
|
PartialNameElement::PNSpan { contents } => {
|
|
|
|
|
TemplateElement::TESpan(Span { contents: contents })
|
|
|
|
|
}
|
|
|
|
|
PartialNameElement::PNReference { path, filters } => {
|
|
|
|
|
TemplateElement::TETag(DustTag::DTReference(Reference {
|
|
|
|
|
path: Path {
|
|
|
|
|
keys: path.into_iter().map(|s| s.as_str()).collect(),
|
|
|
|
|
},
|
|
|
|
|
filters: filters.into_iter().map(|f| f.clone()).collect(),
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Any element significant to dust that isn't plain text
|
|
|
|
|
///
|
|
|
|
|
/// These elements are always wrapped in curly braces
|
|
|
|
@ -467,18 +518,14 @@ where
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn partial<'a, F>(
|
|
|
|
|
fn partial_with_plain_tag<'a>(
|
|
|
|
|
open_matcher: &'static str,
|
|
|
|
|
constructor: F,
|
|
|
|
|
) -> impl Fn(&'a str) -> IResult<&'a str, DustTag<'a>>
|
|
|
|
|
where
|
|
|
|
|
F: Fn(Partial<'a>) -> DustTag<'a>,
|
|
|
|
|
{
|
|
|
|
|
) -> impl Fn(&'a str) -> IResult<&'a str, Partial<'a>> {
|
|
|
|
|
move |i: &'a str| {
|
|
|
|
|
let (i, (name, params)) = delimited(
|
|
|
|
|
tag(open_matcher),
|
|
|
|
|
tuple((
|
|
|
|
|
alt((map(key, String::from), quoted_string)),
|
|
|
|
|
key,
|
|
|
|
|
opt(delimited(
|
|
|
|
|
space1,
|
|
|
|
|
separated_list1(space1, key_value_pair),
|
|
|
|
@ -490,14 +537,71 @@ where
|
|
|
|
|
|
|
|
|
|
Ok((
|
|
|
|
|
i,
|
|
|
|
|
constructor(Partial {
|
|
|
|
|
name: name,
|
|
|
|
|
Partial {
|
|
|
|
|
name: vec![PartialNameElement::PNSpan {
|
|
|
|
|
contents: name.to_owned(),
|
|
|
|
|
}],
|
|
|
|
|
params: params.unwrap_or(Vec::new()),
|
|
|
|
|
}),
|
|
|
|
|
},
|
|
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn partial_quoted_tag(i: &str) -> IResult<&str, Vec<TemplateElement>> {
|
|
|
|
|
all_consuming(many1(alt((
|
|
|
|
|
map(span, TemplateElement::TESpan),
|
|
|
|
|
map(map(reference, DustTag::DTReference), TemplateElement::TETag),
|
|
|
|
|
))))(i)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn partial_with_quoted_tag<'a>(
|
|
|
|
|
open_matcher: &'static str,
|
|
|
|
|
) -> impl Fn(&'a str) -> IResult<&'a str, Partial<'a>> {
|
|
|
|
|
move |i: &'a str| {
|
|
|
|
|
let (i, (name, params)) = delimited(
|
|
|
|
|
tag(open_matcher),
|
|
|
|
|
tuple((
|
|
|
|
|
verify(quoted_string, |s: &String| {
|
|
|
|
|
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) = 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,
|
|
|
|
|
Partial {
|
|
|
|
|
name: partial_name_elements,
|
|
|
|
|
params: params.unwrap_or(Vec::new()),
|
|
|
|
|
},
|
|
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn partial<'a, F>(
|
|
|
|
|
open_matcher: &'static str,
|
|
|
|
|
constructor: F,
|
|
|
|
|
) -> impl Fn(&'a str) -> IResult<&'a str, DustTag<'a>>
|
|
|
|
|
where
|
|
|
|
|
F: Fn(Partial<'a>) -> DustTag<'a>,
|
|
|
|
|
{
|
|
|
|
|
let plain = partial_with_plain_tag(open_matcher);
|
|
|
|
|
let quoted = partial_with_quoted_tag(open_matcher);
|
|
|
|
|
move |i: &'a str| map(alt((&plain, "ed)), &constructor)(i)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn filter(i: &str) -> IResult<&str, Filter> {
|
|
|
|
|
preceded(
|
|
|
|
|
tag("|"),
|
|
|
|
@ -912,7 +1016,10 @@ mod tests {
|
|
|
|
|
Ok((
|
|
|
|
|
"",
|
|
|
|
|
DustTag::DTPartial(Partial {
|
|
|
|
|
name: "foo".to_owned(),
|
|
|
|
|
name: vec![PartialNameElement::PNSpan {
|
|
|
|
|
contents: "foo".to_owned()
|
|
|
|
|
},],
|
|
|
|
|
|
|
|
|
|
params: vec![
|
|
|
|
|
KVPair {
|
|
|
|
|
key: "bar",
|
|
|
|
@ -935,7 +1042,43 @@ mod tests {
|
|
|
|
|
Ok((
|
|
|
|
|
"",
|
|
|
|
|
DustTag::DTPartial(Partial {
|
|
|
|
|
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::DTPartial(Partial {
|
|
|
|
|
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![
|
|
|
|
|
KVPair {
|
|
|
|
|
key: "bar",
|
|
|
|
@ -958,7 +1101,9 @@ mod tests {
|
|
|
|
|
Ok((
|
|
|
|
|
"",
|
|
|
|
|
DustTag::DTPartial(Partial {
|
|
|
|
|
name: "foo".to_owned(),
|
|
|
|
|
name: vec![PartialNameElement::PNSpan {
|
|
|
|
|
contents: "foo".to_owned()
|
|
|
|
|
},],
|
|
|
|
|
params: vec![
|
|
|
|
|
KVPair {
|
|
|
|
|
key: "a",
|
|
|
|
@ -1127,7 +1272,9 @@ mod tests {
|
|
|
|
|
contents: Some(Body {
|
|
|
|
|
elements: vec![TemplateElement::TETag(DustTag::DTPartial(
|
|
|
|
|
Partial {
|
|
|
|
|
name: "partialtwo".to_owned(),
|
|
|
|
|
name: vec![PartialNameElement::PNSpan {
|
|
|
|
|
contents: "partialtwo".to_owned()
|
|
|
|
|
},],
|
|
|
|
|
params: vec![
|
|
|
|
|
KVPair {
|
|
|
|
|
key: "v1",
|
|
|
|
|