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:

  1. write document test cases (have to use this to “clearly express the state of the test values”).

  2. 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: String
ScreamingSnake(String)

Tuple Fields of ScreamingSnake

0: String
Snake(String)

Tuple Fields of Snake

0: String
Kebab(String)

Tuple Fields of Kebab

0: String
Camel(String)

Tuple Fields of Camel

0: String
Pascal(String)

Tuple Fields of Pascal

0: String
Invalid(String)

Can’t be recognized as a known format.

Tuple Fields of Invalid

0: String

Implementations

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

Perform this on Invalid enum will get an Err.

Convert the included string to snake case.

Examples
use naming_lib::{from};

assert_eq!("snake", from("Snake").to_snake().unwrap());
assert_eq!("kebab_case", from("kebab-case").to_snake().unwrap());
assert_eq!("camel_case", from("camelCase").to_snake().unwrap());
Errors

Perform this on Invalid enum will get an Err.

Convert the included string to kebab case.

Examples
use naming_lib::{from};

assert_eq!("kebab", from("Kebab").to_kebab().unwrap());
assert_eq!("camel-case", from("camelCase").to_kebab().unwrap());
assert_eq!("snake-case", from("snake_case").to_kebab().unwrap());
Errors

Perform this on Invalid enum will get an Err.

Convert the included string to camel case.

Examples
use naming_lib::{from};

assert_eq!("camel", from("Camel").to_camel().unwrap());
assert_eq!("pascalCase", from("PascalCase").to_camel().unwrap());
assert_eq!("snakeCase", from("snake_case").to_camel().unwrap());
Errors

Perform this on Invalid enum will get an Err.

Convert the included string to pascal case.

Examples
use naming_lib::{from};

assert_eq!("Pascal", from("Pascal").to_pascal().unwrap());
assert_eq!("CamelCase", from("camelCase").to_pascal().unwrap());
assert_eq!("SnakeCase", from("snake_case").to_pascal().unwrap());
Errors

Perform this on Invalid enum will get an Err.

Trait Implementations

Formats the value using the given formatter. Read more

Formats the value using the given formatter. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.