Enum naming_lib::NamingCase [−][src]
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, so it belongs to a separate category.
Tuple Fields of SingleWord
0: StringScreamingSnake(String)Tuple Fields of ScreamingSnake
0: StringSnake(String)Tuple Fields of Snake
0: StringKebab(String)Tuple Fields of Kebab
0: StringCamel(String)Tuple Fields of Camel
0: StringPascal(String)Tuple Fields of Pascal
0: StringInvalid(String)Can’t be recognized as a known format.
Tuple Fields of Invalid
0: StringImplementations
Create a NamingCase value from an identifier.
Alias of which_case() and from().
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());Errors
Trait Implementations
This method tests for self and other values to be equal, and is used
by ==. Read more
This method tests for !=.