Expand description
š¼ A case conversion library for Rust.
§š Getting started
First, add the anycase
crate to your Cargo manifest.
cargo add anycase
Then you can use the as_<case>
function to get a Display
type.
let s = format!("snake case: {}", anycase::as_snake("Hello world!"));
assert_eq!(s, "snake case: hello_world");
Alternatively, you can use the to_<case>
function to get a String
.
let s = anycase::to_snake("Hello world!");
assert_eq!(s, "hello_world");
§𤸠Usage
The anycase
crate provides a set of functions to convert strings between
different case styles. The following cases are available.
Given an input of Hello world!
:
as_camel
displayshelloWorld
as_pascal
displaysHelloWorld
as_snake
displayshello_world
as_screaming_snake
displaysHELLO_WORLD
as_kebab
displayshello-world
as_screaming_kebab
displaysHELLO_WORLD
as_train
displaysHello-World
as_lower
displayshello world
as_title
displaysHello World
as_upper
displaysHELLO WORLD
For all of the above functions, you can use the to_<case>
variant to get a
String
instead of a Display
type.
Additionally, the crate provides the raw
module containing the raw
functions which can be used to implement custom case conversion functions.
use anycase::raw;
let input = "Hello world!";
let output = raw::to_string(input, raw::write_upper, raw::delim_fn("."));
assert_eq!(output, "HELLO.WORLD");
See the module level documentation for more details.
§How does it work?
This implementation divides the input string into words and applies a āword functionā to each word and calls a ādelimiter functionā for each word boundary (the space between words).
Word boundaries are defined as follows:
- A set of consecutive non-letter/number/symbol e.g.
foo _bar
is two wordsfoo
andbar
. - A transition from a lowercase letter to an uppercase letter e.g.
fooBar
is two wordsfoo
andBar
. - The second last uppercase letter in a word with multiple uppercase letters
e.g.
FOOBar
is two wordsFOO
andBar
.
The following char
methods are used in the above conditions:
char::is_alphanumeric
is used to determine if a character is a letter/number/symbolchar::is_lowercase
is used to determine if a character is a lowercase letterchar::is_uppercase
is used to determine if a character is an uppercase letter
§Features
This crate is designed to be no_std
compatible. This is made possible by
disabling all default features. The following features are available:
-
std
(enabled by default) ā Currently only enables thealloc
feature but is here to allow for forward compatibility with anystd
-only features. -
alloc
ā Links thealloc
crate and enables the use ofString
functions.
§MSRV
The minimum supported Rust version (MSRV) is 1.56.0. The policy of this crate is to only increase the MSRV in a breaking release.
Modules§
- raw
- This module provides raw functions for transforming strings into different cases. It is used by the functions in the root of this crate.
Functions§
- as_
camel - Display a string as ācamelCaseā.
- as_
kebab - Display a string as ākebab-caseā.
- as_
lower - Display a string as ālower caseā.
- as_
pascal - Display a string as āPascalCaseā.
- as_
screaming_ kebab - Display a string as āSCREAMING-KEBAB-CASEā.
- as_
screaming_ snake - Display a string as āSCREAMING_SNAKE_CASEā.
- as_
snake - Display a string as āsnake_caseā.
- as_
title - Display a string as āTitle Caseā.
- as_
train - Display a string as āTrain-Caseā.
- as_
upper - Display a string as āUPPER CASEā.
- to_
camel alloc
- Transforms a string to ācamelCaseā.
- to_
kebab alloc
- Transforms a string to ākebab-caseā.
- to_
lower alloc
- Transforms a string to ālower caseā.
- to_
pascal alloc
- Transforms a string to āPascalCaseā.
- to_
screaming_ kebab alloc
- Transforms a string to āSCREAMING-KEBAB-CASEā.
- to_
screaming_ snake alloc
- Transforms a string to āSCREAMING_SNAKE_CASEā.
- to_
snake alloc
- Transforms a string to āsnake_caseā.
- to_
title alloc
- Transforms a string to āTitle Caseā.
- to_
train alloc
- Transforms a string to āTrain-Caseā.
- to_
upper alloc
- Transforms a string to āUPPER CASEā.