Introducing an RValue enum to handle paths vs literals

This commit is contained in:
Tom Alexander 2020-04-06 23:30:42 -04:00
parent 8641e5a98b
commit dd8b4ac28c
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE

View File

@ -113,6 +113,12 @@ enum TemplateElement<'a> {
TETag(DustTag<'a>),
}
#[derive(Clone, Debug, PartialEq)]
enum RValue<'a> {
RVPath(Path<'a>),
RVLiteral(&'a str),
}
/// Any element significant to dust that isn't plain text
///
/// These elements are always wrapped in curly braces
@ -168,12 +174,13 @@ fn path(i: &str) -> IResult<&str, Path> {
}
/// Either a literal or a path to a value
fn rvalue(i: &str) -> IResult<&str, &str> {
key(i)
fn rvalue(i: &str) -> IResult<&str, RValue> {
map(path, RValue::RVPath)(i)
}
fn key_value_pair(i: &str) -> IResult<&str, (&str, Path)> {
separated_pair(key, tag("="), path)(i)
/// Parameters for a partial
fn key_value_pair(i: &str) -> IResult<&str, (&str, RValue)> {
separated_pair(key, tag("="), rvalue)(i)
}
/// Display a value from the context
@ -681,7 +688,13 @@ mod tests {
)),
tag("/}"),
)("{>foo bar=baz/}"),
Ok(("", ("foo", Some(vec![("bar", Path { keys: vec!["baz"] })]))))
Ok((
"",
(
"foo",
Some(vec![("bar", RValue::RVPath(Path { keys: vec!["baz"] }))])
)
))
);
}
}