[][src]Crate convert_case

Converts to and from various cases.

Provides a Case enum which defines a variety of cases to convert into. A Case can be used with an item that implements the Casing trait, which allows the item to be converted to a given case.

You can convert a string slice into a case using the to_case method on string slices or on owned strings.

use convert_case::{Case, Casing};
 
assert_eq!("Ronnie James Dio", "ronnie james dio".to_case(Case::Title));
assert_eq!("ronnieJamesDio", "Ronnie_James_dio".to_case(Case::Camel));
assert_eq!("Ronnie-James-Dio", "RONNIE_JAMES_DIO".to_case(Case::Train));

You can also explicitly write what case to parse the input as, using from_case. This is useful if the string has a case that is ambiguous.

use convert_case::{Case, Casing};
 
let filename = "2020-04-16_my_cat_cali".from_case(Case::Snake).to_case(Case::Title);
assert_eq!("2020-04-16 My Cat Cali", filename);

Note on Accuracy

The Casing methods from_case and to_case do not fail. Conversion to a case will always succeed. However, the results can still be unexpected.

The to_case method uses a series checks to determine where to split the string into words. Even if that method is explicit from the from_case method, it can always parse the entire string as a single word.

These examples demonstrate some unexpected behavior.

use convert_case::{Case, Casing};

// Mistakenly parsing using Case::Snake
assert_eq!("My-kebab-var", "my-kebab-var".from_case(Case::Snake).to_case(Case::Title));

// Converts using an unexpected method
assert_eq!("Mymany Casevariable", "myMany-caseVariable".to_case(Case::Title));

If your string emulates a variety of cases, try splitting across some delimiter before using the from_case and to_case methods.

Structs

FromCasing

Holds information about parsing before converting into a case.

Enums

Case

Defines the type of casing a string can be.

Traits

Casing

Describes items that can be converted into a case.