3 unstable releases

0.2.1 Mar 15, 2020
0.2.0 Mar 14, 2020
0.1.0 Mar 1, 2020

#1449 in Text processing

Download history 34/week @ 2024-07-20 18/week @ 2024-07-27 15/week @ 2024-08-03 24/week @ 2024-08-10 24/week @ 2024-08-17 40/week @ 2024-08-24 45/week @ 2024-08-31 29/week @ 2024-09-07 27/week @ 2024-09-14 70/week @ 2024-09-21 41/week @ 2024-09-28 11/week @ 2024-10-05 32/week @ 2024-10-12 16/week @ 2024-10-19 16/week @ 2024-10-26 30/week @ 2024-11-02

95 downloads per month
Used in 12 crates (7 directly)

Unlicense

28KB
640 lines

case_style

Crates.io Documentation Crates.io

Converts from and to different case styles

Examples

There are two ways to use this libary. One is when you know the type upfront:

use case_style::CaseStyle;
let kebab = CaseStyle::from_camelcase("camelCase").to_kebabcase();
println!("{}", kebab);
assert_eq!("camel-case", kebab);

Or from string

use case_style::CaseStyle;
let pascal = CaseStyle::from_case("kebab", "kebab-case")
    .expect("kebab is an existing format")
    .to_case("Pascal")
    .expect("pascal is an existing format");
println!("{}", pascal);
assert_eq!("KebabCase", pascal);

And one where you don't know exactly:

use case_style::CaseStyle;
let kebab = CaseStyle::guess("snake_case").unwrap().to_kebabcase();
println!("{}", kebab);
assert_eq!("snake-case", kebab);

The guess method is restrictive by design, so you should decide a backup type yourself like:

use case_style::CaseStyle;
let input = "user just typed this in :P";
let snake = CaseStyle::guess(input)
    .unwrap_or(CaseStyle::from_sentencecase(input))
    .to_snakecase();
println!("{}", snake);
assert_eq!("user_just_typed_this_in_:p", snake);

In the previous example you can see that the convertion of styles does not remove odd characters. This you should also do youself.

use case_style::CaseStyle;
let input = "this is just some random input 🦖 ";
let filtered_input: String = input
    .chars()
    .filter(|x| x == &' ' || x.is_ascii_alphanumeric())
    .collect();
let filtered_input = filtered_input.trim();

let snake = CaseStyle::guess(filtered_input)
    .unwrap_or(CaseStyle::from_lowercase_spacecase(filtered_input))
    .to_camelcase();
println!("{}", snake);
assert_eq!("thisIsJustSomeRandomInput", snake);

Current supported formats:

  • SnakeCase
  • CamelCase
  • ConstantCase
  • KebabCase
  • LowercaseSpace
  • PascalCase
  • SentenceCase

for a up to date list look at the docs

License: Unlicense

No runtime deps