srtemplate/builtin/
text.rs

1use crate::prelude::FuncResult;
2use crate::template::validations;
3
4/// Convert strings to lowercase.
5///
6/// This function takes a slice of strings and converts each string to lowercase.
7/// It then concatenates the converted strings into a single string, separated by space.
8///
9/// # Arguments
10///
11/// * `args`: A slice of strings to be converted to lowercase.
12///
13/// # Returns
14///
15/// * A [`FuncResult`] containing the concatenated lowercase strings.
16///
17/// # Errors
18///
19/// This function can return an error of [`crate::function::FunctionError`] variant:
20/// - `FunctionError::InvalidArgument` if there are insufficient input arguments.
21#[cfg_attr(docsrs, doc(cfg(feature = "text")))]
22#[cfg(feature = "text")]
23pub fn to_lower(args: &[String]) -> FuncResult {
24    validations::args_min_len(args, 1)?;
25
26    Ok(args
27        .iter()
28        .map(|a| a.to_lowercase())
29        .collect::<Vec<_>>()
30        .join(" "))
31}
32
33/// Convert strings to uppercase.
34///
35/// This function takes a slice of strings and converts each string to uppercase.
36/// It then concatenates the converted strings into a single string, separated by space.
37///
38/// # Arguments
39///
40/// * `args`: A slice of strings to be converted to uppercase.
41///
42/// # Returns
43///
44/// * A [`FuncResult`] containing the concatenated uppercase strings.
45///
46/// # Errors
47///
48/// This function can return an error of [`crate::function::FunctionError`] variant:
49/// - `FunctionError::InvalidArgument` if there are insufficient input arguments.
50#[cfg_attr(docsrs, doc(cfg(feature = "text")))]
51#[cfg(feature = "text")]
52pub fn to_upper(args: &[String]) -> FuncResult {
53    validations::args_min_len(args, 1)?;
54
55    Ok(args
56        .iter()
57        .map(|a| a.to_uppercase())
58        .collect::<Vec<_>>()
59        .join(" "))
60}
61
62/// Trim leading and trailing whitespace from strings.
63///
64/// This function takes a slice of strings and removes leading and trailing whitespace
65/// from each string. It then concatenates the trimmed strings into a single string, separated by space.
66///
67/// # Arguments
68///
69/// * `args`: A slice of strings to be trimmed.
70///
71/// # Returns
72///
73/// * A [`FuncResult`] containing the concatenated trimmed strings.
74///
75/// # Errors
76///
77/// This function can return an error of [`crate::function::FunctionError`] variant:
78/// - `FunctionError::InvalidArgument` if there are insufficient input arguments.
79#[cfg_attr(docsrs, doc(cfg(feature = "text")))]
80#[cfg(feature = "text")]
81pub fn trim(args: &[String]) -> FuncResult {
82    validations::args_min_len(args, 1)?;
83
84    Ok(args.iter().map(|a| a.trim()).collect::<Vec<_>>().join(" "))
85}