2020-04-05 00:16:44 +00:00
|
|
|
use nom::branch::alt;
|
2020-04-07 01:09:06 +00:00
|
|
|
use nom::bytes::complete::escaped_transform;
|
2020-04-05 00:37:35 +00:00
|
|
|
use nom::bytes::complete::is_a;
|
2020-04-07 00:20:53 +00:00
|
|
|
use nom::bytes::complete::is_not;
|
2020-04-05 00:16:44 +00:00
|
|
|
use nom::bytes::complete::tag;
|
|
|
|
use nom::bytes::complete::take_until;
|
2020-04-12 20:02:26 +00:00
|
|
|
use nom::bytes::complete::take_until_parser_matches;
|
|
|
|
use nom::character::complete::line_ending;
|
|
|
|
use nom::character::complete::multispace0;
|
2020-04-05 02:45:56 +00:00
|
|
|
use nom::character::complete::one_of;
|
2020-04-07 02:47:24 +00:00
|
|
|
use nom::character::complete::space1;
|
2020-04-12 20:02:26 +00:00
|
|
|
use nom::combinator::all_consuming;
|
2020-04-05 00:16:44 +00:00
|
|
|
use nom::combinator::map;
|
2020-04-05 02:45:56 +00:00
|
|
|
use nom::combinator::opt;
|
2020-04-05 00:37:35 +00:00
|
|
|
use nom::combinator::recognize;
|
2020-04-05 00:16:44 +00:00
|
|
|
use nom::combinator::value;
|
2020-04-05 02:45:56 +00:00
|
|
|
use nom::combinator::verify;
|
2020-04-05 00:51:10 +00:00
|
|
|
use nom::multi::many0;
|
2020-04-05 03:42:27 +00:00
|
|
|
use nom::multi::many1;
|
2020-04-12 21:39:24 +00:00
|
|
|
use nom::multi::separated_list1;
|
2020-04-05 00:16:44 +00:00
|
|
|
use nom::sequence::delimited;
|
2020-04-05 00:51:10 +00:00
|
|
|
use nom::sequence::preceded;
|
2020-04-07 03:21:03 +00:00
|
|
|
use nom::sequence::separated_pair;
|
2020-04-08 01:49:28 +00:00
|
|
|
use nom::sequence::terminated;
|
2020-04-05 00:37:35 +00:00
|
|
|
use nom::sequence::tuple;
|
2020-04-05 00:16:44 +00:00
|
|
|
use nom::IResult;
|
|
|
|
|
2020-04-05 03:42:27 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
2020-04-11 22:25:48 +00:00
|
|
|
pub enum DustTag<'a> {
|
2020-04-05 00:16:44 +00:00
|
|
|
DTSpecial(Special),
|
|
|
|
DTComment(Comment<'a>),
|
2020-04-05 00:37:35 +00:00
|
|
|
DTReference(Reference<'a>),
|
2020-04-05 21:05:22 +00:00
|
|
|
DTSection(Container<'a>),
|
|
|
|
DTExists(Container<'a>),
|
|
|
|
DTNotExists(Container<'a>),
|
2020-04-06 03:47:55 +00:00
|
|
|
DTBlock(NamedBlock<'a>),
|
|
|
|
DTInlinePartial(NamedBlock<'a>),
|
2020-04-08 00:37:15 +00:00
|
|
|
DTPartial(Partial<'a>),
|
2020-04-08 01:49:28 +00:00
|
|
|
DTHelperEquals(ParameterizedBlock<'a>),
|
|
|
|
DTHelperNotEquals(ParameterizedBlock<'a>),
|
|
|
|
DTHelperGreaterThan(ParameterizedBlock<'a>),
|
|
|
|
DTHelperLessThan(ParameterizedBlock<'a>),
|
|
|
|
DTHelperGreaterThenOrEquals(ParameterizedBlock<'a>),
|
|
|
|
DTHelperLessThenOrEquals(ParameterizedBlock<'a>),
|
2020-04-05 00:16:44 +00:00
|
|
|
}
|
|
|
|
|
2020-04-05 02:45:56 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
2020-04-13 01:54:15 +00:00
|
|
|
pub enum Special {
|
2020-04-05 00:16:44 +00:00
|
|
|
Space,
|
|
|
|
NewLine,
|
|
|
|
CarriageReturn,
|
|
|
|
LeftCurlyBrace,
|
|
|
|
RightCurlyBrace,
|
|
|
|
}
|
|
|
|
|
2020-04-05 02:45:56 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
2020-04-11 22:40:36 +00:00
|
|
|
pub struct Comment<'a> {
|
2020-04-05 00:16:44 +00:00
|
|
|
value: &'a str,
|
|
|
|
}
|
|
|
|
|
2020-04-28 23:02:43 +00:00
|
|
|
/// A series of keys separated by '.' to reference a variable in the context
|
|
|
|
///
|
|
|
|
/// Special case: If the path is just "." then keys will be an empty vec
|
2020-04-05 02:45:56 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
2020-04-12 02:52:20 +00:00
|
|
|
pub struct Path<'a> {
|
|
|
|
pub keys: Vec<&'a str>,
|
2020-04-05 00:37:35 +00:00
|
|
|
}
|
|
|
|
|
2020-04-05 02:45:56 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
2020-04-11 22:40:36 +00:00
|
|
|
pub struct Reference<'a> {
|
2020-04-12 02:52:20 +00:00
|
|
|
pub path: Path<'a>,
|
2020-04-13 01:54:15 +00:00
|
|
|
pub filters: Vec<Filter>,
|
2020-04-05 00:51:10 +00:00
|
|
|
}
|
|
|
|
|
2020-04-05 02:45:56 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
2020-04-13 01:54:15 +00:00
|
|
|
pub enum Filter {
|
2020-04-05 00:51:10 +00:00
|
|
|
HtmlEncode,
|
|
|
|
DisableHtmlEncode,
|
|
|
|
JavascriptStringEncode,
|
|
|
|
EncodeUri,
|
|
|
|
EncodeUriComponent,
|
|
|
|
JsonStringify,
|
|
|
|
JsonParse,
|
2020-04-05 00:37:35 +00:00
|
|
|
}
|
|
|
|
|
2020-04-05 02:45:56 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
2020-04-11 22:25:48 +00:00
|
|
|
pub struct Span<'a> {
|
|
|
|
pub contents: &'a str,
|
2020-04-05 01:30:56 +00:00
|
|
|
}
|
|
|
|
|
2020-05-03 16:28:55 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
|
|
pub struct NewSpan<'a> {
|
|
|
|
pub contents: Vec<&'a str>,
|
|
|
|
}
|
|
|
|
|
2020-04-05 03:42:27 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
2020-04-13 01:54:15 +00:00
|
|
|
pub struct Container<'a> {
|
2020-04-29 00:46:29 +00:00
|
|
|
pub path: Path<'a>,
|
|
|
|
pub contents: Option<Body<'a>>,
|
|
|
|
pub else_contents: Option<Body<'a>>,
|
2020-04-05 03:42:27 +00:00
|
|
|
}
|
|
|
|
|
2020-04-06 03:47:55 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
2020-04-13 01:54:15 +00:00
|
|
|
pub struct NamedBlock<'a> {
|
2020-04-06 03:47:55 +00:00
|
|
|
name: &'a str,
|
|
|
|
contents: Option<Body<'a>>,
|
|
|
|
}
|
|
|
|
|
2020-04-07 02:02:10 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
2020-04-13 01:54:15 +00:00
|
|
|
pub struct ParameterizedBlock<'a> {
|
2020-04-08 00:37:15 +00:00
|
|
|
name: &'a str,
|
|
|
|
params: Vec<KVPair<'a>>,
|
|
|
|
contents: Option<Body<'a>>,
|
|
|
|
else_contents: Option<Body<'a>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
2020-04-13 01:54:15 +00:00
|
|
|
pub struct Partial<'a> {
|
2020-04-07 02:02:10 +00:00
|
|
|
name: String,
|
2020-04-07 23:51:06 +00:00
|
|
|
params: Vec<KVPair<'a>>,
|
2020-04-07 02:02:10 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 03:47:50 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
|
|
enum RValue<'a> {
|
|
|
|
RVPath(Path<'a>),
|
|
|
|
RVString(String),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
|
|
struct KVPair<'a> {
|
|
|
|
key: &'a str,
|
|
|
|
value: RValue<'a>,
|
|
|
|
}
|
|
|
|
|
2020-04-05 03:42:27 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
2020-04-11 22:25:48 +00:00
|
|
|
pub struct Body<'a> {
|
|
|
|
pub elements: Vec<TemplateElement<'a>>,
|
2020-04-05 01:30:56 +00:00
|
|
|
}
|
|
|
|
|
2020-04-05 02:52:14 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2020-04-05 03:42:27 +00:00
|
|
|
pub struct Template<'a> {
|
2020-04-11 22:25:48 +00:00
|
|
|
pub contents: Body<'a>,
|
2020-04-05 03:42:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
2020-04-11 22:25:48 +00:00
|
|
|
pub enum TemplateElement<'a> {
|
2020-04-05 01:30:56 +00:00
|
|
|
TESpan(Span<'a>),
|
|
|
|
TETag(DustTag<'a>),
|
|
|
|
}
|
|
|
|
|
2020-04-05 23:34:46 +00:00
|
|
|
/// Any element significant to dust that isn't plain text
|
|
|
|
///
|
|
|
|
/// These elements are always wrapped in curly braces
|
2020-04-05 00:16:44 +00:00
|
|
|
fn dust_tag(i: &str) -> IResult<&str, DustTag> {
|
|
|
|
alt((
|
|
|
|
map(special, DustTag::DTSpecial),
|
|
|
|
map(comment, DustTag::DTComment),
|
2020-04-05 00:37:35 +00:00
|
|
|
map(reference, DustTag::DTReference),
|
2020-04-05 23:39:07 +00:00
|
|
|
conditional("{#", DustTag::DTSection),
|
|
|
|
conditional("{?", DustTag::DTExists),
|
|
|
|
conditional("{^", DustTag::DTNotExists),
|
2020-04-06 03:47:55 +00:00
|
|
|
named_block("{+", DustTag::DTBlock),
|
|
|
|
named_block("{<", DustTag::DTInlinePartial),
|
2020-04-08 00:21:26 +00:00
|
|
|
partial("{>", DustTag::DTPartial),
|
2020-04-08 01:49:28 +00:00
|
|
|
parameterized_block("{@", "gte", DustTag::DTHelperGreaterThenOrEquals),
|
|
|
|
parameterized_block("{@", "lte", DustTag::DTHelperLessThenOrEquals),
|
|
|
|
parameterized_block("{@", "eq", DustTag::DTHelperEquals),
|
|
|
|
parameterized_block("{@", "ne", DustTag::DTHelperNotEquals),
|
|
|
|
parameterized_block("{@", "gt", DustTag::DTHelperGreaterThan),
|
|
|
|
parameterized_block("{@", "lt", DustTag::DTHelperLessThan),
|
2020-04-05 00:16:44 +00:00
|
|
|
))(i)
|
|
|
|
}
|
|
|
|
|
2020-04-05 23:34:46 +00:00
|
|
|
/// Special characters
|
2020-04-05 00:16:44 +00:00
|
|
|
fn special(i: &str) -> IResult<&str, Special> {
|
|
|
|
delimited(
|
|
|
|
tag("{~"),
|
|
|
|
alt((
|
2020-04-05 02:45:56 +00:00
|
|
|
value(Special::LeftCurlyBrace, tag("lb")),
|
|
|
|
value(Special::RightCurlyBrace, tag("rb")),
|
2020-04-05 00:16:44 +00:00
|
|
|
value(Special::Space, tag("s")),
|
2020-04-05 02:45:56 +00:00
|
|
|
value(Special::NewLine, tag("n")),
|
|
|
|
value(Special::CarriageReturn, tag("r")),
|
2020-04-05 00:16:44 +00:00
|
|
|
)),
|
|
|
|
tag("}"),
|
|
|
|
)(i)
|
|
|
|
}
|
|
|
|
|
2020-04-05 23:34:46 +00:00
|
|
|
/// Part of a dust template that does not get rendered
|
2020-04-05 00:16:44 +00:00
|
|
|
fn comment(i: &str) -> IResult<&str, Comment> {
|
|
|
|
map(delimited(tag("{!"), take_until("!}"), tag("!}")), |body| {
|
|
|
|
Comment { value: body }
|
|
|
|
})(i)
|
|
|
|
}
|
2020-04-05 00:37:35 +00:00
|
|
|
|
2020-04-05 23:34:46 +00:00
|
|
|
/// A single element of a path
|
|
|
|
fn key(i: &str) -> IResult<&str, &str> {
|
|
|
|
recognize(tuple((
|
|
|
|
one_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_$"),
|
|
|
|
opt(is_a(
|
|
|
|
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_$0123456789-",
|
|
|
|
)),
|
|
|
|
)))(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A series of keys separated by '.' to reference a variable in the context
|
2020-04-05 00:37:35 +00:00
|
|
|
fn path(i: &str) -> IResult<&str, Path> {
|
2020-04-28 23:02:43 +00:00
|
|
|
alt((
|
|
|
|
map(separated_list1(tag("."), key), |body| Path { keys: body }),
|
|
|
|
value(Path { keys: Vec::new() }, tag(".")),
|
|
|
|
))(i)
|
2020-04-05 00:37:35 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 03:21:03 +00:00
|
|
|
/// Either a literal or a path to a value
|
2020-04-07 03:30:42 +00:00
|
|
|
fn rvalue(i: &str) -> IResult<&str, RValue> {
|
2020-04-07 03:47:50 +00:00
|
|
|
alt((
|
|
|
|
map(path, RValue::RVPath),
|
|
|
|
map(quoted_string, RValue::RVString),
|
|
|
|
))(i)
|
2020-04-07 03:21:03 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 03:30:42 +00:00
|
|
|
/// Parameters for a partial
|
2020-04-07 03:35:09 +00:00
|
|
|
fn key_value_pair(i: &str) -> IResult<&str, KVPair> {
|
|
|
|
map(separated_pair(key, tag("="), rvalue), |(k, v)| KVPair {
|
|
|
|
key: k,
|
|
|
|
value: v,
|
|
|
|
})(i)
|
2020-04-07 03:21:03 +00:00
|
|
|
}
|
|
|
|
|
2020-04-05 23:34:46 +00:00
|
|
|
/// Display a value from the context
|
2020-04-05 00:37:35 +00:00
|
|
|
fn reference(i: &str) -> IResult<&str, Reference> {
|
2020-04-05 00:51:10 +00:00
|
|
|
let (remaining, (p, filters)) = delimited(tag("{"), tuple((path, many0(filter))), tag("}"))(i)?;
|
|
|
|
Ok((
|
|
|
|
remaining,
|
|
|
|
Reference {
|
|
|
|
path: p,
|
|
|
|
filters: filters,
|
|
|
|
},
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2020-04-05 23:39:07 +00:00
|
|
|
fn conditional<'a, F>(
|
2020-04-05 20:53:15 +00:00
|
|
|
open_matcher: &'static str,
|
2020-04-05 21:05:22 +00:00
|
|
|
constructor: F,
|
2020-04-12 21:39:24 +00:00
|
|
|
) -> impl FnMut(&'a str) -> IResult<&'a str, DustTag<'a>>
|
2020-04-05 21:12:48 +00:00
|
|
|
where
|
|
|
|
F: Copy + Fn(Container<'a>) -> DustTag<'a>,
|
|
|
|
{
|
|
|
|
alt((
|
2020-04-05 23:39:07 +00:00
|
|
|
conditional_with_body(open_matcher, constructor),
|
|
|
|
self_closing_conditional(open_matcher, constructor),
|
2020-04-05 21:12:48 +00:00
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2020-04-05 23:39:07 +00:00
|
|
|
fn conditional_with_body<'a, F>(
|
2020-04-05 21:12:48 +00:00
|
|
|
open_matcher: &'static str,
|
|
|
|
constructor: F,
|
|
|
|
) -> impl Fn(&'a str) -> IResult<&'a str, DustTag<'a>>
|
2020-04-05 20:53:15 +00:00
|
|
|
where
|
2020-04-05 21:05:22 +00:00
|
|
|
F: Fn(Container<'a>) -> DustTag<'a>,
|
2020-04-05 20:53:15 +00:00
|
|
|
{
|
|
|
|
move |i: &'a str| {
|
2020-04-05 21:24:13 +00:00
|
|
|
let (i, (opening_name, inner, maybe_else, _closing_name)) = verify(
|
2020-04-05 20:53:15 +00:00
|
|
|
tuple((
|
|
|
|
delimited(tag(open_matcher), path, tag("}")),
|
2020-04-05 23:29:16 +00:00
|
|
|
opt(body),
|
|
|
|
opt(preceded(tag("{:else}"), opt(body))),
|
2020-04-05 20:53:15 +00:00
|
|
|
delimited(tag("{/"), path, tag("}")),
|
|
|
|
)),
|
2020-04-05 21:24:13 +00:00
|
|
|
|(open, _inn, _maybe_else, close)| open == close,
|
2020-04-05 20:53:15 +00:00
|
|
|
)(i)?;
|
|
|
|
|
2020-04-05 21:05:22 +00:00
|
|
|
Ok((
|
|
|
|
i,
|
|
|
|
constructor(Container {
|
|
|
|
path: opening_name,
|
|
|
|
contents: inner,
|
2020-04-05 21:24:13 +00:00
|
|
|
else_contents: maybe_else.flatten(),
|
2020-04-05 21:05:22 +00:00
|
|
|
}),
|
|
|
|
))
|
2020-04-05 20:53:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-05 23:39:07 +00:00
|
|
|
fn self_closing_conditional<'a, F>(
|
2020-04-05 21:12:48 +00:00
|
|
|
open_matcher: &'static str,
|
|
|
|
constructor: F,
|
|
|
|
) -> impl Fn(&'a str) -> IResult<&'a str, DustTag<'a>>
|
|
|
|
where
|
|
|
|
F: Fn(Container<'a>) -> DustTag<'a>,
|
|
|
|
{
|
|
|
|
move |i: &'a str| {
|
|
|
|
let (i, path) = delimited(tag(open_matcher), path, tag("/}"))(i)?;
|
|
|
|
|
|
|
|
Ok((
|
|
|
|
i,
|
|
|
|
constructor(Container {
|
|
|
|
path: path,
|
|
|
|
contents: None,
|
2020-04-05 21:24:13 +00:00
|
|
|
else_contents: None,
|
2020-04-05 21:12:48 +00:00
|
|
|
}),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-06 03:47:55 +00:00
|
|
|
fn named_block<'a, F>(
|
|
|
|
open_matcher: &'static str,
|
|
|
|
constructor: F,
|
2020-04-12 21:39:24 +00:00
|
|
|
) -> impl FnMut(&'a str) -> IResult<&'a str, DustTag<'a>>
|
2020-04-06 03:47:55 +00:00
|
|
|
where
|
|
|
|
F: Copy + Fn(NamedBlock<'a>) -> DustTag<'a>,
|
|
|
|
{
|
|
|
|
alt((
|
|
|
|
named_block_with_body(open_matcher, constructor),
|
|
|
|
self_closing_named_block(open_matcher, constructor),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn named_block_with_body<'a, F>(
|
|
|
|
open_matcher: &'static str,
|
|
|
|
constructor: F,
|
|
|
|
) -> impl Fn(&'a str) -> IResult<&'a str, DustTag<'a>>
|
|
|
|
where
|
|
|
|
F: Fn(NamedBlock<'a>) -> DustTag<'a>,
|
|
|
|
{
|
|
|
|
move |i: &'a str| {
|
|
|
|
let (i, (opening_name, inner, _closing_name)) = verify(
|
|
|
|
tuple((
|
|
|
|
delimited(tag(open_matcher), key, tag("}")),
|
|
|
|
opt(body),
|
|
|
|
delimited(tag("{/"), key, tag("}")),
|
|
|
|
)),
|
|
|
|
|(open, _inn, close)| open == close,
|
|
|
|
)(i)?;
|
|
|
|
|
|
|
|
Ok((
|
|
|
|
i,
|
|
|
|
constructor(NamedBlock {
|
|
|
|
name: opening_name,
|
|
|
|
contents: inner,
|
|
|
|
}),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn self_closing_named_block<'a, F>(
|
|
|
|
open_matcher: &'static str,
|
|
|
|
constructor: F,
|
|
|
|
) -> impl Fn(&'a str) -> IResult<&'a str, DustTag<'a>>
|
|
|
|
where
|
|
|
|
F: Fn(NamedBlock<'a>) -> DustTag<'a>,
|
|
|
|
{
|
|
|
|
move |i: &'a str| {
|
|
|
|
let (i, name) = delimited(tag(open_matcher), key, tag("/}"))(i)?;
|
|
|
|
|
|
|
|
Ok((
|
|
|
|
i,
|
|
|
|
constructor(NamedBlock {
|
|
|
|
name: name,
|
|
|
|
contents: None,
|
|
|
|
}),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-08 01:49:28 +00:00
|
|
|
fn parameterized_block<'a, F>(
|
|
|
|
open_matcher: &'static str,
|
|
|
|
tag_name: &'static str,
|
|
|
|
constructor: F,
|
2020-04-12 21:39:24 +00:00
|
|
|
) -> impl FnMut(&'a str) -> IResult<&'a str, DustTag<'a>>
|
2020-04-08 01:49:28 +00:00
|
|
|
where
|
|
|
|
F: Copy + Fn(ParameterizedBlock<'a>) -> DustTag<'a>,
|
|
|
|
{
|
|
|
|
alt((
|
|
|
|
parameterized_block_with_body(open_matcher, tag_name, constructor),
|
|
|
|
parameterized_self_closing_block(open_matcher, tag_name, constructor),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parameterized_block_with_body<'a, F>(
|
|
|
|
open_matcher: &'static str,
|
|
|
|
tag_name: &'static str,
|
|
|
|
constructor: F,
|
|
|
|
) -> impl Fn(&'a str) -> IResult<&'a str, DustTag<'a>>
|
|
|
|
where
|
|
|
|
F: Fn(ParameterizedBlock<'a>) -> DustTag<'a>,
|
|
|
|
{
|
|
|
|
move |i: &'a str| {
|
|
|
|
let (i, (name, params, inner, maybe_else, _closing_name)) = tuple((
|
|
|
|
preceded(tag(open_matcher), tag(tag_name)),
|
|
|
|
terminated(
|
2020-04-12 21:39:24 +00:00
|
|
|
opt(preceded(space1, separated_list1(space1, key_value_pair))),
|
2020-04-08 01:49:28 +00:00
|
|
|
tag("}"),
|
|
|
|
),
|
|
|
|
opt(body),
|
|
|
|
opt(preceded(tag("{:else}"), opt(body))),
|
|
|
|
delimited(tag("{/"), tag(tag_name), tag("}")),
|
|
|
|
))(i)?;
|
|
|
|
|
|
|
|
Ok((
|
|
|
|
i,
|
|
|
|
constructor(ParameterizedBlock {
|
|
|
|
name: name,
|
|
|
|
params: params.unwrap_or(Vec::new()),
|
|
|
|
contents: inner,
|
|
|
|
else_contents: maybe_else.flatten(),
|
|
|
|
}),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-07 02:02:10 +00:00
|
|
|
fn parameterized_self_closing_block<'a, F>(
|
|
|
|
open_matcher: &'static str,
|
2020-04-08 00:37:15 +00:00
|
|
|
tag_name: &'static str,
|
2020-04-07 02:02:10 +00:00
|
|
|
constructor: F,
|
|
|
|
) -> impl Fn(&'a str) -> IResult<&'a str, DustTag<'a>>
|
|
|
|
where
|
|
|
|
F: Fn(ParameterizedBlock<'a>) -> DustTag<'a>,
|
|
|
|
{
|
|
|
|
move |i: &'a str| {
|
2020-04-07 02:47:24 +00:00
|
|
|
let (i, (name, params)) = delimited(
|
|
|
|
tag(open_matcher),
|
|
|
|
tuple((
|
2020-04-08 00:37:15 +00:00
|
|
|
tag(tag_name),
|
2020-04-12 21:39:24 +00:00
|
|
|
opt(preceded(space1, separated_list1(space1, key_value_pair))),
|
2020-04-07 02:47:24 +00:00
|
|
|
)),
|
|
|
|
tag("/}"),
|
|
|
|
)(i)?;
|
2020-04-07 02:02:10 +00:00
|
|
|
|
|
|
|
Ok((
|
|
|
|
i,
|
|
|
|
constructor(ParameterizedBlock {
|
2020-04-07 23:51:06 +00:00
|
|
|
name: name,
|
2020-04-08 00:21:26 +00:00
|
|
|
params: params.unwrap_or(Vec::new()),
|
2020-04-08 00:37:15 +00:00
|
|
|
contents: None,
|
|
|
|
else_contents: None,
|
2020-04-08 00:21:26 +00:00
|
|
|
}),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn partial<'a, F>(
|
|
|
|
open_matcher: &'static str,
|
|
|
|
constructor: F,
|
|
|
|
) -> impl Fn(&'a str) -> IResult<&'a str, DustTag<'a>>
|
|
|
|
where
|
2020-04-08 00:37:15 +00:00
|
|
|
F: Fn(Partial<'a>) -> DustTag<'a>,
|
2020-04-08 00:21:26 +00:00
|
|
|
{
|
|
|
|
move |i: &'a str| {
|
|
|
|
let (i, (name, params)) = delimited(
|
|
|
|
tag(open_matcher),
|
|
|
|
tuple((
|
|
|
|
alt((map(key, String::from), quoted_string)),
|
2020-04-12 21:39:24 +00:00
|
|
|
opt(preceded(space1, separated_list1(space1, key_value_pair))),
|
2020-04-08 00:21:26 +00:00
|
|
|
)),
|
|
|
|
tag("/}"),
|
|
|
|
)(i)?;
|
|
|
|
|
|
|
|
Ok((
|
|
|
|
i,
|
2020-04-08 00:37:15 +00:00
|
|
|
constructor(Partial {
|
2020-04-08 00:21:26 +00:00
|
|
|
name: name,
|
2020-04-07 02:47:24 +00:00
|
|
|
params: params.unwrap_or(Vec::new()),
|
2020-04-07 02:02:10 +00:00
|
|
|
}),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-05 00:51:10 +00:00
|
|
|
fn filter(i: &str) -> IResult<&str, Filter> {
|
|
|
|
preceded(
|
|
|
|
tag("|"),
|
|
|
|
alt((
|
|
|
|
value(Filter::JsonStringify, tag("js")),
|
|
|
|
value(Filter::JsonParse, tag("jp")),
|
|
|
|
value(Filter::EncodeUriComponent, tag("uc")),
|
|
|
|
value(Filter::HtmlEncode, tag("h")),
|
|
|
|
value(Filter::DisableHtmlEncode, tag("s")),
|
|
|
|
value(Filter::JavascriptStringEncode, tag("j")),
|
|
|
|
value(Filter::EncodeUri, tag("u")),
|
|
|
|
)),
|
|
|
|
)(i)
|
2020-04-05 00:37:35 +00:00
|
|
|
}
|
2020-04-05 01:30:56 +00:00
|
|
|
|
2020-05-03 16:28:55 +00:00
|
|
|
/// Whitespace at the beginning of lines is ignored so inside a span
|
|
|
|
/// we are matching a newline character followed by as much contiguous
|
|
|
|
/// whitespace as possible, all of which will be thrown away by other
|
|
|
|
/// parsers.
|
|
|
|
fn span_end_of_line(i: &str) -> IResult<&str, (&str, &str)> {
|
|
|
|
tuple((line_ending, multispace0))(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn span_line(i: &str) -> IResult<&str, &str> {
|
|
|
|
verify(
|
|
|
|
take_until_parser_matches(alt((
|
|
|
|
tag("{"),
|
|
|
|
line_ending,
|
|
|
|
recognize(all_consuming(eof_whitespace)),
|
|
|
|
))),
|
|
|
|
|s: &str| s.len() > 0,
|
|
|
|
)(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Any text that is not a Dust element
|
|
|
|
fn new_span(i: &str) -> IResult<&str, NewSpan> {
|
|
|
|
let (remaining, lines) = preceded(
|
|
|
|
opt(span_end_of_line),
|
|
|
|
many1(terminated(span_line, opt(span_end_of_line))),
|
|
|
|
)(i)?;
|
|
|
|
Ok((remaining, NewSpan { contents: lines }))
|
|
|
|
}
|
|
|
|
|
2020-04-05 01:30:56 +00:00
|
|
|
/// Any text that is not a Dust element
|
|
|
|
fn span(i: &str) -> IResult<&str, Span> {
|
2020-04-12 20:02:26 +00:00
|
|
|
let (remaining, body) = verify(
|
|
|
|
alt((
|
|
|
|
take_until("{"),
|
|
|
|
take_until_parser_matches(all_consuming(eof_whitespace)),
|
|
|
|
)),
|
|
|
|
|s: &str| s.len() > 0,
|
|
|
|
)(i)?;
|
2020-04-05 01:30:56 +00:00
|
|
|
Ok((remaining, Span { contents: body }))
|
|
|
|
}
|
|
|
|
|
2020-04-05 23:29:16 +00:00
|
|
|
fn body(i: &str) -> IResult<&str, Body> {
|
2020-04-05 03:42:27 +00:00
|
|
|
let (remaining, template_elements) = many1(alt((
|
2020-04-05 01:30:56 +00:00
|
|
|
map(span, TemplateElement::TESpan),
|
|
|
|
map(dust_tag, TemplateElement::TETag),
|
|
|
|
)))(i)?;
|
|
|
|
Ok((
|
|
|
|
remaining,
|
2020-04-05 23:29:16 +00:00
|
|
|
Body {
|
2020-04-05 01:30:56 +00:00
|
|
|
elements: template_elements,
|
|
|
|
},
|
|
|
|
))
|
|
|
|
}
|
2020-04-05 02:45:56 +00:00
|
|
|
|
2020-04-05 03:42:27 +00:00
|
|
|
pub fn template(i: &str) -> IResult<&str, Template> {
|
2020-04-12 20:02:26 +00:00
|
|
|
// DustJS ignores all preceding whitespace (tabs, newlines, spaces) but only ignores trailing newlines
|
|
|
|
let (remaining, contents) = delimited(multispace0, body, eof_whitespace)(i)?;
|
2020-04-05 03:42:27 +00:00
|
|
|
Ok((remaining, Template { contents: contents }))
|
|
|
|
}
|
|
|
|
|
2020-04-07 01:09:06 +00:00
|
|
|
fn quoted_string(i: &str) -> IResult<&str, String> {
|
2020-04-07 01:03:03 +00:00
|
|
|
delimited(
|
|
|
|
tag(r#"""#),
|
2020-04-07 01:09:06 +00:00
|
|
|
escaped_transform(is_not(r#"\""#), '\\', one_of(r#"\""#)),
|
2020-04-07 01:03:03 +00:00
|
|
|
tag(r#"""#),
|
|
|
|
)(i)
|
2020-04-07 00:20:53 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 20:02:26 +00:00
|
|
|
fn eof_whitespace(i: &str) -> IResult<&str, Vec<&str>> {
|
|
|
|
many0(line_ending)(i)
|
|
|
|
}
|
|
|
|
|
2020-04-05 02:45:56 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use nom::bytes::complete::is_a;
|
|
|
|
use nom::error::ErrorKind;
|
|
|
|
use nom::Err::Error;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_reference() {
|
|
|
|
assert_eq!(
|
|
|
|
super::reference("{foo.bar.baz|js|s}"),
|
|
|
|
Ok((
|
|
|
|
"",
|
|
|
|
Reference {
|
|
|
|
path: Path {
|
|
|
|
keys: vec!["foo", "bar", "baz"]
|
|
|
|
},
|
|
|
|
filters: vec![Filter::JsonStringify, Filter::DisableHtmlEncode],
|
|
|
|
}
|
|
|
|
))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_path() {
|
|
|
|
assert_eq!(
|
|
|
|
is_a::<_, _, (_, ErrorKind)>("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_$")(
|
|
|
|
"foo"
|
|
|
|
),
|
|
|
|
Ok(("", "foo"))
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
super::path("foo.bar.baz"),
|
|
|
|
Ok((
|
|
|
|
"",
|
|
|
|
Path {
|
|
|
|
keys: vec!["foo", "bar", "baz"]
|
|
|
|
}
|
|
|
|
))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_special() {
|
|
|
|
assert_eq!(super::special("{~s}"), Ok(("", Special::Space)));
|
|
|
|
assert_eq!(super::special("{~n}"), Ok(("", Special::NewLine)));
|
|
|
|
assert_eq!(super::special("{~r}"), Ok(("", Special::CarriageReturn)));
|
|
|
|
assert_eq!(super::special("{~lb}"), Ok(("", Special::LeftCurlyBrace)));
|
|
|
|
assert_eq!(super::special("{~rb}"), Ok(("", Special::RightCurlyBrace)));
|
|
|
|
assert_eq!(
|
|
|
|
super::special("{~zzz}"),
|
|
|
|
Err(Error(("zzz}", ErrorKind::Tag)))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_comment() {
|
|
|
|
assert_eq!(
|
|
|
|
super::comment("{! yo dawg} this is a comment !}"),
|
|
|
|
Ok((
|
|
|
|
"",
|
|
|
|
Comment {
|
|
|
|
value: " yo dawg} this is a comment "
|
|
|
|
}
|
|
|
|
))
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
super::special("{! this is a comment without a close"),
|
|
|
|
Err(Error((
|
|
|
|
"{! this is a comment without a close",
|
|
|
|
ErrorKind::Tag
|
|
|
|
)))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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)))
|
|
|
|
);
|
|
|
|
}
|
2020-05-03 16:28:55 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_span_end_of_line() {
|
|
|
|
assert_eq!(
|
|
|
|
super::span_end_of_line("\n \t \n\nfoo"),
|
|
|
|
Ok(("foo", ("\n", " \t \n\n")))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_span_line() {
|
|
|
|
assert_eq!(
|
|
|
|
super::span_line("this is just some text"),
|
|
|
|
Ok(("", "this is just some text"))
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
super::span_line("this is just some text {~lb}"),
|
|
|
|
Ok(("{~lb}", "this is just some text "))
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
super::span_line("{~lb}"),
|
|
|
|
Err(Error(("{~lb}", ErrorKind::Verify)))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_new_span() {
|
|
|
|
assert_eq!(
|
|
|
|
super::new_span("this is just some text"),
|
|
|
|
Ok((
|
|
|
|
"",
|
|
|
|
NewSpan {
|
|
|
|
contents: vec!["this is just some text"]
|
|
|
|
}
|
|
|
|
))
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
super::new_span("this is just some text {~lb}"),
|
|
|
|
Ok((
|
|
|
|
"{~lb}",
|
|
|
|
NewSpan {
|
|
|
|
contents: vec!["this is just some text "]
|
|
|
|
}
|
|
|
|
))
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
super::new_span("{~lb}"),
|
|
|
|
Err(Error(("{~lb}", ErrorKind::Verify)))
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
super::new_span("this is \t \n\n \t \n \t multiline text\n {foo}"),
|
|
|
|
Ok((
|
|
|
|
"{foo}",
|
|
|
|
NewSpan {
|
|
|
|
contents: vec!["this is \t ", "multiline text"]
|
|
|
|
}
|
|
|
|
))
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
super::new_span("\n leading whitespace"),
|
|
|
|
Ok((
|
|
|
|
"",
|
|
|
|
NewSpan {
|
|
|
|
contents: vec!["leading whitespace"]
|
|
|
|
}
|
|
|
|
))
|
|
|
|
);
|
|
|
|
}
|
2020-04-05 03:42:27 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_section_mismatched_paths() {
|
|
|
|
assert_eq!(
|
2020-04-05 21:05:22 +00:00
|
|
|
super::dust_tag("{#foo.bar}{/baz}"),
|
|
|
|
Err(Error(("{#foo.bar}{/baz}", ErrorKind::Tag)))
|
2020-04-05 03:42:27 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_empty_section() {
|
|
|
|
assert_eq!(
|
2020-04-05 21:05:22 +00:00
|
|
|
super::dust_tag("{#foo.bar}{/foo.bar}"),
|
2020-04-05 03:42:27 +00:00
|
|
|
Ok((
|
|
|
|
"",
|
2020-04-05 21:05:22 +00:00
|
|
|
DustTag::DTSection(Container {
|
2020-04-05 21:12:48 +00:00
|
|
|
path: Path {
|
|
|
|
keys: vec!["foo", "bar"]
|
|
|
|
},
|
2020-04-05 21:24:13 +00:00
|
|
|
contents: None,
|
|
|
|
else_contents: None,
|
2020-04-05 21:12:48 +00:00
|
|
|
})
|
|
|
|
))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_self_closing_section() {
|
|
|
|
assert_eq!(
|
|
|
|
super::dust_tag("{#foo.bar/}"),
|
|
|
|
Ok((
|
|
|
|
"",
|
|
|
|
DustTag::DTSection(Container {
|
2020-04-05 03:42:27 +00:00
|
|
|
path: Path {
|
|
|
|
keys: vec!["foo", "bar"]
|
|
|
|
},
|
2020-04-05 21:24:13 +00:00
|
|
|
contents: None,
|
|
|
|
else_contents: None,
|
2020-04-05 21:05:22 +00:00
|
|
|
})
|
2020-04-05 03:42:27 +00:00
|
|
|
))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_section_with_body() {
|
|
|
|
assert_eq!(
|
2020-04-05 21:05:22 +00:00
|
|
|
super::dust_tag("{#foo.bar}hello {name}{/foo.bar}"),
|
2020-04-05 03:42:27 +00:00
|
|
|
Ok((
|
|
|
|
"",
|
2020-04-05 21:05:22 +00:00
|
|
|
DustTag::DTSection(Container {
|
2020-04-05 03:42:27 +00:00
|
|
|
path: Path {
|
|
|
|
keys: vec!["foo", "bar"]
|
|
|
|
},
|
2020-04-05 23:29:16 +00:00
|
|
|
contents: Some(Body {
|
2020-04-05 03:42:27 +00:00
|
|
|
elements: vec![
|
|
|
|
TemplateElement::TESpan(Span { contents: "hello " }),
|
|
|
|
TemplateElement::TETag(DustTag::DTReference(Reference {
|
|
|
|
path: Path { keys: vec!["name"] },
|
|
|
|
filters: Vec::new()
|
|
|
|
}))
|
|
|
|
]
|
2020-04-05 21:24:13 +00:00
|
|
|
}),
|
|
|
|
else_contents: None,
|
|
|
|
})
|
|
|
|
))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_section_with_else_body() {
|
|
|
|
assert_eq!(
|
|
|
|
super::dust_tag("{#greeting}hello {name}{:else}goodbye {name}{/greeting}"),
|
|
|
|
Ok((
|
|
|
|
"",
|
|
|
|
DustTag::DTSection(Container {
|
|
|
|
path: Path {
|
|
|
|
keys: vec!["greeting"]
|
|
|
|
},
|
2020-04-05 23:29:16 +00:00
|
|
|
contents: Some(Body {
|
2020-04-05 21:24:13 +00:00
|
|
|
elements: vec![
|
|
|
|
TemplateElement::TESpan(Span { contents: "hello " }),
|
|
|
|
TemplateElement::TETag(DustTag::DTReference(Reference {
|
|
|
|
path: Path { keys: vec!["name"] },
|
|
|
|
filters: Vec::new()
|
|
|
|
}))
|
|
|
|
]
|
|
|
|
}),
|
2020-04-05 23:29:16 +00:00
|
|
|
else_contents: Some(Body {
|
2020-04-05 21:24:13 +00:00
|
|
|
elements: vec![
|
|
|
|
TemplateElement::TESpan(Span {
|
|
|
|
contents: "goodbye "
|
|
|
|
}),
|
|
|
|
TemplateElement::TETag(DustTag::DTReference(Reference {
|
|
|
|
path: Path { keys: vec!["name"] },
|
|
|
|
filters: Vec::new()
|
|
|
|
}))
|
|
|
|
]
|
|
|
|
}),
|
2020-04-05 21:05:22 +00:00
|
|
|
})
|
2020-04-05 03:42:27 +00:00
|
|
|
))
|
|
|
|
);
|
|
|
|
}
|
2020-04-06 03:47:55 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_self_closing_block() {
|
|
|
|
assert_eq!(
|
|
|
|
super::dust_tag("{+foo/}"),
|
|
|
|
Ok((
|
|
|
|
"",
|
|
|
|
DustTag::DTBlock(NamedBlock {
|
|
|
|
name: "foo",
|
|
|
|
contents: None
|
|
|
|
})
|
|
|
|
))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_block() {
|
|
|
|
assert_eq!(
|
|
|
|
super::dust_tag("{+foo}hello {name}{/foo}"),
|
|
|
|
Ok((
|
|
|
|
"",
|
|
|
|
DustTag::DTBlock(NamedBlock {
|
|
|
|
name: "foo",
|
|
|
|
contents: Some(Body {
|
|
|
|
elements: vec![
|
|
|
|
TemplateElement::TESpan(Span { contents: "hello " }),
|
|
|
|
TemplateElement::TETag(DustTag::DTReference(Reference {
|
|
|
|
path: Path { keys: vec!["name"] },
|
|
|
|
filters: Vec::new()
|
|
|
|
}))
|
|
|
|
]
|
|
|
|
})
|
|
|
|
})
|
|
|
|
))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_self_closing_inline_partial() {
|
|
|
|
assert_eq!(
|
|
|
|
super::dust_tag("{<foo/}"),
|
|
|
|
Ok((
|
|
|
|
"",
|
|
|
|
DustTag::DTInlinePartial(NamedBlock {
|
|
|
|
name: "foo",
|
|
|
|
contents: None
|
|
|
|
})
|
|
|
|
))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_inline_partial() {
|
|
|
|
assert_eq!(
|
|
|
|
super::dust_tag("{<foo}hello {name}{/foo}"),
|
|
|
|
Ok((
|
|
|
|
"",
|
|
|
|
DustTag::DTInlinePartial(NamedBlock {
|
|
|
|
name: "foo",
|
|
|
|
contents: Some(Body {
|
|
|
|
elements: vec![
|
|
|
|
TemplateElement::TESpan(Span { contents: "hello " }),
|
|
|
|
TemplateElement::TETag(DustTag::DTReference(Reference {
|
|
|
|
path: Path { keys: vec!["name"] },
|
|
|
|
filters: Vec::new()
|
|
|
|
}))
|
|
|
|
]
|
|
|
|
})
|
|
|
|
})
|
|
|
|
))
|
|
|
|
);
|
|
|
|
}
|
2020-04-07 00:20:53 +00:00
|
|
|
|
|
|
|
#[test]
|
2020-04-07 01:26:08 +00:00
|
|
|
fn test_quoted_string() {
|
2020-04-07 01:09:06 +00:00
|
|
|
assert_eq!(
|
2020-04-07 01:26:08 +00:00
|
|
|
quoted_string(r#""foo\"bar\\baz""#),
|
|
|
|
Ok(("", r#"foo"bar\baz"#.to_owned()))
|
2020-04-07 01:09:06 +00:00
|
|
|
);
|
2020-04-07 00:20:53 +00:00
|
|
|
}
|
2020-04-07 02:02:10 +00:00
|
|
|
|
|
|
|
#[test]
|
2020-04-07 23:51:06 +00:00
|
|
|
fn test_unquoted_partial() {
|
2020-04-07 03:47:50 +00:00
|
|
|
assert_eq!(
|
2020-04-07 23:51:06 +00:00
|
|
|
dust_tag(r#"{>foo bar=baz animal="cat"/}"#),
|
2020-04-07 03:47:50 +00:00
|
|
|
Ok((
|
|
|
|
"",
|
2020-04-08 00:37:15 +00:00
|
|
|
DustTag::DTPartial(Partial {
|
2020-04-07 23:51:06 +00:00
|
|
|
name: "foo".to_owned(),
|
|
|
|
params: vec![
|
|
|
|
KVPair {
|
|
|
|
key: "bar",
|
|
|
|
value: RValue::RVPath(Path { keys: vec!["baz"] })
|
|
|
|
},
|
|
|
|
KVPair {
|
|
|
|
key: "animal",
|
|
|
|
value: RValue::RVString("cat".to_owned())
|
|
|
|
}
|
|
|
|
]
|
|
|
|
})
|
2020-04-07 03:47:50 +00:00
|
|
|
))
|
|
|
|
);
|
2020-04-07 02:02:10 +00:00
|
|
|
}
|
2020-04-07 23:53:57 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_quoted_partial() {
|
|
|
|
assert_eq!(
|
|
|
|
dust_tag(r#"{>"template name * with * special \" characters" bar=baz animal="cat"/}"#),
|
|
|
|
Ok((
|
|
|
|
"",
|
2020-04-08 00:37:15 +00:00
|
|
|
DustTag::DTPartial(Partial {
|
2020-04-07 23:53:57 +00:00
|
|
|
name: 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::RVString("cat".to_owned())
|
|
|
|
}
|
|
|
|
]
|
|
|
|
})
|
|
|
|
))
|
|
|
|
);
|
|
|
|
}
|
2020-04-08 01:49:28 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_helper() {
|
|
|
|
assert_eq!(
|
|
|
|
dust_tag(r#"{@eq key=name value="cat"}Pet the {name}!{/eq}"#),
|
|
|
|
Ok((
|
|
|
|
"",
|
|
|
|
DustTag::DTHelperEquals(ParameterizedBlock {
|
|
|
|
name: "eq",
|
|
|
|
params: vec![
|
|
|
|
KVPair {
|
|
|
|
key: "key",
|
|
|
|
value: RValue::RVPath(Path { keys: vec!["name"] })
|
|
|
|
},
|
|
|
|
KVPair {
|
|
|
|
key: "value",
|
|
|
|
value: RValue::RVString("cat".to_owned())
|
|
|
|
}
|
|
|
|
],
|
|
|
|
contents: Some(Body {
|
|
|
|
elements: vec![
|
|
|
|
TemplateElement::TESpan(Span {
|
|
|
|
contents: "Pet the "
|
|
|
|
}),
|
|
|
|
TemplateElement::TETag(DustTag::DTReference(Reference {
|
|
|
|
path: Path { keys: vec!["name"] },
|
|
|
|
filters: Vec::new()
|
|
|
|
})),
|
|
|
|
TemplateElement::TESpan(Span { contents: "!" })
|
|
|
|
]
|
|
|
|
}),
|
|
|
|
else_contents: None
|
|
|
|
})
|
|
|
|
))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_self_closing_helper() {
|
|
|
|
assert_eq!(
|
|
|
|
dust_tag(r#"{@eq key=name value="cat"/}"#),
|
|
|
|
Ok((
|
|
|
|
"",
|
|
|
|
DustTag::DTHelperEquals(ParameterizedBlock {
|
|
|
|
name: "eq",
|
|
|
|
params: vec![
|
|
|
|
KVPair {
|
|
|
|
key: "key",
|
|
|
|
value: RValue::RVPath(Path { keys: vec!["name"] })
|
|
|
|
},
|
|
|
|
KVPair {
|
|
|
|
key: "value",
|
|
|
|
value: RValue::RVString("cat".to_owned())
|
|
|
|
}
|
|
|
|
],
|
|
|
|
contents: None,
|
|
|
|
else_contents: None
|
|
|
|
})
|
|
|
|
))
|
|
|
|
);
|
|
|
|
}
|
2020-04-05 02:45:56 +00:00
|
|
|
}
|