diff options
author | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2017-08-10 07:38:34 +0200 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2017-08-10 09:27:33 +0200 |
commit | 1e0ee705a8e295ebddd31a472990133d34ad3465 (patch) | |
tree | 203bc040015dee5a69592c531de4741ac6a03f91 | |
parent | ffc74a1775f6e4bbd8cd161fd68d814ec981f3c2 (diff) | |
download | askama-1e0ee705a8e295ebddd31a472990133d34ad3465.tar.gz askama-1e0ee705a8e295ebddd31a472990133d34ad3465.tar.bz2 askama-1e0ee705a8e295ebddd31a472990133d34ad3465.zip |
Propagate format errors and rename `render_to()` to `render_into()`
Diffstat (limited to '')
-rw-r--r-- | askama/src/lib.rs | 9 | ||||
-rw-r--r-- | askama_derive/src/generator.rs | 39 | ||||
-rw-r--r-- | testing/tests/filters.rs | 4 | ||||
-rw-r--r-- | testing/tests/include.rs | 2 | ||||
-rw-r--r-- | testing/tests/inheritance.rs | 4 | ||||
-rw-r--r-- | testing/tests/loops.rs | 4 | ||||
-rw-r--r-- | testing/tests/operators.rs | 4 | ||||
-rw-r--r-- | testing/tests/simple.rs | 28 |
8 files changed, 52 insertions, 42 deletions
diff --git a/askama/src/lib.rs b/askama/src/lib.rs index 8821871..54d211e 100644 --- a/askama/src/lib.rs +++ b/askama/src/lib.rs @@ -215,6 +215,7 @@ extern crate askama_derive; use std::env; +use std::fmt; use std::fs::{self, DirEntry}; use std::io; use std::path::{Path, PathBuf}; @@ -222,12 +223,12 @@ use std::path::{Path, PathBuf}; /// Main `Template` trait; implementations are generally derived pub trait Template { /// Renders the template to the given `writer` buffer - fn render_to(&self, writer: &mut std::fmt::Write); + fn render_into(&self, writer: &mut std::fmt::Write) -> Result<(), fmt::Error>; /// Helper method which allocates a new `String` and renders into it - fn render(&self) -> String { + fn render(&self) -> Result<String, fmt::Error> { let mut buf = String::new(); - self.render_to(&mut buf); - buf + self.render_into(&mut buf)?; + Ok(buf) } } diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs index 02bf71e..b412c9d 100644 --- a/askama_derive/src/generator.rs +++ b/askama_derive/src/generator.rs @@ -162,7 +162,7 @@ impl<'a> Generator<'a> { if self.next_ws.is_some() && !ws.0 { let val = self.next_ws.unwrap(); if !val.is_empty() { - self.writeln(&format!("writer.write_str({:#?}).unwrap();", + self.writeln(&format!("writer.write_str({:#?})?;", val)); } } @@ -304,12 +304,12 @@ impl<'a> Generator<'a> { assert!(rws.is_empty()); self.next_ws = Some(lws); } else { - self.writeln(&format!("writer.write_str({:#?}).unwrap();", + self.writeln(&format!("writer.write_str({:#?})?;", lws)); } } if !val.is_empty() { - self.writeln(&format!("writer.write_str({:#?}).unwrap();", val)); + self.writeln(&format!("writer.write_str({:#?})?;", val)); } if !rws.is_empty() { self.next_ws = Some(rws); @@ -320,7 +320,7 @@ impl<'a> Generator<'a> { self.handle_ws(ws); self.write("writer.write_fmt(format_args!(\"{}\", "); self.visit_expr(s); - self.writeln(")).unwrap();"); + self.writeln("))?;"); } fn write_cond(&mut self, conds: &'a [Cond], ws: &WS) { @@ -371,7 +371,7 @@ impl<'a> Generator<'a> { fn write_block(&mut self, ws1: &WS, name: &str, ws2: &WS) { self.flush_ws(ws1); - self.writeln(&format!("timpl.render_block_{}_to(writer);", name)); + self.writeln(&format!("timpl.render_block_{}_into(writer)?;", name)); self.prepare_ws(ws2); } @@ -379,11 +379,13 @@ impl<'a> Generator<'a> { ws2: &WS) { self.writeln("#[allow(unused_variables)]"); self.writeln(&format!( - "fn render_block_{}_to(&self, writer: &mut ::std::fmt::Write) {{", + "fn render_block_{}_into(&self, writer: &mut ::std::fmt::Write) \ + -> Result<(), ::std::fmt::Error> {{", name)); self.prepare_ws(ws1); self.handle(nodes); self.flush_ws(ws2); + self.writeln("Ok(())"); self.writeln("}"); } @@ -479,9 +481,11 @@ impl<'a> Generator<'a> { // Implement `Template` for the given context struct. fn impl_template(&mut self, ast: &syn::DeriveInput, nodes: &'a [Node]) { self.write_header(ast, "::askama::Template"); - self.writeln("fn render_to(&self, writer: &mut ::std::fmt::Write) {"); + self.writeln("fn render_into(&self, writer: &mut ::std::fmt::Write) -> \ + Result<(), ::std::fmt::Error> {"); self.handle(nodes); self.flush_ws(&WS(false, false)); + self.writeln("Ok(())"); self.writeln("}"); self.writeln("}"); } @@ -506,16 +510,18 @@ impl<'a> Generator<'a> { self.writeln("#[allow(unused_variables)]"); self.writeln(&format!( - "fn render_trait_to(&self, timpl: &{}, writer: &mut ::std::fmt::Write) {{", + "fn render_trait_into(&self, timpl: &{}, writer: &mut ::std::fmt::Write) \ + -> Result<(), ::std::fmt::Error> {{", trait_name)); if let Some(nodes) = nodes { self.handle(nodes); self.flush_ws(&WS(false, false)); } else { - self.writeln("self._parent.render_trait_to(self, writer);"); + self.writeln("self._parent.render_trait_into(self, writer)?;"); } + self.writeln("Ok(())"); self.writeln("}"); self.flush_ws(&WS(false, false)); self.writeln("}"); @@ -524,12 +530,14 @@ impl<'a> Generator<'a> { // Implement `Template` for templates that implement a template trait. fn impl_template_for_trait(&mut self, ast: &syn::DeriveInput, derived: bool) { self.write_header(ast, "::askama::Template"); - self.writeln("fn render_to(&self, writer: &mut ::std::fmt::Write) {"); + self.writeln("fn render_into(&self, writer: &mut ::std::fmt::Write) \ + -> Result<(), ::std::fmt::Error> {"); if derived { - self.writeln("self._parent.render_trait_to(self, writer);"); + self.writeln("self._parent.render_trait_into(self, writer)?;"); } else { - self.writeln("self.render_trait_to(self, writer);"); + self.writeln("self.render_trait_into(self, writer)?;"); } + self.writeln("Ok(())"); self.writeln("}"); self.writeln("}"); } @@ -540,13 +548,14 @@ impl<'a> Generator<'a> { for bname in block_names { self.writeln(&format!( - "fn render_block_{}_to(&self, writer: &mut ::std::fmt::Write);", + "fn render_block_{}_into(&self, writer: &mut ::std::fmt::Write) \ + -> Result<(), ::std::fmt::Error>;", bname)); } self.writeln(&format!( - "fn render_trait_to(&self, timpl: &{}, writer: &mut ::std::fmt::Write);", + "fn render_trait_into(&self, timpl: &{}, writer: &mut ::std::fmt::Write) \ + -> Result<(), ::std::fmt::Error>;", trait_name)); - self.writeln("}"); } diff --git a/testing/tests/filters.rs b/testing/tests/filters.rs index 0b1b7c9..665e50d 100644 --- a/testing/tests/filters.rs +++ b/testing/tests/filters.rs @@ -14,7 +14,7 @@ fn filter_escape() { let s = TestTemplate { strvar: "my <html> is unsafe & should be escaped".to_string(), }; - assert_eq!(s.render(), + assert_eq!(s.render().unwrap(), "my <html> is unsafe & should be escaped"); } @@ -28,5 +28,5 @@ struct FormatTemplate<'a> { #[test] fn filter_format() { let t = FormatTemplate { var: "formatted" }; - assert_eq!(t.render(), "\"formatted\""); + assert_eq!(t.render().unwrap(), "\"formatted\""); } diff --git a/testing/tests/include.rs b/testing/tests/include.rs index 7ddccd2..d2998a1 100644 --- a/testing/tests/include.rs +++ b/testing/tests/include.rs @@ -15,5 +15,5 @@ fn test_include() { let s = IncludeTemplate { strs: &strs, }; - assert_eq!(s.render(), "INCLUDED: fooINCLUDED: bar") + assert_eq!(s.render().unwrap(), "INCLUDED: fooINCLUDED: bar") } diff --git a/testing/tests/inheritance.rs b/testing/tests/inheritance.rs index 5993cd6..ca267e1 100644 --- a/testing/tests/inheritance.rs +++ b/testing/tests/inheritance.rs @@ -18,11 +18,11 @@ struct ChildTemplate<'a> { #[test] fn test_use_base_directly() { let t = BaseTemplate { title: "Foo" }; - assert_eq!(t.render(), "Foo\n\nCopyright 2017"); + assert_eq!(t.render().unwrap(), "Foo\n\nCopyright 2017"); } #[test] fn test_simple_extends() { let t = ChildTemplate { _parent: BaseTemplate { title: "Bar" } }; - assert_eq!(t.render(), "Bar\n(Bar) Content goes here\nCopyright 2017"); + assert_eq!(t.render().unwrap(), "Bar\n(Bar) Content goes here\nCopyright 2017"); } diff --git a/testing/tests/loops.rs b/testing/tests/loops.rs index eedbfa0..3125598 100644 --- a/testing/tests/loops.rs +++ b/testing/tests/loops.rs @@ -14,7 +14,7 @@ fn test_for() { let s = ForTemplate { strings: vec!["A", "alfa", "1"], }; - assert_eq!(s.render(), "0. A\n1. alfa\n2. 1\n"); + assert_eq!(s.render().unwrap(), "0. A\n1. alfa\n2. 1\n"); } @@ -31,5 +31,5 @@ fn test_nested_for() { let s = NestedForTemplate { seqs: vec![&alpha, &numbers], }; - assert_eq!(s.render(), "1\n 0a1b2c2\n 0one1two"); + assert_eq!(s.render().unwrap(), "1\n 0a1b2c2\n 0one1two"); } diff --git a/testing/tests/operators.rs b/testing/tests/operators.rs index 73b30b1..c58f650 100644 --- a/testing/tests/operators.rs +++ b/testing/tests/operators.rs @@ -14,7 +14,7 @@ struct CompareTemplate { #[test] fn test_compare() { let t = CompareTemplate { a: 1, b: 1, c: 2 }; - assert_eq!(t.render(), "tf\ntf\ntf\ntf\ntf\ntf"); + assert_eq!(t.render().unwrap(), "tf\ntf\ntf\ntf\ntf\ntf"); } @@ -29,5 +29,5 @@ struct OperatorsTemplate { #[test] fn test_operators() { let t = OperatorsTemplate { a: 1, b: 1, c: 2 }; - assert_eq!(t.render(), "muldivmodaddrshlshbandbxorborandor"); + assert_eq!(t.render().unwrap(), "muldivmodaddrshlshbandbxorborandor"); } diff --git a/testing/tests/simple.rs b/testing/tests/simple.rs index c924e85..b9b7a7c 100644 --- a/testing/tests/simple.rs +++ b/testing/tests/simple.rs @@ -18,10 +18,10 @@ fn test_variables() { num: 42, i18n: "Iñtërnâtiônàlizætiøn".to_string(), }; - assert_eq!(s.render(), "\nhello world, foo\n\ - with number: 42\n\ - Iñtërnâtiônàlizætiøn is important\n\ - in vars too: Iñtërnâtiônàlizætiøn"); + assert_eq!(s.render().unwrap(), "\nhello world, foo\n\ + with number: 42\n\ + Iñtërnâtiônàlizætiøn is important\n\ + in vars too: Iñtërnâtiônàlizætiøn"); } @@ -34,7 +34,7 @@ struct IfTemplate { #[test] fn test_if() { let s = IfTemplate { cond: true }; - assert_eq!(s.render(), "true"); + assert_eq!(s.render().unwrap(), "true"); } @@ -47,7 +47,7 @@ struct ElseTemplate { #[test] fn test_else() { let s = ElseTemplate { cond: false }; - assert_eq!(s.render(), "false"); + assert_eq!(s.render().unwrap(), "false"); } @@ -61,7 +61,7 @@ struct ElseIfTemplate { #[test] fn test_else_if() { let s = ElseIfTemplate { cond: false, check: true }; - assert_eq!(s.render(), "checked"); + assert_eq!(s.render().unwrap(), "checked"); } @@ -72,7 +72,7 @@ struct LiteralsTemplate {} #[test] fn test_literals() { let s = LiteralsTemplate {}; - assert_eq!(s.render(), "a"); + assert_eq!(s.render().unwrap(), "a"); } @@ -89,7 +89,7 @@ struct AttrTemplate { #[test] fn test_attr() { let t = AttrTemplate { inner: Holder { a: 5 } }; - assert_eq!(t.render(), "5"); + assert_eq!(t.render().unwrap(), "5"); } @@ -108,7 +108,7 @@ fn test_nested_attr() { let t = NestedAttrTemplate { inner: NestedHolder { holder: Holder { a: 5 } } }; - assert_eq!(t.render(), "5"); + assert_eq!(t.render().unwrap(), "5"); } @@ -121,9 +121,9 @@ struct OptionTemplate<'a> { #[test] fn test_option() { let some = OptionTemplate { var: Some("foo") }; - assert_eq!(some.render(), "some: foo"); + assert_eq!(some.render().unwrap(), "some: foo"); let none = OptionTemplate { var: None }; - assert_eq!(none.render(), "none"); + assert_eq!(none.render().unwrap(), "none"); } @@ -138,7 +138,7 @@ struct GenericsTemplate<T: std::fmt::Display, U = u8> #[test] fn test_generics() { let t = GenericsTemplate { t: "a", u: 42 }; - assert_eq!(t.render(), "a42"); + assert_eq!(t.render().unwrap(), "a42"); } @@ -152,5 +152,5 @@ struct JsonTemplate<'a> { #[test] fn test_json() { let t = JsonTemplate { foo: "a", bar: "b" }; - assert_eq!(t.render(), "{\"foo\": \"a\", \"bar\": \"b\"}"); + assert_eq!(t.render().unwrap(), "{\"foo\": \"a\", \"bar\": \"b\"}"); } |