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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
/*! Transformers for the template
To apply a tranformer to a variable provide it after [`VAR_TRANSFORM_SEP_CHAR`] (currently ":") to a variable template.
There are a few transformers available:
| Transformer | Arguments | Function | Example |
|----------------------|-----------|--------------------------|--------------------------|
| f [`format_float`] | [.]N | only N number of decimal | {"1.12":f(.1)} ⇒ 1.1 |
| case [`string_case`] | up | UPCASE a string | {"na":case(up)} ⇒ NA |
| case [`string_case`] | down | downcase a string | {"nA":case(down)} ⇒ na |
| case [`string_case`] | proper | Upcase the first letter | {"nA":case(proper)} ⇒ Na |
| case [`string_case`] | title | Title Case the string | {"na":case(title)} ⇒ Na |
| calc | [+-*\/^]N | Airthmatic calculation | {"1":calc(+1*2^2)} ⇒ 16 |
| calc | [+-*\/^]N | Airthmatic calculation | {"1":calc(+1,-1)} ⇒ 2,0 |
| count | str | count str occurance | {"nata":count(a)} ⇒ 2 |
You can chain transformers ones after another for combined actions. For example, `count( ):calc(+1)` will give you total number of words in a sentence.
Examples are in individual functions.
*/
use std::ops::{Bound, RangeBounds};
use crate::errors::TransformerError;
use crate::VAR_TRANSFORM_SEP_CHAR;
use lazy_static::lazy_static;
use regex::Regex;
use titlecase::titlecase;
/// Applies any tranformations to the variable, you can chain the
/// transformers Called whenever you use [`VAR_TRANSFORM_SEP_CHAR`] to
/// provide a transformer in the template.
pub fn apply_tranformers(val: &str, transformations: &str) -> Result<String, TransformerError> {
let mut val: String = val.to_string();
for tstr in transformations.split(VAR_TRANSFORM_SEP_CHAR) {
if tstr.is_empty() {
continue;
}
let (name, args) = tstr.split_once('(').unwrap();
let args: Vec<&str> = args.strip_suffix(')').unwrap().split(',').collect();
val = match name {
"f" => float_format(&val, args)?,
"case" => string_case(&val, args)?,
"calc" => calc(&val, args)?,
"count" => count(&val, args)?,
_ => {
return Err(TransformerError::UnknownTranformer(
name.to_string(),
val.to_string(),
))
}
};
}
Ok(val)
}
fn bound(b: Bound<&usize>, lower: bool) -> Option<usize> {
match b {
Bound::Unbounded => None,
Bound::Included(v) => Some(*v),
Bound::Excluded(v) => Some(if lower { v - 1 } else { v + 1 }),
}
}
/// Checks whether the arguments lenth matches what is required
fn check_arguments_len<R: RangeBounds<usize>>(
func_name: &'static str,
req: R,
given: usize,
) -> Result<(), TransformerError> {
if req.contains(&given) {
Ok(())
} else {
match (
bound(req.start_bound(), true),
bound(req.end_bound(), false),
) {
(None, Some(r)) => Err(TransformerError::TooManyArguments(func_name, r, given)),
(Some(r), None) => Err(TransformerError::TooFewArguments(func_name, r, given)),
(Some(r1), Some(r2)) => {
if given < r1 {
Err(TransformerError::TooFewArguments(func_name, r1, given))
} else {
Err(TransformerError::TooFewArguments(func_name, r2, given))
}
}
_ => Ok(()),
}
}
}
/// format the float (numbers). For example with `val=1.123`, `{val:f(2)}` or `{val:f(.2)}` gives `1.12`
///
/// ```rust
/// # use std::error::Error;
/// # use string_template_plus::transformers::*;
/// #
/// # fn main() -> Result<(), Box<dyn Error>> {
/// assert_eq!(float_format("1.12", vec![".1"])?, "1.1");
/// assert_eq!(float_format("1.12", vec!["2"])?, "1.12");
/// assert_eq!(float_format("1.12", vec!["0"])?, "1");
/// # Ok(())
/// # }
pub fn float_format(val: &str, args: Vec<&str>) -> Result<String, TransformerError> {
let func_name = "f";
check_arguments_len(func_name, 1..=1, args.len())?;
let format = args[0];
let val = val
.parse::<f64>()
.map_err(|_| TransformerError::InvalidValueType(func_name, "float"))?;
let mut start = 0usize;
let mut decimal = 6usize;
if let Some((d, f)) = format.split_once('.') {
if !d.is_empty() {
start = d.parse().map_err(|_| {
TransformerError::InvalidArgumentType(func_name, d.to_string(), "uint")
})?;
}
if f.is_empty() {
decimal = 0;
} else {
decimal = f.parse().map_err(|_| {
TransformerError::InvalidArgumentType(func_name, f.to_string(), "uint")
})?;
}
} else {
if !format.is_empty() {
decimal = format.parse().map_err(|_| {
TransformerError::InvalidArgumentType(func_name, format.to_string(), "uint")
})?;
}
}
Ok(format!("{0:1$.2$}", val, start, decimal))
}
/// Format the string. Supports `up`=> UPCASE, `down`=> downcase, `proper` => first character UPCASE all others downcase, `title` => title case according to [`titlecase::titlecase`]. e.g. `{var:case(up)}`.
///
/// ```rust
/// # use std::error::Error;
/// # use string_template_plus::transformers::*;
/// #
/// # fn main() -> Result<(), Box<dyn Error>> {
/// assert_eq!(string_case("na", vec!["up"])?, "NA");
/// assert_eq!(string_case("nA", vec!["down"])?, "na");
/// assert_eq!(string_case("nA", vec!["proper"])?, "Na");
/// assert_eq!(string_case("here, an apple", vec!["title"])?, "Here, an Apple");
/// # Ok(())
/// # }
pub fn string_case(val: &str, args: Vec<&str>) -> Result<String, TransformerError> {
let func_name = "case";
check_arguments_len(func_name, 1..=1, args.len())?;
let format = args[0];
match format.to_lowercase().as_str() {
"up" => Ok(val.to_uppercase()),
"down" => Ok(val.to_lowercase()),
"title" => Ok(titlecase(val)),
"proper" => Ok({
let mut c = val.chars();
match c.next() {
None => String::new(),
Some(f) => {
f.to_uppercase().collect::<String>() + c.as_str().to_lowercase().as_str()
}
}
}),
_ => Err(TransformerError::InvalidArgumentType(
func_name,
format.to_string(),
"{up;down;proper;title}",
)),
}
}
lazy_static! {
static ref CALC_NUMBERS: Regex = Regex::new("[0-9.]+").unwrap();
}
/// Airthmatic calculations, the value needs to be float. e.g. `{val:calc(+1)}` will add 1 to the value. The order of calculation is left to right.
///
/// ```rust
/// # use std::error::Error;
/// # use string_template_plus::transformers::*;
/// #
/// # fn main() -> Result<(), Box<dyn Error>> {
/// assert_eq!(calc("1.24", vec!["+1"])?, "2.24");
/// assert_eq!(calc("1", vec!["+1*2^2"])?, "16");
/// assert_eq!(calc("1.24", vec!["+1", "-1"])?, "2.24,0.24");
/// # Ok(())
/// # }
pub fn calc(val: &str, args: Vec<&str>) -> Result<String, TransformerError> {
let func_name = "calc";
check_arguments_len(func_name, 1.., args.len())?;
let val: f64 = val
.parse()
.map_err(|_| TransformerError::InvalidValueType(func_name, "float"))?;
let mut results: Vec<String> = Vec::new();
for expr in args {
let mut last_match = 0usize;
let mut result = val;
for cap in CALC_NUMBERS.captures_iter(expr) {
let m = cap.get(0).unwrap();
let curr_val = m.as_str().parse().map_err(|_| {
TransformerError::InvalidArgumentType(func_name, m.as_str().to_string(), "float")
})?;
if m.start() == 0 {
result = curr_val;
} else {
match &expr[last_match..m.start()] {
"+" => result += curr_val,
"-" => result -= curr_val,
"/" => result /= curr_val,
"*" => result *= curr_val,
"^" => result = result.powf(curr_val),
s => {
return Err(TransformerError::InvalidArgumentType(
func_name,
s.to_string(),
"{+,-,*,/,^}",
))
}
};
}
last_match = m.end();
}
results.push(result.to_string());
}
Ok(results.join(","))
}
/// Count the number of occurances of a pattern in the string. You can chain it with [`calc`] to get the number of word like: `{val:count( ):calc(+1)}`
///
/// ```rust
/// # use std::error::Error;
/// # use string_template_plus::transformers::*;
/// #
/// # fn main() -> Result<(), Box<dyn Error>> {
/// assert_eq!(count("nata", vec!["a"])?, "2");
/// assert_eq!(count("nata", vec![" "])?, "0");
/// assert_eq!(count("hi there fellow", vec![" "])?, "2");
/// # Ok(())
/// # }
pub fn count(val: &str, args: Vec<&str>) -> Result<String, TransformerError> {
let func_name = "count";
check_arguments_len(func_name, 1.., args.len())?;
let counts: Vec<String> = args
.iter()
.map(|sep| val.matches(sep).count().to_string())
.collect();
Ok(counts.join(","))
}