NamingCase

Enum NamingCase 

Source
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 (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

Source

pub fn new(identifier: &str) -> NamingCase

Create a NamingCase value from an identifier.

Alias of which_case() and from().

Source

pub fn is_invalid(&self) -> bool

Check if this is an Invalid instance.

Source

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());
§Errors

Perform this on Invalid enum will get an Err.

Source

pub fn to_snake(&self) -> Result<String, &'static str>

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.

Source

pub fn to_kebab(&self) -> Result<String, &'static str>

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.

Source

pub fn to_camel(&self) -> Result<String, &'static str>

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.

Source

pub fn to_pascal(&self) -> Result<String, &'static str>

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§

Source§

impl Debug for NamingCase

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for NamingCase

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult

Formats the value using the given formatter. Read more
Source§

impl PartialEq for NamingCase

Source§

fn eq(&self, other: &NamingCase) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for NamingCase

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.