-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
liquid.rs
113 lines (103 loc) · 3.72 KB
/
liquid.rs
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
/*
MANDY PROCESSORS by Alexander Abraham a.k.a. "Angel Dollface".
Licensed under the MIT license.
*/
/// Importing the
/// "object" macro
/// from the "liquid"
/// crate.
use liquid::object;
/// Importing Mandy's error
/// struct.
use merrors::MandyError;
/// Importing the structure representing
/// a Mandy site's context.
use context::SiteContext;
/// Importing Liquid's
/// standard "ParserBuilder"
/// entity.
use liquid::ParserBuilder;
/// Importing Rust's standard
/// "HashMap" API.
use std::collections::HashMap;
/// Importing the "EagerCompiler"
/// structure from the "liquid" crate.
use liquid::partials::EagerCompiler;
/// Importing the "InMemorySource"
/// structure from the "liquid" crate.
use liquid::partials::InMemorySource;
/// Renders a string written in Liquid with a context
/// and returns a string or an error.
pub fn render_template_no_partials(
liquid_string: &String,
context: &SiteContext
) -> Result<String, MandyError> {
let liquid_parser = match ParserBuilder::with_stdlib().build() {
Ok(liquid_parser) => liquid_parser,
Err(e) => {return Err::<String, MandyError>(MandyError::new(&e.to_string()));}
};
let liquid_template = match liquid_parser.parse(&liquid_string){
Ok(liquid_template) => liquid_template,
Err(e) => {
return Err::<String, MandyError>(MandyError::new(&e.to_string()));}
};
let globals = object!(context);
let output: String = match liquid_template.render(&globals){
Ok(output) => output,
Err(render_error) => {return Err::<String, MandyError>(MandyError::new(&render_error.to_string()));}
};
return Ok(output);
}
/// Renders a string written in Liquid with a context and
/// partials and returns a string or an error.
pub fn render_template_partials(
liquid_string: &String,
context: &SiteContext,
partials: &HashMap<String, String>
) -> Result<String, MandyError>{
type Partials = EagerCompiler<InMemorySource>;
let mut partial_source = Partials::empty();
for (k,v) in partials.into_iter() {
partial_source.add(k,v);
}
let liquid_parser = match ParserBuilder::with_stdlib().partials(partial_source).build() {
Ok(liquid_parser) => liquid_parser,
Err(e) => {return Err::<String, MandyError>(MandyError::new(&e.to_string()));}
};
let liquid_template = match liquid_parser.parse(&liquid_string){
Ok(liquid_template) => liquid_template,
Err(e) => {return Err::<String, MandyError>(MandyError::new(&e.to_string()));}
};
let globals = object!(context);
let output: String = match liquid_template.render(&globals){
Ok(output) => output,
Err(render_error) => {return Err::<String, MandyError>(MandyError::new(&render_error.to_string()));}
};
return Ok(output);
}
/// Renders a string written in Liquid with a context
/// and returns a string or an error.
pub fn render_template(
liquid_string: &String,
context: &SiteContext,
partials: &Option<HashMap<String, String>>
) -> Result<String, MandyError> {
let result;
match partials {
Some(map) => {
let output: String = match render_template_partials(liquid_string, context, map){
Ok(output) => output,
Err(render_error) => {return Err::<String, MandyError>(MandyError::new(&render_error.to_string()));}
};
result = output;
},
None => {
let output: String = match render_template_no_partials(liquid_string, context){
Ok(output) => output,
Err(render_error) => {return Err::<String, MandyError>(MandyError::new(&render_error.to_string()));}
};
result = output;
}
};
return Ok(result);
}