pub enum NamingCase {
SingleWord(String),
ScreamingSnake(String),
Snake(String),
Kebab(String),
Camel(String),
Pascal(String),
Invalid(String),
}Expand description
Indicates which format the string belongs to, and acts as an intermediary between format conversions.
§Create Instances
There are three ways to create an instance. These three are aliases of each other.
use naming_lib::{NamingCase, which_case, from};
let first = NamingCase::new("identifier");
let second = which_case("identifier");
let third = from("identifier");§Notice
Of course you can generate instances of a specific enum type directly, I can’t stop you (in fact there is a solution, but it makes things more complex), but I don’t recommend using this approach.
use naming_lib::NamingCase;
let direct_instance = NamingCase::Invalid("text".to_string());I can’t do an input valid check when you use this approach, type-related methods on these instances may cause unexpected panic.
I currently use this myself to:
-
write document test cases (have to use this to “clearly express the state of the test values”).
-
generated instances of Invalid enum type (it’s safe, because conversion methods cannot be called on this enum type, there are no other type-related methods available now).
§Get Origin String From An Instance
A NamingCase instance holds the given string value when created, which can be got by calling to_string().
use naming_lib::from;
assert_eq!("example",from("example").to_string())§Convert An Instance To Other Naming Case String
A NamingCase instance also can be converted to a string in another naming format, as long as it’s not the Invalid enum.
use naming_lib::from;
assert_eq!("camel_case", from("camelCase").to_snake().unwrap());§Notice
For ease of use, instead of implementing the conversion methods with Invalid excluded, I have chosen that all conversion methods will return the Result type.
Calling any conversion method on an Invalid enum will return an Err.
Variants§
SingleWord(String)
A single word will be recognized as multiple formats (snake, kebab, camel), so it belongs to a separate category.
ScreamingSnake(String)
Snake(String)
Kebab(String)
Camel(String)
Pascal(String)
Invalid(String)
Can’t be recognized as a known format.
Implementations§
Source§impl NamingCase
impl NamingCase
Sourcepub fn new(identifier: &str) -> NamingCase
pub fn new(identifier: &str) -> NamingCase
Create a NamingCase value from an identifier.
Alias of which_case() and from().
Sourcepub fn is_invalid(&self) -> bool
pub fn is_invalid(&self) -> bool
Check if this is an Invalid instance.
Sourcepub fn to_screaming_snake(&self) -> Result<String, &'static str>
pub fn to_screaming_snake(&self) -> Result<String, &'static str>
Convert the included string to screaming snake case.
§Examples
use naming_lib::{from};
assert_eq!("SCREAMING", from("Screaming").to_screaming_snake().unwrap());
assert_eq!("CAMEL_CASE", from("camelCase").to_screaming_snake().unwrap());
assert_eq!("SNAKE_CASE", from("snake_case").to_screaming_snake().unwrap());