atom_engine/filters/
encoding.rs1use serde_json::Value;
2use std::collections::HashMap;
3use tera::Error;
4
5use super::FilterResult;
6
7pub fn json_decode(value: &Value, _: &HashMap<String, Value>) -> FilterResult {
8 let s = value
9 .as_str()
10 .ok_or_else(|| Error::msg("Expected string"))?;
11 serde_json::from_str(s).map_err(|e| Error::msg(e.to_string()))
12}
13
14pub fn urlescape(value: &Value, _: &HashMap<String, Value>) -> FilterResult {
15 if let Some(s) = value.as_str() {
16 Ok(Value::String(urlencoding::encode(s).to_string()))
17 } else {
18 Ok(value.clone())
19 }
20}
21
22pub fn urlunescape(value: &Value, _: &HashMap<String, Value>) -> FilterResult {
23 if let Some(s) = value.as_str() {
24 Ok(Value::String(
25 urlencoding::decode(s)
26 .map_err(|e| Error::msg(e.to_string()))?
27 .to_string(),
28 ))
29 } else {
30 Ok(value.clone())
31 }
32}
33
34pub fn strip_tags(value: &Value, _: &HashMap<String, Value>) -> FilterResult {
35 if let Some(s) = value.as_str() {
36 let re = regex::Regex::new(r"<[^>]*>").unwrap();
37 Ok(Value::String(re.replace_all(s, "").to_string()))
38 } else {
39 Ok(value.clone())
40 }
41}
42
43pub fn base64_encode(value: &Value, _: &HashMap<String, Value>) -> FilterResult {
44 if let Some(s) = value.as_str() {
45 Ok(Value::String(base64::Engine::encode(
46 &base64::engine::general_purpose::STANDARD,
47 s,
48 )))
49 } else {
50 Ok(value.clone())
51 }
52}
53
54pub fn base64_decode(value: &Value, _: &HashMap<String, Value>) -> FilterResult {
55 if let Some(s) = value.as_str() {
56 let decoded = base64::Engine::decode(&base64::engine::general_purpose::STANDARD, s)
57 .map_err(|e| Error::msg(e.to_string()))?;
58 Ok(Value::String(
59 String::from_utf8(decoded).map_err(|e| Error::msg(e.to_string()))?,
60 ))
61 } else {
62 Ok(value.clone())
63 }
64}