diff options
author | Dave Poulter <hello@davepoulter.net> | 2019-10-08 11:17:38 +0200 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2019-10-08 11:36:19 +0200 |
commit | c82db307b0456e2a901599516b14f0d92e9cc682 (patch) | |
tree | 4970f0824b6cc7ab452e22869329d22a183e4ec6 | |
parent | 66274b7c8ef2f47fe479013b9d408ac21a36472d (diff) | |
download | askama-c82db307b0456e2a901599516b14f0d92e9cc682.tar.gz askama-c82db307b0456e2a901599516b14f0d92e9cc682.tar.bz2 askama-c82db307b0456e2a901599516b14f0d92e9cc682.zip |
Add support for boolean literals
-rw-r--r-- | askama_derive/src/generator.rs | 6 | ||||
-rw-r--r-- | askama_derive/src/parser.rs | 7 | ||||
-rw-r--r-- | testing/templates/literals.html | 2 | ||||
-rw-r--r-- | testing/tests/simple.rs | 2 |
4 files changed, 16 insertions, 1 deletions
diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs index 7757016..28b8246 100644 --- a/askama_derive/src/generator.rs +++ b/askama_derive/src/generator.rs @@ -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), @@ -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 diff --git a/askama_derive/src/parser.rs b/askama_derive/src/parser.rs index b8fa94d..eaa4b62 100644 --- a/askama_derive/src/parser.rs +++ b/askama_derive/src/parser.rs @@ -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), @@ -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() )); @@ -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 | diff --git a/testing/templates/literals.html b/testing/templates/literals.html index b8d8fbe..9d973bb 100644 --- a/testing/templates/literals.html +++ b/testing/templates/literals.html @@ -1 +1,3 @@ {{ "a" }} +{{ true }} +{{ false }} diff --git a/testing/tests/simple.rs b/testing/tests/simple.rs index e4239ce..519b87c 100644 --- a/testing/tests/simple.rs +++ b/testing/tests/simple.rs @@ -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 { |