aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2017-08-23 16:32:07 +0200
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2017-08-23 20:50:52 +0200
commitbf51e7264fb4273672430f74ce8c2b16a74d1c38 (patch)
treef714a0eeb130eb126743a117fa63b6d549d6c09a
parentd8041c00fee819af3b0d831bd2521b4cdd78d584 (diff)
downloadaskama-bf51e7264fb4273672430f74ce8c2b16a74d1c38.tar.gz
askama-bf51e7264fb4273672430f74ce8c2b16a74d1c38.tar.bz2
askama-bf51e7264fb4273672430f74ce8c2b16a74d1c38.zip
Add optional support for Rocket
Diffstat (limited to '')
-rw-r--r--askama/Cargo.toml2
-rw-r--r--askama/src/lib.rs8
-rw-r--r--askama_derive/Cargo.toml1
-rw-r--r--askama_derive/src/generator.rs24
4 files changed, 35 insertions, 0 deletions
diff --git a/askama/Cargo.toml b/askama/Cargo.toml
index f96b939..62d4fec 100644
--- a/askama/Cargo.toml
+++ b/askama/Cargo.toml
@@ -18,6 +18,7 @@ travis-ci = { repository = "djc/askama" }
[dependencies]
askama_derive = { path = "../askama_derive", version = "0.3.4" }
error-chain = "0.10"
+rocket = { version = "0.3", optional = true }
serde = { version = "1.0", optional = true }
serde_json = { version = "1.0", optional = true }
iron = { version = "0.5", optional = true }
@@ -26,6 +27,7 @@ iron = { version = "0.5", optional = true }
serde-json = ["serde", "serde_json"]
default = []
with-iron = ["iron", "askama_derive/iron"]
+with-rocket = ["rocket", "askama_derive/rocket"]
[package.metadata.docs.rs]
features = [ "serde-json" ]
diff --git a/askama/src/lib.rs b/askama/src/lib.rs
index 4d9b2fb..70202f5 100644
--- a/askama/src/lib.rs
+++ b/askama/src/lib.rs
@@ -250,6 +250,14 @@ pub mod iron {
pub use self::iron::response::Response;
}
+#[cfg(feature = "with-rocket")]
+pub mod rocket {
+ extern crate rocket;
+ pub use self::rocket::http::{ContentType, Status};
+ pub use self::rocket::request::Request;
+ pub use self::rocket::response::{Responder, Response};
+}
+
// Duplicates askama_derive::path::template_dir()
fn template_dir() -> PathBuf {
let mut path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
diff --git a/askama_derive/Cargo.toml b/askama_derive/Cargo.toml
index 40f1ad5..8016b37 100644
--- a/askama_derive/Cargo.toml
+++ b/askama_derive/Cargo.toml
@@ -14,6 +14,7 @@ proc-macro = true
[features]
default = []
iron = []
+rocket = []
[dependencies]
nom = "3"
diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs
index 3595a84..1d8e8f2 100644
--- a/askama_derive/src/generator.rs
+++ b/askama_derive/src/generator.rs
@@ -56,6 +56,9 @@ pub fn generate(ast: &syn::DeriveInput, path: &Path, mut nodes: Vec<Node>) -> St
if cfg!(feature = "iron") {
gen.impl_modifier_response(ast);
}
+ if cfg!(feature = "rocket") {
+ gen.impl_responder(ast, path);
+ }
gen.result()
}
@@ -645,6 +648,27 @@ impl<'a> Generator<'a> {
self.writeln("}");
}
+ // Implement Rocket's `Responder`.
+ fn impl_responder(&mut self, ast: &syn::DeriveInput, path: &Path) {
+ self.write_header(ast, "::askama::rocket::Responder<'r>", &vec!["'r"]);
+ self.writeln("fn respond_to(self, _: &::askama::rocket::Request) \
+ -> ::std::result::Result<\
+ ::askama::rocket::Response<'r>, ::askama::rocket::Status> {");
+ let ext = match path.extension() {
+ Some(s) => s.to_str().unwrap(),
+ None => "txt",
+ };
+ self.writeln("::askama::rocket::Response::build()");
+ self.indent();
+ self.writeln(&format!(".header(::askama::rocket::ContentType::from_extension({:?})\
+ .unwrap())", ext));
+ self.writeln(".sized_body(::std::io::Cursor::new(self.render().unwrap()))");
+ self.writeln(".ok()");
+ self.dedent();
+ self.writeln("}");
+ self.writeln("}");
+ }
+
fn result(self) -> String {
self.buf
}