inflections/
lib.rs

1//! This is a library which allows anyone to change various properties of their
2//! strings with a heavy emphasis on performance. Allows programmers to
3//! manipulate a single programatic name consistently in multiple contexts.
4//!
5//! # Example
6//! ```rust
7//! // Remember to import the `Inflect` trait!
8//! use inflections::Inflect;
9//!
10//! assert_eq!("Hello World".to_camel_case(), "helloWorld".to_owned());
11//! ```
12
13pub mod case;
14
15/// An extension trait to make the functions in the `case` module available as
16/// methods on the `str` type.
17///
18/// # Example
19///
20/// ```rust
21/// // Remember to import the `Inflect` trait!
22/// use inflections::Inflect;
23///
24/// assert_eq!("Hello World".to_camel_case(), "helloWorld".to_owned());
25/// ```
26///
27/// # Stability
28///
29/// This trait is *not* meant to be used for generic programming. We reserve
30/// the right to add more methods to this trait and to change the
31/// implementations of this trait for primitive types *without making a major
32/// version release* as long as we don't break existing method calls.
33pub trait Inflect {
34  fn to_upper_case(&self) -> String;
35  fn is_upper_case(&self) -> bool;
36  fn to_lower_case(&self) -> String;
37  fn is_lower_case(&self) -> bool;
38  fn to_sentence_case(&self) -> String;
39  fn is_sentence_case(&self) -> bool;
40  fn to_title_case(&self) -> String;
41  fn is_title_case(&self) -> bool;
42  fn to_camel_case(&self) -> String;
43  fn is_camel_case(&self) -> bool;
44  fn to_pascal_case(&self) -> String;
45  fn is_pascal_case(&self) -> bool;
46  fn to_kebab_case(&self) -> String;
47  fn is_kebab_case(&self) -> bool;
48  fn to_train_case(&self) -> String;
49  fn is_train_case(&self) -> bool;
50  fn to_snake_case(&self) -> String;
51  fn is_snake_case(&self) -> bool;
52  fn to_constant_case(&self) -> String;
53  fn is_constant_case(&self) -> bool;
54}
55
56impl Inflect for str {
57  #[inline] fn to_upper_case(&self) -> String { case::to_upper_case(self) }
58  #[inline] fn is_upper_case(&self) -> bool { case::is_upper_case(self) }
59  #[inline] fn to_lower_case(&self) -> String { case::to_lower_case(self) }
60  #[inline] fn is_lower_case(&self) -> bool { case::is_lower_case(self) }
61  #[inline] fn to_sentence_case(&self) -> String { case::to_sentence_case(self) }
62  #[inline] fn is_sentence_case(&self) -> bool { case::is_sentence_case(self) }
63  #[inline] fn to_title_case(&self) -> String { case::to_title_case(self) }
64  #[inline] fn is_title_case(&self) -> bool { case::is_title_case(self) }
65  #[inline] fn to_camel_case(&self) -> String { case::to_camel_case(self) }
66  #[inline] fn is_camel_case(&self) -> bool { case::is_camel_case(self) }
67  #[inline] fn to_pascal_case(&self) -> String { case::to_pascal_case(self) }
68  #[inline] fn is_pascal_case(&self) -> bool { case::is_pascal_case(self) }
69  #[inline] fn to_kebab_case(&self) -> String { case::to_kebab_case(self) }
70  #[inline] fn is_kebab_case(&self) -> bool { case::is_kebab_case(self) }
71  #[inline] fn to_train_case(&self) -> String { case::to_train_case(self) }
72  #[inline] fn is_train_case(&self) -> bool { case::is_train_case(self) }
73  #[inline] fn to_snake_case(&self) -> String { case::to_snake_case(self) }
74  #[inline] fn is_snake_case(&self) -> bool { case::is_snake_case(self) }
75  #[inline] fn to_constant_case(&self) -> String { case::to_constant_case(self) }
76  #[inline] fn is_constant_case(&self) -> bool { case::is_constant_case(self) }
77}
78
79#[cfg(test)]
80mod test {
81  use super::Inflect;
82
83  #[test]
84  fn test_str() {
85    assert_eq!("foo".to_title_case(), "Foo".to_owned());
86  }
87
88  #[test]
89  fn test_string() {
90    assert_eq!("foo".to_owned().to_title_case(), "Foo".to_owned());
91  }
92}