Integrated the new span class

This commit is contained in:
Tom Alexander 2020-05-03 12:48:02 -04:00
parent b8e8f19724
commit f97bb25ca8
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
2 changed files with 33 additions and 63 deletions

View File

@ -85,11 +85,6 @@ pub enum Filter {
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub struct Span<'a> { pub struct Span<'a> {
pub contents: &'a str,
}
#[derive(Clone, Debug, PartialEq)]
pub struct NewSpan<'a> {
pub contents: Vec<&'a str>, pub contents: Vec<&'a str>,
} }
@ -500,24 +495,12 @@ fn span_line(i: &str) -> IResult<&str, &str> {
} }
/// Any text that is not a Dust element /// Any text that is not a Dust element
fn new_span(i: &str) -> IResult<&str, NewSpan> { fn span(i: &str) -> IResult<&str, Span> {
let (remaining, lines) = preceded( let (remaining, lines) = preceded(
opt(span_end_of_line), opt(span_end_of_line),
many1(terminated(span_line, opt(span_end_of_line))), many1(terminated(span_line, opt(span_end_of_line))),
)(i)?; )(i)?;
Ok((remaining, NewSpan { contents: lines })) Ok((remaining, Span { contents: lines }))
}
/// Any text that is not a Dust element
fn span(i: &str) -> IResult<&str, Span> {
let (remaining, body) = verify(
alt((
take_until("{"),
take_until_parser_matches(all_consuming(eof_whitespace)),
)),
|s: &str| s.len() > 0,
)(i)?;
Ok((remaining, Span { contents: body }))
} }
fn body(i: &str) -> IResult<&str, Body> { fn body(i: &str) -> IResult<&str, Body> {
@ -627,32 +610,6 @@ mod tests {
); );
} }
#[test]
fn test_span() {
assert_eq!(
super::span("this is just some text"),
Ok((
"",
Span {
contents: "this is just some text"
}
))
);
assert_eq!(
super::span("this is just some text {~lb}"),
Ok((
"{~lb}",
Span {
contents: "this is just some text "
}
))
);
assert_eq!(
super::span("{~lb}"),
Err(Error(("{~lb}", ErrorKind::Verify)))
);
}
#[test] #[test]
fn test_span_end_of_line() { fn test_span_end_of_line() {
assert_eq!( assert_eq!(
@ -678,43 +635,43 @@ mod tests {
} }
#[test] #[test]
fn test_new_span() { fn test_span() {
assert_eq!( assert_eq!(
super::new_span("this is just some text"), super::span("this is just some text"),
Ok(( Ok((
"", "",
NewSpan { Span {
contents: vec!["this is just some text"] contents: vec!["this is just some text"]
} }
)) ))
); );
assert_eq!( assert_eq!(
super::new_span("this is just some text {~lb}"), super::span("this is just some text {~lb}"),
Ok(( Ok((
"{~lb}", "{~lb}",
NewSpan { Span {
contents: vec!["this is just some text "] contents: vec!["this is just some text "]
} }
)) ))
); );
assert_eq!( assert_eq!(
super::new_span("{~lb}"), super::span("{~lb}"),
Err(Error(("{~lb}", ErrorKind::Verify))) Err(Error(("{~lb}", ErrorKind::Verify)))
); );
assert_eq!( assert_eq!(
super::new_span("this is \t \n\n \t \n \t multiline text\n {foo}"), super::span("this is \t \n\n \t \n \t multiline text\n {foo}"),
Ok(( Ok((
"{foo}", "{foo}",
NewSpan { Span {
contents: vec!["this is \t ", "multiline text"] contents: vec!["this is \t ", "multiline text"]
} }
)) ))
); );
assert_eq!( assert_eq!(
super::new_span("\n leading whitespace"), super::span("\n leading whitespace"),
Ok(( Ok((
"", "",
NewSpan { Span {
contents: vec!["leading whitespace"] contents: vec!["leading whitespace"]
} }
)) ))
@ -775,7 +732,9 @@ mod tests {
}, },
contents: Some(Body { contents: Some(Body {
elements: vec![ elements: vec![
TemplateElement::TESpan(Span { contents: "hello " }), TemplateElement::TESpan(Span {
contents: vec!["hello "]
}),
TemplateElement::TETag(DustTag::DTReference(Reference { TemplateElement::TETag(DustTag::DTReference(Reference {
path: Path { keys: vec!["name"] }, path: Path { keys: vec!["name"] },
filters: Vec::new() filters: Vec::new()
@ -800,7 +759,9 @@ mod tests {
}, },
contents: Some(Body { contents: Some(Body {
elements: vec![ elements: vec![
TemplateElement::TESpan(Span { contents: "hello " }), TemplateElement::TESpan(Span {
contents: vec!["hello "]
}),
TemplateElement::TETag(DustTag::DTReference(Reference { TemplateElement::TETag(DustTag::DTReference(Reference {
path: Path { keys: vec!["name"] }, path: Path { keys: vec!["name"] },
filters: Vec::new() filters: Vec::new()
@ -810,7 +771,7 @@ mod tests {
else_contents: Some(Body { else_contents: Some(Body {
elements: vec![ elements: vec![
TemplateElement::TESpan(Span { TemplateElement::TESpan(Span {
contents: "goodbye " contents: vec!["goodbye "]
}), }),
TemplateElement::TETag(DustTag::DTReference(Reference { TemplateElement::TETag(DustTag::DTReference(Reference {
path: Path { keys: vec!["name"] }, path: Path { keys: vec!["name"] },
@ -847,7 +808,9 @@ mod tests {
name: "foo", name: "foo",
contents: Some(Body { contents: Some(Body {
elements: vec![ elements: vec![
TemplateElement::TESpan(Span { contents: "hello " }), TemplateElement::TESpan(Span {
contents: vec!["hello "]
}),
TemplateElement::TETag(DustTag::DTReference(Reference { TemplateElement::TETag(DustTag::DTReference(Reference {
path: Path { keys: vec!["name"] }, path: Path { keys: vec!["name"] },
filters: Vec::new() filters: Vec::new()
@ -883,7 +846,9 @@ mod tests {
name: "foo", name: "foo",
contents: Some(Body { contents: Some(Body {
elements: vec![ elements: vec![
TemplateElement::TESpan(Span { contents: "hello " }), TemplateElement::TESpan(Span {
contents: vec!["hello "]
}),
TemplateElement::TETag(DustTag::DTReference(Reference { TemplateElement::TETag(DustTag::DTReference(Reference {
path: Path { keys: vec!["name"] }, path: Path { keys: vec!["name"] },
filters: Vec::new() filters: Vec::new()
@ -970,13 +935,15 @@ mod tests {
contents: Some(Body { contents: Some(Body {
elements: vec![ elements: vec![
TemplateElement::TESpan(Span { TemplateElement::TESpan(Span {
contents: "Pet the " contents: vec!["Pet the "]
}), }),
TemplateElement::TETag(DustTag::DTReference(Reference { TemplateElement::TETag(DustTag::DTReference(Reference {
path: Path { keys: vec!["name"] }, path: Path { keys: vec!["name"] },
filters: Vec::new() filters: Vec::new()
})), })),
TemplateElement::TESpan(Span { contents: "!" }) TemplateElement::TESpan(Span {
contents: vec!["!"]
})
] ]
}), }),
else_contents: None else_contents: None

View File

@ -85,7 +85,10 @@ impl<'a> DustRenderer<'a> {
let mut output = String::new(); let mut output = String::new();
for elem in &body.elements { for elem in &body.elements {
match elem { match elem {
TemplateElement::TESpan(span) => output.push_str(span.contents), TemplateElement::TESpan(span) => span
.contents
.iter()
.for_each(|line: &&str| output.push_str(line)),
TemplateElement::TETag(dt) => { TemplateElement::TETag(dt) => {
output.push_str(&self.render_tag(dt, context)?); output.push_str(&self.render_tag(dt, context)?);
} }