Get rid of the concept of NamedBlock and replace it with a ParameterizedBlock special case that does not support an else block.

master
Tom Alexander 4 years ago
parent eb0eb8d4ca
commit add77cf6a5
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE

@ -32,8 +32,8 @@ pub enum DustTag<'a> {
DTSection(ParameterizedBlock<'a>),
DTExists(ParameterizedBlock<'a>),
DTNotExists(ParameterizedBlock<'a>),
DTBlock(NamedBlock<'a>),
DTInlinePartial(NamedBlock<'a>),
DTBlock(ParameterizedBlock<'a>),
DTInlinePartial(ParameterizedBlock<'a>),
DTPartial(Partial<'a>),
DTHelperEquals(ParameterizedBlock<'a>),
DTHelperNotEquals(ParameterizedBlock<'a>),
@ -92,13 +92,6 @@ pub struct Span<'a> {
pub contents: &'a str,
}
#[derive(Clone, Debug, PartialEq)]
pub struct NamedBlock<'a> {
pub name: &'a str,
pub explicit_context: Option<Path<'a>>,
pub contents: Option<Body<'a>>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct ParameterizedBlock<'a> {
pub path: Path<'a>,
@ -213,8 +206,14 @@ fn dust_tag(i: &str) -> IResult<&str, DustTag> {
map(parameterized_block("{#", path), DustTag::DTSection),
map(parameterized_block("{?", path), DustTag::DTExists),
map(parameterized_block("{^", path), DustTag::DTNotExists),
named_block("{+", DustTag::DTBlock),
named_block("{<", DustTag::DTInlinePartial),
map(
parameterized_block_without_else("{+", key_to_path),
DustTag::DTBlock,
),
map(
parameterized_block_without_else("{<", key_to_path),
DustTag::DTInlinePartial,
),
partial("{>", DustTag::DTPartial),
map(
parameterized_block("{@", &tag_to_path("gte")),
@ -342,86 +341,74 @@ fn reference(i: &str) -> IResult<&str, Reference> {
))
}
fn named_block<'a, F>(
fn parameterized_block<'a, F>(
open_matcher: &'static str,
constructor: F,
) -> impl FnMut(&'a str) -> IResult<&'a str, DustTag<'a>>
name_matcher: F,
) -> impl FnMut(&'a str) -> IResult<&'a str, ParameterizedBlock<'a>>
where
F: Copy + Fn(NamedBlock<'a>) -> DustTag<'a>,
F: Copy + Fn(&'a str) -> IResult<&'a str, Path<'a>>,
{
alt((
named_block_with_body(open_matcher, constructor),
self_closing_named_block(open_matcher, constructor),
parameterized_block_with_body(open_matcher, name_matcher),
parameterized_self_closing_block(open_matcher, name_matcher),
))
}
fn named_block_with_body<'a, F>(
fn parameterized_block_without_else<'a, F>(
open_matcher: &'static str,
constructor: F,
) -> impl Fn(&'a str) -> IResult<&'a str, DustTag<'a>>
name_matcher: F,
) -> impl FnMut(&'a str) -> IResult<&'a str, ParameterizedBlock<'a>>
where
F: Fn(NamedBlock<'a>) -> DustTag<'a>,
F: Copy + Fn(&'a str) -> IResult<&'a str, Path<'a>>,
{
move |i: &'a str| {
let (i, (opening_name, maybe_explicit_context, inner, _closing_name)) = verify(
tuple((
preceded(tag(open_matcher), key),
terminated(opt(preceded(tag(":"), path)), tag("}")),
opt(body),
delimited(tag("{/"), key, tag("}")),
)),
|(open, _maybe_explicit, _inn, close)| open == close,
)(i)?;
Ok((
i,
constructor(NamedBlock {
name: opening_name,
explicit_context: maybe_explicit_context,
contents: inner,
}),
))
}
alt((
parameterized_block_with_body_without_else(open_matcher, name_matcher),
parameterized_self_closing_block(open_matcher, name_matcher),
))
}
fn self_closing_named_block<'a, F>(
fn parameterized_block_with_body<'a, F>(
open_matcher: &'static str,
constructor: F,
) -> impl Fn(&'a str) -> IResult<&'a str, DustTag<'a>>
name_matcher: F,
) -> impl Fn(&'a str) -> IResult<&'a str, ParameterizedBlock<'a>>
where
F: Fn(NamedBlock<'a>) -> DustTag<'a>,
F: Copy + Fn(&'a str) -> IResult<&'a str, Path<'a>>,
{
move |i: &'a str| {
let (i, (opening_name, maybe_explicit_context)) = tuple((
preceded(tag(open_matcher), key),
terminated(opt(preceded(tag(":"), path)), tag("/}")),
))(i)?;
let (i, (opening_name, maybe_explicit_context, params, inner, maybe_else, _closing_name)) =
verify(
tuple((
preceded(tag(open_matcher), name_matcher),
opt(preceded(tag(":"), path)),
terminated(
opt(delimited(
space1,
separated_list1(space1, key_value_pair),
space0,
)),
tag("}"),
),
opt(body),
opt(preceded(tag("{:else}"), opt(body))),
delimited(tag("{/"), name_matcher, tag("}")),
)),
|(open, _maybe_explicit, _params, _inn, _maybe_else, close)| open == close,
)(i)?;
Ok((
i,
constructor(NamedBlock {
name: opening_name,
ParameterizedBlock {
path: opening_name,
explicit_context: maybe_explicit_context,
contents: None,
}),
params: params.unwrap_or(Vec::new()),
contents: inner,
else_contents: maybe_else.flatten(),
},
))
}
}
fn parameterized_block<'a, F>(
open_matcher: &'static str,
name_matcher: F,
) -> impl FnMut(&'a str) -> IResult<&'a str, ParameterizedBlock<'a>>
where
F: Copy + Fn(&'a str) -> IResult<&'a str, Path<'a>>,
{
alt((
parameterized_block_with_body(open_matcher, name_matcher),
parameterized_self_closing_block(open_matcher, name_matcher),
))
}
fn parameterized_block_with_body<'a, F>(
fn parameterized_block_with_body_without_else<'a, F>(
open_matcher: &'static str,
name_matcher: F,
) -> impl Fn(&'a str) -> IResult<&'a str, ParameterizedBlock<'a>>
@ -429,7 +416,7 @@ where
F: Copy + Fn(&'a str) -> IResult<&'a str, Path<'a>>,
{
move |i: &'a str| {
let (i, (opening_name, maybe_explicit_context, params, inner, maybe_else, _closing_name)) =
let (i, (opening_name, maybe_explicit_context, params, inner, _closing_name)) =
verify(
tuple((
preceded(tag(open_matcher), name_matcher),
@ -443,10 +430,9 @@ where
tag("}"),
),
opt(body),
opt(preceded(tag("{:else}"), opt(body))),
delimited(tag("{/"), name_matcher, tag("}")),
)),
|(open, _maybe_explicit, _params, _inn, _maybe_else, close)| open == close,
|(open, _maybe_explicit, _params, _inn, close)| open == close,
)(i)?;
Ok((
@ -456,7 +442,7 @@ where
explicit_context: maybe_explicit_context,
params: params.unwrap_or(Vec::new()),
contents: inner,
else_contents: maybe_else.flatten(),
else_contents: None,
},
))
}
@ -984,10 +970,12 @@ mod tests {
super::dust_tag("{+foo/}"),
Ok((
"",
DustTag::DTBlock(NamedBlock {
name: "foo",
DustTag::DTBlock(ParameterizedBlock {
path: Path { keys: vec!["foo"] },
explicit_context: None,
contents: None
params: Vec::new(),
contents: None,
else_contents: None
})
))
);
@ -999,9 +987,10 @@ mod tests {
super::dust_tag("{+foo}hello {name}{/foo}"),
Ok((
"",
DustTag::DTBlock(NamedBlock {
name: "foo",
DustTag::DTBlock(ParameterizedBlock {
path: Path { keys: vec!["foo"] },
explicit_context: None,
params: Vec::new(),
contents: Some(Body {
elements: vec![
TemplateElement::TESpan(Span { contents: "hello " }),
@ -1010,7 +999,8 @@ mod tests {
filters: Vec::new()
}))
]
})
}),
else_contents: None
})
))
);
@ -1022,12 +1012,14 @@ mod tests {
super::dust_tag("{+foo:bar.baz/}"),
Ok((
"",
DustTag::DTBlock(NamedBlock {
name: "foo",
DustTag::DTBlock(ParameterizedBlock {
path: Path { keys: vec!["foo"] },
explicit_context: Some(Path {
keys: vec!["bar", "baz"]
}),
contents: None
params: Vec::new(),
contents: None,
else_contents: None
})
))
);
@ -1039,11 +1031,12 @@ mod tests {
super::dust_tag("{+foo:bar.baz}hello {name}{/foo}"),
Ok((
"",
DustTag::DTBlock(NamedBlock {
name: "foo",
DustTag::DTBlock(ParameterizedBlock {
path: Path { keys: vec!["foo"] },
explicit_context: Some(Path {
keys: vec!["bar", "baz"]
}),
params: Vec::new(),
contents: Some(Body {
elements: vec![
TemplateElement::TESpan(Span { contents: "hello " }),
@ -1052,7 +1045,8 @@ mod tests {
filters: Vec::new()
}))
]
})
}),
else_contents: None
})
))
);
@ -1064,10 +1058,12 @@ mod tests {
super::dust_tag("{<foo/}"),
Ok((
"",
DustTag::DTInlinePartial(NamedBlock {
name: "foo",
DustTag::DTInlinePartial(ParameterizedBlock {
path: Path { keys: vec!["foo"] },
explicit_context: None,
contents: None
params: Vec::new(),
contents: None,
else_contents: None
})
))
);
@ -1079,9 +1075,10 @@ mod tests {
super::dust_tag("{<foo}hello {name}{/foo}"),
Ok((
"",
DustTag::DTInlinePartial(NamedBlock {
name: "foo",
DustTag::DTInlinePartial(ParameterizedBlock {
path: Path { keys: vec!["foo"] },
explicit_context: None,
params: Vec::new(),
contents: Some(Body {
elements: vec![
TemplateElement::TESpan(Span { contents: "hello " }),
@ -1090,7 +1087,8 @@ mod tests {
filters: Vec::new()
}))
]
})
}),
else_contents: None
})
))
);

@ -97,7 +97,7 @@ fn extract_inline_partials_from_tag<'a, 'b>(
}
DustTag::DTPartial(..) => (),
DustTag::DTInlinePartial(named_block) => {
blocks.insert(&named_block.name, &named_block.contents);
blocks.insert(&named_block.path.keys[0], &named_block.contents);
}
DustTag::DTBlock(..) => (),
DustTag::DTHelperEquals(parameterized_block) => {

@ -347,7 +347,7 @@ impl<'a> DustRenderer<'a> {
None,
&named_block.explicit_context,
);
return match blocks.blocks.get_block(named_block.name) {
return match blocks.blocks.get_block(named_block.path.keys[0]) {
None => self.render_maybe_body(
&named_block.contents,
new_breadcrumbs.as_ref().unwrap_or(breadcrumbs),

Loading…
Cancel
Save