Add support for filters

This commit is contained in:
Tom Alexander 2020-04-04 20:51:10 -04:00
parent 5127534609
commit ac2ee2c756
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE

View File

@ -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<Filter>,
}
#[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)
}