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
//! Trim modifier — strips characters from ends of strings, or filters list elements.
//! Without arg: trims whitespace, removes empty strings from list.
//! With arg: trims specified characters, removes elements equal to arg.
use anyhow::Result;
use super::modifier::{Modifier, ModifierKey};
use crate::models::context::ContextModel;
pub struct TrimModifier;
impl Modifier for TrimModifier {
fn key(&self) -> ModifierKey {
ModifierKey::Trim
}
fn apply(&self, value: &ContextModel, arg: &str) -> Result<ContextModel> {
let chars: Vec<char> = arg.chars().collect();
match value {
ContextModel::String(s) => {
let trimmed = s
.trim_matches(|c: char| c.is_whitespace() || chars.contains(&c))
.to_string();
if arg.is_empty() {
Ok(ContextModel::String(trimmed))
} else if trimmed == arg {
Ok(ContextModel::String(String::new()))
} else {
Ok(ContextModel::String(trimmed))
}
}
ContextModel::List(items) => {
let filtered: Vec<ContextModel> = items
.iter()
.filter(|i| {
let s = i
.to_string()
.trim_matches(|c: char| c.is_whitespace() || chars.contains(&c))
.to_string();
if arg.is_empty() {
!s.is_empty()
} else {
s != arg
}
})
.map(|i| {
let s = i
.to_string()
.trim_matches(|c: char| c.is_whitespace() || chars.contains(&c))
.to_string();
ContextModel::String(s)
})
.collect();
Ok(ContextModel::List(filtered))
}
_ => anyhow::bail!("Modifier 'trim' expects a string or list"),
}
}
}