| Crates.io | anycase |
| lib.rs | anycase |
| version | 0.1.0 |
| created_at | 2024-07-11 17:51:56.135752+00 |
| updated_at | 2025-04-23 15:11:13.934541+00 |
| description | a case conversion library for Rust |
| homepage | |
| repository | https://github.com/rossmacarthur/anycase |
| max_upload_size | |
| id | 1299849 |
| size | 49,806 |
đź’Ľ A case conversion library for Rust.
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");
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 displays helloWorldas_pascal displays HelloWorldas_snake displays hello_worldas_screaming_snake displays HELLO_WORLDas_kebab displays hello-worldas_screaming_kebab displays HELLO_WORLDas_train displays Hello-Worldas_lower displays hello worldas_title displays Hello Worldas_upper displays HELLO WORLDFor 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.
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:
foo _bar is two words
foo and bar.fooBar
is two words foo and Bar.FOOBar is two words FOO and Bar.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
letterThis 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 the alloc
feature but is here to allow for forward compatibility with any
std-only features.
alloc — Links the alloc crate and enables the use of String
functions.
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.
This project is distributed under the terms of both the MIT license and the Apache License (Version 2.0).
See LICENSE-APACHE and LICENSE-MIT for details.