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
//! Provides standard string manipulation and utility functions.
//!
//! See [`std_fun`](rubble-templates::std_fun) or [`string_functions`] for more info.
//!
use std::collections::HashMap;
use rubble_templates_core::evaluator::Function;
use rubble_templates_core::functions::SimpleFunction;

pub const EMPTY_STRING: &str = "";

/// Provides a set of string related functions.
///
/// Available functions:
/// * [`concat`](concat_function) - Concatenates parameters.
/// * [`trim`](trim_function) - Trims parameters.
/// * [`$}`](right_brackets_function) - Inserts "}}".
/// * [`$quote`](right_brackets_function) - Inserts double qoutes (").
pub fn string_functions() -> HashMap<String, Box<dyn Function>> {
    let mut functions: HashMap<String, Box<dyn Function>> = HashMap::new();
    functions.insert("concat".to_string(), SimpleFunction::new(concat_function));
    functions.insert("trim".to_string(), SimpleFunction::new(trim_function));
    functions.insert("$}".to_string(), SimpleFunction::new(right_brackets_function));
    functions.insert("$quote".to_string(), SimpleFunction::new(quotes_function));
    functions
}

/// Concatenates the parameters.
///
/// Eg.
/// ```text
/// concat 1 "hello" " " 3.14 "world!"
/// ```
/// Expected output:
/// ```text
/// 1hello 3.14world!
/// ```
pub fn concat_function(parameters: &[String]) -> String {
    let mut result = EMPTY_STRING.to_string();
    parameters.iter().for_each(|param| {
        result.push_str(param);
    });
    result
}

/// Trims the parameters.
/// If there is more than one parameter, then concatenates them.
///
/// Eg.
/// ```text
/// trim "  hello"
/// trim " hello " "   world" "text  "
/// ```
/// Expected output:
/// ```text
/// hello
/// helloworldtext
/// ```
pub fn trim_function(parameters: &[String]) -> String {
    let mut result = EMPTY_STRING.to_string();
    parameters.iter().for_each(|param| {
        result.push_str(param.trim());
    });
    result
}

/// Inserts "}}". Ignores the parameters.
///
/// Eg.
/// ```text
/// $}}
/// $}} " hello " "   world" "text  "
/// ```
/// Expected output:
/// ```text
/// }}
/// }}
/// ```
pub fn right_brackets_function(_: &[String]) -> String {
    "}}".to_string()
}

/// Inserts ". Ignores the parameters.
///
/// Eg.
/// ```text
/// $quote
/// $quote " hello " "   world" "text  "
/// ```
/// Expected output:
/// ```text
/// "
/// "
/// ```
pub fn quotes_function(_: &[String]) -> String {
    "\"".to_string()
}