1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
//! Please contact me on github and file any issues if you find some, I'm also open to PRs or other suggestions.
//!
//! Updated to Tera 0.11 / Serde 1.0 / Iron 0.5 !
//! Serde 1.0 to_value returns a `Result`, this means you need to handle the possiblity of a serialization failure.
//! If you just want to `unwrap()` there is an implementation of From<Value> for TemplateMode so `Template::new(path, value)`
//! works also. This is the implementation that's on 'stable'.
//!
//! You can try the unstable crate feature which uses `TryFrom`/`TryInto` to make `Template::new` polymorphic over `Context` and `Value`.
//!
//! **update iron-tera-0.5.0**: If you build this crate with feature = "unstable" on the nightly compiler,
//! I've included a `TryFrom` impl to improve API ergonomics.
//!
//! ## Examples
//! Full examples ON GITHUB for both stable and unstable.
//!
//! ```ignore
//!   [dependencies]
//!   iron-tera = { version = "0.5.0" }  # optional:  features = [ "unstable" ]
//! ```
//!
//! Using `iron-tera` stable.
//!
//! ```ignore
//!     extern crate tera;
//!     extern crate iron;
//!     extern crate router;
//!     #[macro_use] extern crate serde_json;
//!     #[macro_use] extern crate serde_derive;
//!     extern crate iron_tera;
//!
//! fn main() {
//!     use iron::prelude::*;
//!     use iron::status;
//!     use router::Router;
//!     use tera::Context;
//!
//!     use iron_tera::{Template, TeraEngine};
//!
//!     let mut router = Router::new();
//!     router.get("/context", context_handler, "context");
//!     router.get("/json", json_handler, "json");
//!
//!     let mut chain = Chain::new(router);
//!     let teng = TeraEngine::new("src/examples/templates/**/*");
//!     chain.link_after(teng);
//!
//!     Iron::new(chain).http("localhost:5000").unwrap();
//!
//!
//!     fn context_handler(_: &mut Request) -> IronResult<Response> {
//!         let mut resp = Response::new();
//!
//!         let mut context = Context::new();
//!         context.add("username", &"Bob");
//!         context.add("my_var", &"Thing"); // comment out to see alternate thing
//!         context.add("numbers", &vec![1, 2, 3]);
//!         context.add("bio", &"<script>alert('pwnd');</script>");
//!
//!         // can use Template::new(path, TemplateMode::from_context(context)) or TemplateMode::from(context) also
//!         resp.set_mut(Template::new("users/profile.html", context))
//!             .set_mut(status::Ok);
//!         Ok(resp)
//!     }
//!     fn json_handler(_: &mut Request) -> IronResult<Response> {
//!         let mut resp = Response::new();
//!
//!         let blob = json!({
//!             "username": "John Doe",
//!             "my_var": "Thing",
//!             "numbers": [
//!                 "1",
//!                 "+44 2345678",
//!                 "3"
//!             ],
//!             "bio": "<script>alert('pwnd');</script>"
//!          });
//!         // you can use blob.try_into() and handle the `Result` explicitly (not shown here)
//!         // on the `unstable` feature of `iron-tera`
//!         resp.set_mut(Template::new("users/profile.html", blob))
//!             .set_mut(status::Ok);
//!         Ok(resp)
//!     }
//! }
//! ```
//!
//! Creating a template from a struct
//!
//! ```ignore
//! // The following uses serde's Serialize
//! #[derive(Serialize)]
//! struct Product {
//!     name: String,
//!     value: i32,
//! }
//! // Rendering from a struct that implements Serialize
//! fn produce_handler(_: &mut Request) -> IronResult<Response> {
//!     let mut resp = Response::new();
//!
//!     // Using serialized values
//!     let product = Product {
//!         name: "Foo".into(),
//!         value: 42,
//!     };
//!     // You can use TemplateMode::from()
//!     resp.set_mut(Template::new("product.html", product))
//!         .set_mut(status::Ok);
//!     Ok(resp)
//! }
//! ```
#![cfg_attr(feature = "unstable", feature(try_from))]
#![allow(dead_code)]
#[macro_use]
extern crate serde_json;
#[macro_use]
extern crate tera;
extern crate iron;
extern crate plugin;
extern crate serde;

use iron::headers::ContentType;
use iron::modifier::Modifier;
use iron::prelude::*;
use iron::{status, typemap, AfterMiddleware};

use plugin::Plugin;

use serde::ser::Serialize;
use serde_json::{to_value, Value};

use std::convert::{TryFrom, TryInto};
use std::error::Error;

use tera::{Context, Tera};

/// There are 2 main ways to pass data to generate a template.
#[derive(Clone, Debug)]
pub enum TemplateMode {
    /// TeraContext constructor takes a `Context`
    TeraContext(Context),
    /// Serialized constructor takes a `Value` from `serde_json`
    Serialized(Value),
}

/// TemplateMode should only ever be created from these smart constructors,
/// not with the enums type constructors.
impl TemplateMode {
    pub fn from_context(context: Context) -> TemplateMode {
        TemplateMode::TeraContext(context)
    }

    pub fn from_serial<S: Serialize>(serializeable: S) -> Result<TemplateMode, TemplateError> {
        Ok(TemplateMode::Serialized(to_value(serializeable)?))
    }
}
#[cfg(not(feature = "unstable"))]
impl From<Context> for TemplateMode {
    fn from(context: Context) -> Self {
        TemplateMode::from_context(context)
    }
}

