-
Notifications
You must be signed in to change notification settings - Fork 516
/
custom_filters.rs
30 lines (25 loc) · 1.01 KB
/
custom_filters.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
use std::collections::HashMap;
use tera::{Result, Value};
type Filter = fn(&Value, &HashMap<String, Value>) -> Result<Value>;
pub static CUSTOM_FILTERS: &[(&str, Filter)] = &[("to_hex", to_hex), ("snake_case", snake_case)];
pub fn to_hex(value: &Value, _args: &HashMap<String, Value>) -> Result<Value> {
let Some(value) = value.as_u64() else {
return Err(tera::Error::call_filter(
"to_hex filter expects an unsigned integer",
"serde_json::value::Value::as_u64",
));
};
Ok(serde_json::Value::String(format!("{:x}", value)))
}
pub fn snake_case(value: &Value, _args: &HashMap<String, Value>) -> Result<Value> {
let Some(value) = value.as_str() else {
return Err(tera::Error::call_filter(
"snake_case filter expects a string",
"serde_json::value::Value::as_str",
));
};
Ok(serde_json::Value::String(
// slug is the same dependency tera uses for its builtin 'slugify'
slug::slugify(value).replace('-', "_"),
))
}