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
//! # Tide-Tera integration This crate exposes [an extension
//! trait](TideTeraExt) that adds two methods to [`tera::Tera`]:
//! [`render_response`](TideTeraExt::render_response) and
//! [`render_body`](TideTeraExt::render_body). It also adds a
//! convenience [`context`] macro for creating ad-hoc tera
//! [`Context`](tera::Context)s.

use std::path::PathBuf;
use tera::{Context, Tera};
use tide::{http::Mime, Body, Response, Result};

/// This extension trait adds two methods to [`tera::Tera`]:
/// [`render_response`](TideTeraExt::render_response) and
/// [`render_body`](TideTeraExt::render_body)
pub trait TideTeraExt {
    /// `render_body` returns a fully-rendered [`tide::Body`] with mime
    /// type set based on the template name file extension using the
    /// logic at [`tide::http::Mime::from_extension`]. This will
    /// return an `Err` variant if the render was unsuccessful.
    ///
    /// ```rust
    /// use tide_tera::prelude::*;
    /// let tera = tera::Tera::new("tests/templates/**/*").unwrap();
    /// let response = tera
    ///     .render_response("good_template.html", &context! { "name" => "tide" })
    ///     .unwrap();
    /// assert_eq!(response.content_type(), Some(tide::http::mime::HTML));
    ///```
    fn render_response(&self, template_name: &str, context: &Context) -> Result;
    /// `render_response` returns a tide Response with a body rendered
    /// with [`render_body`](TideTeraExt::render_body). This will
    /// return an `Err` variant if the render was unsuccessful.
    ///
    /// ```rust
    /// use tide_tera::prelude::*;
    /// let tera = tera::Tera::new("tests/templates/**/*").unwrap();
    /// let body = tera
    ///     .render_body("good_template.html", &context! { "name" => "tide" })
    ///     .unwrap();
    /// assert_eq!(body.mime(), &tide::http::mime::HTML);
    ///```
    fn render_body(&self, template_name: &str, context: &Context) -> Result<Body>;
}

impl TideTeraExt for Tera {
    fn render_body(&self, template_name: &str, context: &Context) -> Result<Body> {
        let string = self.render(template_name, context)?;
        let mut body = Body::from_string(string);

        let path = PathBuf::from(template_name);
        if let Some(extension) = path.extension() {
            if let Some(mime) = Mime::from_extension(extension.to_string_lossy()) {
                body.set_mime(mime)
            }
        }

        Ok(body)
    }

    fn render_response(&self, template_name: &str, context: &tera::Context) -> Result {
        let mut response = Response::new(200);
        response.set_body(self.render_body(template_name, context)?);
        Ok(response)
    }
}

/// this macro simplifies creation of ad-hoc [`tera::Context`]s.
/// ```rust
/// # use tide_tera::context;
/// let mut context = tera::Context::new();
/// context.insert("template-engine", "tera");
/// assert_eq!(context, context! { "template-engine" => "tera" });
/// ```
#[macro_export]
macro_rules! context {
    ($($key:expr => $value:expr,)+) => { context!($($key => $value),+) };
    ($($key:expr => $value:expr),*) => {
        {
            let mut _context = ::tera::Context::new();
            $(
                _context.insert($key, &$value);
            )*
            _context
        }
     };
}

pub mod prelude {
    //! exposes [`context`] and [`TideTeraExt].
    pub use super::{context, TideTeraExt};
}

#[cfg(test)]
mod tests {
    use super::*;
    use async_std::prelude::*;

    #[test]
    fn context() {
        let context = context! {
            "key" => "value"
        };

        assert_eq!(context.into_json()["key"], "value");

        let context = context! { "key1" => "value1", "key2" => "value2" };
        assert_eq!(context.into_json()["key2"], "value2");
    }

    #[async_std::test]
    async fn test_body() {
        let tera = Tera::new("tests/templates/**/*").unwrap();
        let mut body = tera
            .render_body("good_template.html", &context! { "name" => "tide" })
            .unwrap();

        assert_eq!(body.mime(), &tide::http::mime::HTML);

        let mut body_string = String::new();
        body.read_to_string(&mut body_string).await.unwrap();
        assert_eq!(body_string, "hello tide!\n");
    }

    #[async_std::test]
    async fn response() {
        let tera = Tera::new("tests/templates/**/*").unwrap();
        let mut response = tera
            .render_response("good_template.html", &context! { "name" => "tide" })
            .unwrap();

        assert_eq!(response.content_type(), Some(tide::http::mime::HTML));

        let http_response: &mut tide::http::Response = response.as_mut();
        let body_string = http_response.body_string().await.unwrap();
        assert_eq!(body_string, "hello tide!\n");
    }

    #[test]
    fn unknown_content_type() {
        let tera = Tera::new("tests/templates/**/*").unwrap();
        let body = tera
            .render_body("unknown_extension.tide", &context! { "name" => "tide" })
            .unwrap();

        assert_eq!(body.mime(), &tide::http::mime::PLAIN);
    }

    #[test]
    fn no_extension() {
        let tera = Tera::new("tests/templates/**/*").unwrap();
        let body = tera
            .render_body("no_extension", &context! { "name" => "tide" })
            .unwrap();

        assert_eq!(body.mime(), &tide::http::mime::PLAIN);
    }

    #[test]
    fn bad_template() {
        let tera = Tera::new("tests/templates/**/*").unwrap();
        let result = tera.render_body("good_template.html", &context! { "framework" => "tide" });

        assert!(result.is_err());
    }
}