scsys_traits/
string.rs

1/*
2    Appellation: string <module>
3    Contrib: FL03 <jo3mccain@icloud.com>
4*/
5
6#[cfg(feature = "alloc")]
7pub use impl_alloc::*;
8
9/// This trait defines a method for removing the first and last entries within an entity.
10///
11/// Typically, this is used to remove the first and last characters of a string, such as a
12/// quote or a parenthesis.
13pub trait RemoveFnl {
14    type Output;
15
16    fn remove_fnl(&self) -> Self::Output;
17}
18
19impl<'a> RemoveFnl for &'a str {
20    type Output = &'a str;
21
22    fn remove_fnl(&self) -> Self::Output {
23        &self[1..self.len() - 1]
24    }
25}
26
27#[cfg(feature = "alloc")]
28impl RemoveFnl for alloc::string::String {
29    type Output = alloc::string::String;
30
31    fn remove_fnl(&self) -> Self::Output {
32        self[1..self.len() - 1].to_string()
33    }
34}
35
36#[cfg(feature = "alloc")]
37mod impl_alloc {
38    use alloc::string::String;
39
40    pub trait StringFmt {
41        fn snake_case(&self) -> String;
42
43        fn title_case(&self) -> String;
44    }
45}
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50
51    #[test]
52    fn test_remove_fnl() {
53        let s = "\"Hello, World!\"";
54        assert_eq!(s.chars().nth(0), Some('"'));
55        assert_eq!(s.remove_fnl(), "Hello, World!");
56    }
57
58    #[cfg(feature = "alloc")]
59    #[test]
60    fn test_remove_fnl_alloc() {
61        let s = String::from("\"Hello, World!\"");
62        assert_eq!(s.remove_fnl(), String::from("Hello, World!"));
63    }
64}