rocket_include_tera/lib.rs
1/*!
2# Include Tera Templates for Rocket Framework
3
4This is a crate which provides macros `tera_resources_initialize!` and `tera_response!` to statically include Tera files from your Rust project and make them be the HTTP response sources quickly.
5
6## Example
7
8```rust,ignore
9#[macro_use]
10extern crate rocket;
11
12#[macro_use]
13extern crate rocket_include_tera;
14
15use std::collections::HashMap;
16
17use rocket::State;
18
19use rocket_include_tera::{EtagIfNoneMatch, TeraContextManager, TeraResponse};
20
21#[get("/")]
22fn index(tera_cm: &State<TeraContextManager>, etag_if_none_match: EtagIfNoneMatch) -> TeraResponse {
23 let mut map = HashMap::new();
24
25 map.insert("title", "Title");
26 map.insert("body", "Hello, world!");
27
28 tera_response!(tera_cm, etag_if_none_match, "index", map)
29}
30
31#[launch]
32fn rocket() -> _ {
33 rocket::build()
34 .attach(tera_resources_initializer!(
35 "index" => "views/index.tera",
36 ))
37 .mount("/", routes![index])
38}
39```
40
41* `tera_resources_initialize!` is used in the fairing of `TeraResponse` to include Tera files into your executable binary file. You need to specify each file's name and its path relative to the directory containing the manifest of your package. In order to reduce the compilation time and allow to hot-reload templates, files are compiled into your executable binary file together, only when you are using the **release** profile.
42* `tera_response!` is used for retrieving and rendering the file you input through the macro `tera_resources_initialize!` as a `TeraResponse` instance with rendered HTML. When its `respond_to` method is called, three HTTP headers, **Content-Type**, **Content-Length** and **Etag**, will be automatically added, and the rendered HTML can optionally not be minified.
43* `tera_response_cache!` is used for wrapping a `TeraResponse` and its constructor, and use a **key** to cache its HTML and ETag in memory. The cache is generated only when you are using the **release** profile.
44* `tera_resources_initializer!` is used for generating a fairing for tera resources.
45*/
46
47#[macro_use]
48extern crate educe;
49
50#[doc(hidden)]
51pub extern crate manifest_dir_macros;
52
53mod functions;
54
55#[cfg(debug_assertions)]
56mod debug;
57
58#[cfg(not(debug_assertions))]
59mod release;
60
61mod macros;
62
63#[cfg(debug_assertions)]
64pub use debug::*;
65#[cfg(not(debug_assertions))]
66pub use release::*;
67pub use rocket_etag_if_none_match::{EtagIfNoneMatch, entity_tag::EntityTag};
68
69const DEFAULT_CACHE_CAPACITY: usize = 64;