#[cfg(not(feature = "unstable"))]
impl From<Value> for TemplateMode {
    fn from(serializeable: Value) -> Self {
        TemplateMode::from_serial(serializeable).unwrap()
    }
}
#[derive(Debug)]
pub enum TemplateError {
    SerdeErr(serde_json::Error),
    ContextErr(),
}

impl From<serde_json::Error> for TemplateError {
    fn from(e: serde_json::Error) -> TemplateError {
        TemplateError::SerdeErr(e)
    }
}
impl ::std::fmt::Display for TemplateError {
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
        match *self {
            TemplateError::SerdeErr(ref e) => write!(f, "Serde Error: {}", e),
            TemplateError::ContextErr() => write!(f, "Context Error"),
        }
    }
}

impl Error for TemplateError {
    fn description(&self) -> &str {
        match *self {
            TemplateError::SerdeErr(ref e) => e.description(),
            TemplateError::ContextErr() => "Context Error",
        }
    }
    fn cause(&self) -> Option<&Error> {
        None
    }
}

#[cfg(feature = "unstable")]
impl TryFrom<Value> for TemplateMode {
    type Error = TemplateError;
    fn try_from(serialize: Value) -> Result<Self, Self::Error> {
        TemplateMode::from_serial(serialize)
    }
}

#[cfg(feature = "unstable")]
impl TryFrom<Context> for TemplateMode {
    type Error = TemplateError;
    fn try_from(serialize: Context) -> Result<Self, Self::Error> {
        Ok(TemplateMode::from_context(serialize))
    }
}

/// Our template holds a name (path to template) and a mode (constructed with `from_context` or `from_serial`)
#[derive(Clone, Debug)]
pub struct Template {
    mode: TemplateMode,
    name: String,
}

impl Template {
    #[cfg(not(feature = "unstable"))]
    pub fn new<T: Into<TemplateMode>, S: Into<String>>(name: S, mode: T) -> Template {
        Template {
            name: name.into(),
            mode: mode.into(),
        }
    }
    #[cfg(feature = "unstable")]
    pub fn new<T, S>(name: S, mode: T) -> Result<Template, TemplateError>
    where
        TemplateError: From<<T as TryInto<TemplateMode>>::Error>,
        S: Into<String>,
        T: TryInto<TemplateMode>,
    {
        let mode = mode.try_into()?;
        Ok(Template {
            name: name.into(),
            mode,
        })
    }
}

/// TeraEngine holds the Tera struct so that it can be used by many handlers without explicitly passing
pub struct TeraEngine {
    pub tera: Tera,
}

/// `compile_templates!` is used to parse the contents of a dir for all templates.
impl TeraEngine {
    /// Take a `String` and convert to a slice
    pub fn new<S: AsRef<str>>(dir: S) -> TeraEngine {
        TeraEngine {
            tera: compile_templates!(dir.as_ref()),
        }
    }
}

impl typemap::Key for TeraEngine {
    type Value = Template;
}

impl Modifier<Response> for Template {
    fn modify(self, resp: &mut Response) {
        resp.extensions.insert::<TeraEngine>(self);
    }
}

/// The middleware implementation for TeraEngine
impl AfterMiddleware for TeraEngine {
    /// This is where all the magic happens. We extract `TeraEngine` from Iron's `Response`,
    /// determine what `TemplateMode` we should render in, and pass the appropriate values to
    /// tera's render methods.
    fn after(&self, _: &mut Request, mut resp: Response) -> IronResult<Response> {
        let wrapper = resp.extensions
            .remove::<TeraEngine>()
            .and_then(|t| match t.mode {
                TemplateMode::TeraContext(ref context) => Some(self.tera.render(&t.name, context)),
                TemplateMode::Serialized(ref value) => Some(self.tera.render(&t.name, value)),
            });
        match wrapper {
            Some(result) => result
                .map_err(|e| IronError::new(e, status::InternalServerError))
                .and_then(|page| {
                    if !resp.headers.has::<ContentType>() {
                        resp.headers.set(ContentType::html());
                    }
                    resp.set_mut(page);
                    Ok(resp)
                }),
            None => Ok(resp),
        }
    }

    fn catch(&self, req: &mut Request, mut err: IronError) -> IronResult<Response> {
        err.response = self.after(req, err.response)?;
        Err(err)
    }
}

impl Plugin<Response> for TeraEngine {
    type Error = ();

    fn eval(resp: &mut Response) -> Result<Template, ()> {
        match resp.extensions.get::<TeraEngine>() {
            Some(t) => Ok(t.clone()),
            None => Err(()),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{Template, TemplateMode, TeraEngine};
    use iron::prelude::*;
    use tera::Context;

    fn from_context_response() -> IronResult<Response> {
        let resp = Response::new();
        let mut context = Context::new();
        context.add("greeting", &"hi!");
        Ok(resp.set(Template::new("./test_template/users/foo.html", context)))
    }

    #[test]
    fn test_from_context() {
        let mut resp = from_context_response().ok().expect("response expected");
        match resp.get::<TeraEngine>() {
            Ok(h) => {
                assert_eq!(h.name, "./test_template/users/foo.html".to_string());
                if let TemplateMode::TeraContext(context) = h.mode {
                    assert_eq!(
                        context
                            .as_json()
                            .unwrap()
                            .get("greeting")
                            .unwrap()
                            .as_str()
                            .unwrap(),
                        "hi!"
                    );
                } else {
                    panic!("TeraContext expected");
                }
            }
            _ => panic!("template expected"),
        }
    }
}