inflector/string/demodulize/mod.rs
1#[cfg(feature = "heavyweight")]
2use cases::classcase::to_class_case;
3
4#[cfg(feature = "heavyweight")]
5/// Demodulize a `&str`
6///
7/// ```
8/// use inflector::string::demodulize::demodulize;
9/// let mock_string: &str = "Bar";
10/// let expected_string: String = "Bar".to_owned();
11/// let asserted_string: String = demodulize(mock_string);
12/// assert!(asserted_string == expected_string);
13///
14/// ```
15/// ```
16/// use inflector::string::demodulize::demodulize;
17/// let mock_string: &str = "::Bar";
18/// let expected_string: String = "Bar".to_owned();
19/// let asserted_string: String = demodulize(mock_string);
20/// assert!(asserted_string == expected_string);
21///
22/// ```
23/// ```
24/// use inflector::string::demodulize::demodulize;
25/// let mock_string: &str = "Foo::Bar";
26/// let expected_string: String = "Bar".to_owned();
27/// let asserted_string: String = demodulize(mock_string);
28/// assert!(asserted_string == expected_string);
29///
30/// ```
31/// ```
32/// use inflector::string::demodulize::demodulize;
33/// let mock_string: &str = "Test::Foo::Bar";
34/// let expected_string: String = "Bar".to_owned();
35/// let asserted_string: String = demodulize(mock_string);
36/// assert!(asserted_string == expected_string);
37///
38/// ```
39pub fn demodulize(non_demodulize_string: &str) -> String {
40 if non_demodulize_string.contains("::") {
41 let split_string: Vec<&str> = non_demodulize_string.split("::").collect();
42 to_class_case(split_string[split_string.len() - 1])
43 } else {
44 non_demodulize_string.to_owned()
45 }
46}