scsys_traits/
string.rs

1/*
2    Appellation: string <module>
3    Contrib: FL03 <jo3mccain@icloud.com>
4*/
5
6#[cfg(feature = "alloc")]
7use alloc::string::{String, ToString};
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/// [`StringFmt`] is a trait that provides methods for formatting strings.
19/// **Note** This crate requires the `alloc` feature
20#[cfg(feature = "alloc")]
21pub trait StringFmt {
22    /// Converts the string to a `kebab-case` format.
23    fn kebab_case(&self) -> String;
24    /// Converts the string to a `dot.case` format.
25    fn dot_case(&self) -> String;
26    /// converts into a `snake_case` format.
27    fn snake_case(&self) -> String;
28    /// Converts the string to a `Title case` format.
29    fn title_case(&self) -> String;
30}
31
32/*
33 ************* Implementations *************
34*/
35
36impl<'a> RemoveFnl for &'a str {
37    type Output = &'a str;
38
39    fn remove_fnl(&self) -> Self::Output {
40        &self[1..self.len() - 1]
41    }
42}
43
44#[cfg(feature = "alloc")]
45impl RemoveFnl for String {
46    type Output = String;
47
48    fn remove_fnl(&self) -> Self::Output {
49        self[1..self.len() - 1].to_string()
50    }
51}
52
53#[cfg(test)]
54mod tests {
55    use super::*;
56
57    #[test]
58    fn test_remove_fnl() {
59        let s = "\"Hello, World!\"";
60        assert_eq!(s.chars().next(), Some('"'));
61        assert_eq!(s.remove_fnl(), "Hello, World!");
62    }
63
64    #[cfg(feature = "alloc")]
65    #[test]
66    fn test_remove_fnl_alloc() {
67        let s = String::from("\"Hello, World!\"");
68        assert_eq!(s.remove_fnl(), String::from("Hello, World!"));
69    }
70}