ruby_inflector/string/
deconstantize.rs

1use std::collections::HashSet;
2
3use crate::case::class::to_class_case;
4
5/// Deconstantizes a `&str`
6///
7/// ```
8/// use ruby_inflector::string::deconstantize::deconstantize;
9/// let mock_string: &str = "Bar";
10/// let expected_string: String = "".to_owned();
11/// let asserted_string: String = deconstantize(mock_string);
12/// assert!(asserted_string == expected_string);
13///
14/// ```
15/// ```
16/// use ruby_inflector::string::deconstantize::deconstantize;
17/// let mock_string: &str = "::Bar";
18/// let expected_string: String = "".to_owned();
19/// let asserted_string: String = deconstantize(mock_string);
20/// assert!(asserted_string == expected_string);
21///
22/// ```
23/// ```
24/// use ruby_inflector::string::deconstantize::deconstantize;
25/// let mock_string: &str = "Foo::Bar";
26/// let expected_string: String = "Foo".to_owned();
27/// let asserted_string: String = deconstantize(mock_string);
28/// assert!(asserted_string == expected_string);
29///
30/// ```
31/// ```
32/// use ruby_inflector::string::deconstantize::deconstantize;
33/// let mock_string: &str = "Test::Foo::Bar";
34/// let expected_string: String = "Foo".to_owned();
35/// let asserted_string: String = deconstantize(mock_string);
36/// assert!(asserted_string == expected_string);
37///
38/// ```
39pub fn deconstantize(non_deconstantized_string: &str) -> String {
40    if non_deconstantized_string.contains("::") {
41        let split_string: Vec<&str> = non_deconstantized_string.split("::").collect();
42        if split_string.len() > 1 {
43            to_class_case(split_string[split_string.len() - 2], &HashSet::new())
44        } else {
45            "".to_owned()
46        }
47    } else {
48        "".to_owned()
49    }
50}