#string #case #identifier #snake #valid #lower-case #non-empty

snake_case

SnakeCase is a String-like type that can only contain valid non-empty snake_case

5 unstable releases

0.3.1 Mar 13, 2021
0.3.0 Mar 13, 2021
0.2.1 Mar 13, 2021
0.2.0 Sep 25, 2019
0.1.0 Sep 24, 2019

#1670 in Encoding

Download history 62/week @ 2024-07-22 71/week @ 2024-07-29 105/week @ 2024-08-05 131/week @ 2024-08-12 62/week @ 2024-08-19 92/week @ 2024-08-26 83/week @ 2024-09-02 107/week @ 2024-09-09 79/week @ 2024-09-16 89/week @ 2024-09-23 115/week @ 2024-09-30 107/week @ 2024-10-07 107/week @ 2024-10-14 117/week @ 2024-10-21 76/week @ 2024-10-28 64/week @ 2024-11-04

365 downloads per month
Used in diffus

MIT license

10KB
216 lines

snake_case

Latest version Documentation Build Status MIT

A Rust crate for working with snake_case identifiers

The purpose of this crate is to expose the type SnakeCase, a wrapper around a String that can only contain valid, non-empty snake_case without leading digits. In other words, it always matches ^[_a-z][_a-z0-9]*$

  • Non-empty
  • Starts with a lower case ASCII letter or underscore
  • Contains only lower case ASCII letters, underscores and digits

NOTE: ___foo__bar_ is considered valid snake case by this crate.

TL;DR: SnakeCase can hold any string that is also a valid lower case identifier in Rust.

Why?

The common case for this is unique identifiers, for which snake case is perfectly suited. SnakeCase will always be valid, meaning you will never have the problem of trailing spaces or empty strings.

Basic usage

let id = SnakeCase::try_from_str("hello_world").unwrap();
assert_eq!(id, "hello_world");

There is also SnakeCaseRef which is a non-owning reference to a snake_case string.

Serde

If you enable the "serde" feature then SnakeCase will implement Serialize and Deserialize.

Deserialize will fail if a string is not valid snake case.

Dependencies

~165KB