From ac2ee2c756df052512b1bcce3c45813957a7a943 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sat, 4 Apr 2020 20:51:10 -0400 Subject: [PATCH] Add support for filters --- src/duster/parser.rs | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/src/duster/parser.rs b/src/duster/parser.rs index a0024c3..117be9c 100644 --- a/src/duster/parser.rs +++ b/src/duster/parser.rs @@ -5,8 +5,10 @@ use nom::bytes::complete::take_until; use nom::combinator::map; use nom::combinator::recognize; use nom::combinator::value; +use nom::multi::many0; use nom::multi::separated_list; use nom::sequence::delimited; +use nom::sequence::preceded; use nom::sequence::tuple; use nom::IResult; @@ -39,6 +41,18 @@ struct Path<'a> { #[derive(Clone, Debug)] struct Reference<'a> { path: Path<'a>, + filters: Vec, +} + +#[derive(Clone, Debug)] +enum Filter { + HtmlEncode, + DisableHtmlEncode, + JavascriptStringEncode, + EncodeUri, + EncodeUriComponent, + JsonStringify, + JsonParse, } fn dust_tag(i: &str) -> IResult<&str, DustTag> { @@ -83,7 +97,27 @@ fn path(i: &str) -> IResult<&str, Path> { } fn reference(i: &str) -> IResult<&str, Reference> { - // TODO: Add support for filters - let (remaining, p) = delimited(tag("{"), path, tag("}"))(i)?; - Ok((remaining, Reference { path: p })) + let (remaining, (p, filters)) = delimited(tag("{"), tuple((path, many0(filter))), tag("}"))(i)?; + Ok(( + remaining, + Reference { + path: p, + filters: filters, + }, + )) +} + +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) }