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
|
use serde::Serialize;
use std::fs;
use crate::queries;
#[derive(Debug, Serialize)]
pub enum Header {
Payments,
Categories,
Incomes,
Balance,
Statistics,
}
pub fn get() -> Result<minijinja::Environment<'static>, String> {
let mut env = minijinja::Environment::new();
for path in read_files_recursive("templates") {
let path = path
.to_str()
.ok_or("Error getting string of path: {path:?}")?
.to_string();
let content = fs::read_to_string(&path)
.map_err(|_| "Error reading template {path}")?;
let path_without_prefix = path
.strip_prefix("templates/")
.ok_or("Error removing prefix from template path")?
.to_string();
env.add_template_owned(path_without_prefix, content)
.map_err(|_| "Error adding template {path} to environment")?;
}
env.add_function("payments_params", payments_params);
env.add_function("pluralize", pluralize);
env.add_function("now", now);
env.add_filter("numeric", numeric);
env.add_filter("euros", euros);
env.add_filter("round", round);
env.add_filter("with_param", with_param);
env.add_filter("filter", filter);
Ok(env)
}
fn read_files_recursive(
path: impl AsRef<std::path::Path>,
) -> Vec<std::path::PathBuf> {
let Ok(entries) = fs::read_dir(path) else {
return vec![];
};
entries
.flatten()
.flat_map(|entry| {
let Ok(meta) = entry.metadata() else {
return vec![];
};
if meta.is_dir() {
return read_files_recursive(entry.path());
}
if meta.is_file() {
return vec![entry.path()];
}
vec![]
})
.collect()
}
fn payments_params(value: minijinja::Value) -> String {
let str = value.to_string().replace("none", "null");
match serde_json::from_str(&str) {
Ok(q) => queries::payments_url(q),
Err(err) => {
log::error!("Error parsing payments params {}: {:?}", str, err);
"".to_string()
}
}
}
fn now(format: &str) -> String {
let date = chrono::Local::now();
format!("{}", date.format(format))
}
fn euros(n: i64) -> String {
let str = rgrouped(n.abs().to_string(), 3).join(" ");
let sign = if n < 0 { "-" } else { "" };
format!("{}{} €", sign, str)
}
fn numeric(n: i64) -> String {
let str = rgrouped(n.abs().to_string(), 3).join(" ");
let sign = if n < 0 { "-" } else { "" };
format!("{}{}", sign, str)
}
fn pluralize(n: i32, s: String) -> String {
if n > 0 {
format!("{s}s")
} else {
s
}
}
fn round(n: f32) -> i32 {
n.round() as i32
}
fn with_param(url: String, key: String, value: String) -> String {
if url.contains("?") {
format!("{url}&{key}={value}")
} else {
format!("{url}?{key}={value}")
}
}
fn filter(
xs: Vec<minijinja::Value>,
key: &str,
value: String,
) -> Vec<minijinja::Value> {
let mut res = vec![];
for x in xs {
if let Ok(v) = x.get_attr(key) {
if let Some(v) = v.as_str() {
if v == value {
res.push(x);
}
}
}
}
res
}
fn rgrouped(str: String, n: usize) -> Vec<String> {
let mut str = str;
let mut l = str.len();
let mut res = vec![];
while l > n {
let str2 = str.clone();
let (start, end) = str2.split_at(l - n);
l -= n;
str = start.to_string();
res.push(end.to_string());
}
if !str.is_empty() {
res.push(str);
}
res.reverse();
res
}
|