Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for boolean literals #265

Merged
merged 1 commit into from Oct 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions askama_derive/src/generator.rs
Expand Up @@ -865,6 +865,7 @@ impl<'a> Generator<'a> {

fn visit_expr(&mut self, buf: &mut Buffer, expr: &Expr) -> DisplayWrap {
match *expr {
Expr::BoolLit(s) => self.visit_bool_lit(buf, s),
Expr::NumLit(s) => self.visit_num_lit(buf, s),
Expr::StrLit(s) => self.visit_str_lit(buf, s),
Expr::Var(s) => self.visit_var(buf, s),
Expand Down Expand Up @@ -1127,6 +1128,11 @@ impl<'a> Generator<'a> {
DisplayWrap::Unwrapped
}

fn visit_bool_lit(&mut self, buf: &mut Buffer, s: &str) -> DisplayWrap {
buf.write(s);
DisplayWrap::Unwrapped
}

fn visit_str_lit(&mut self, buf: &mut Buffer, s: &str) -> DisplayWrap {
buf.write(&format!("\"{}\"", s));
DisplayWrap::Unwrapped
Expand Down
7 changes: 7 additions & 0 deletions askama_derive/src/parser.rs
Expand Up @@ -8,6 +8,7 @@ use askama_shared::Syntax;

#[derive(Debug)]
pub enum Expr<'a> {
BoolLit(&'a str),
NumLit(&'a str),
StrLit(&'a str),
Var(&'a str),
Expand Down Expand Up @@ -200,6 +201,11 @@ fn non_ascii(chr: u8) -> bool {
chr >= 0x80 && chr <= 0xFD
}

named!(expr_bool_lit<Input, Expr>, map!(
alt!(tag!("true") | tag!("false")),
|s| Expr::BoolLit(str::from_utf8(&s).unwrap())
));

named!(num_lit<Input, &str>, map!(nom::digit,
|s| str::from_utf8(s.0).unwrap()
));
Expand Down Expand Up @@ -376,6 +382,7 @@ named!(expr_group<Input, Expr>, map!(
));

named!(expr_single<Input, Expr>, alt!(
expr_bool_lit |
expr_num_lit |
expr_str_lit |
expr_path |
Expand Down
2 changes: 2 additions & 0 deletions testing/templates/literals.html
@@ -1 +1,3 @@
{{ "a" }}
{{ true }}
{{ false }}
2 changes: 1 addition & 1 deletion testing/tests/simple.rs
Expand Up @@ -114,7 +114,7 @@ struct LiteralsTemplate {}
#[test]
fn test_literals() {
let s = LiteralsTemplate {};
assert_eq!(s.render().unwrap(), "a");
assert_eq!(s.render().unwrap(), "a\ntrue\nfalse");
}

struct Holder {
Expand Down