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
use gtmpl::{Context, Template};
use gtmpl_value::Value;
pub mod math;
pub mod range;
pub mod select;
pub mod string;
pub mod svg;
pub mod exec;
pub trait THelper: Sized {
fn push_helper(self, fname: &str, f: fn(&[Value]) -> Result<Value, String>) -> Self;
fn render_me(&self, c: &Context) -> Result<String, String>;
fn q_render<V: Into<Value>>(&self, v: V) -> Result<String, String> {
let c = Context::from(Value::from(v))?;
self.render_me(&c)
}
fn with_math(self) -> Self {
self.push_helper("add", math::add)
.push_helper("mul", math::mul)
.push_helper("sub", math::sub)
.push_helper("div", math::div)
.push_helper("idiv", math::idiv)
.push_helper("mod", math::imod)
}
fn with_range(self) -> Self {
self.push_helper("is_list", range::is_list)
.push_helper("as_list", range::as_list)
.push_helper("safe_len", range::safe_len)
.push_helper("num_range", range::num_range)
.push_helper("sort_on", range::sort_on)
.push_helper("to_json", range::to_json)
}
fn with_select(self) -> Self {
self.push_helper("b_sel", select::b_sel)
.push_helper("match", select::v_match)
.push_helper("has", select::has)
.push_helper("first", select::first)
}
fn with_string(self) -> Self {
self.push_helper("ccat", string::ccat)
.push_helper("sep", string::sep)
.push_helper("wrap", string::wrap)
.push_helper("markdown", string::markdown)
}
fn with_svg(self) -> Self {
self.push_helper("xywh", svg::xywh)
.push_helper("xy", svg::xy)
.push_helper("fl_stk", svg::fl_stk)
.push_helper("font", svg::font)
.push_helper("xml_es", svg::xml_es)
}
#[deprecated(since = "0.1.2", note = "please use `with_defaults` instead")]
fn with_all(self) -> Self {
self.with_defaults()
}
fn with_defaults(self) -> Self {
self.with_string()
.with_math()
.with_range()
.with_select()
.with_svg()
}
fn with_exec(self) -> Self {
self.push_helper("exec", exec::exec)
}
}
impl THelper for Template {
fn push_helper(mut self, fname: &str, f: fn(&[Value]) -> Result<Value, String>) -> Self {
self.add_func(fname, f);
self
}
fn render_me(&self, c: &Context) -> Result<String, String> {
self.render(c)
}
}