Skip to main content

teaql_tool_std/
text.rs

1use convert_case::{Case, Casing};
2use teaql_tool_core::MustPurpose;
3
4pub struct TextTool;
5
6impl TextTool {
7    pub fn new() -> Self {
8        Self
9    }
10
11    pub fn trim(&self, s: &str) -> MustPurpose<String> {
12        MustPurpose::new(s.trim().to_string())
13    }
14
15    pub fn lowercase(&self, s: &str) -> MustPurpose<String> {
16        MustPurpose::new(s.to_lowercase())
17    }
18
19    pub fn uppercase(&self, s: &str) -> MustPurpose<String> {
20        MustPurpose::new(s.to_uppercase())
21    }
22
23    pub fn capitalize(&self, s: &str) -> MustPurpose<String> {
24        let mut c = s.chars();
25        MustPurpose::new(match c.next() {
26            None => String::new(),
27            Some(f) => f.to_uppercase().collect::<String>() + c.as_str(),
28        })
29    }
30
31    pub fn to_snake_case(&self, s: &str) -> MustPurpose<String> {
32        MustPurpose::new(s.to_case(Case::Snake))
33    }
34
35    pub fn to_camel_case(&self, s: &str) -> MustPurpose<String> {
36        MustPurpose::new(s.to_case(Case::Camel))
37    }
38
39    pub fn to_pascal_case(&self, s: &str) -> MustPurpose<String> {
40        MustPurpose::new(s.to_case(Case::Pascal))
41    }
42
43    pub fn to_kebab_case(&self, s: &str) -> MustPurpose<String> {
44        MustPurpose::new(s.to_case(Case::Kebab))
45    }
46
47    pub fn normalize_whitespace(&self, s: &str) -> MustPurpose<String> {
48        MustPurpose::new(s.split_whitespace().collect::<Vec<&str>>().join(" "))
49    }
50
51    pub fn contains(&self, s: &str, substring: &str) -> MustPurpose<bool> {
52        MustPurpose::new(s.contains(substring))
53    }
54
55    pub fn starts_with(&self, s: &str, prefix: &str) -> MustPurpose<bool> {
56        MustPurpose::new(s.starts_with(prefix))
57    }
58
59    pub fn ends_with(&self, s: &str, suffix: &str) -> MustPurpose<bool> {
60        MustPurpose::new(s.ends_with(suffix))
61    }
62}
63
64impl Default for TextTool {
65    fn default() -> Self {
66        Self::new()
67    }
68}