Debian Codegraph @

src/lib.rs

raw symbols SCIP
1 //! Crate for changing case of Rust identifiers.
2 //!
3 //! # Features
4 //! * Supports `snake_case`, `lowercase`, `camelCase`,
5 //! `PascalCase`, `SCREAMING_SNAKE_CASE`, and `kebab-case`
6 //! * Rename variants, and fields
7 //!
8 //! # Examples
9 //! ```rust
10 //! use ident_case::RenameRule;
11 //!
12 //! assert_eq!("helloWorld", RenameRule::CamelCase.apply_to_field("hello_world"));
13 //!
14 //! assert_eq!("i_love_serde", RenameRule::SnakeCase.apply_to_variant("ILoveSerde"));
15 //! ```
16
17 // Copyright 2017 Serde Developers
18 //
19 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
20 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
21 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
22 // option. This file may not be copied, modified, or distributed
23 // except according to those terms.
24
25 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::asciirust-analyzer cargo std https://github.com/rust-lang/rust/library/std ascii/::AsciiExtrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ascii/AsciiExt#;
26 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/::FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#;
27
28 use selfrust-analyzer cargo ident_case 1.0.1 crate/::RenameRulerust-analyzer cargo ident_case 1.0.1 RenameRule#::*;
29
30 /// A casing rule for renaming Rust identifiers.
31 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:)]
32 pub enum RenameRulerust-analyzer cargo ident_case 1.0.1 RenameRule# {
33 /// No-op rename rule.
34 Nonerust-analyzer cargo ident_case 1.0.1 RenameRule#None#,
35 /// Rename direct children to "lowercase" style.
36 LowerCaserust-analyzer cargo ident_case 1.0.1 RenameRule#LowerCase#,
37 /// Rename direct children to "PascalCase" style, as typically used for enum variants.
38 PascalCaserust-analyzer cargo ident_case 1.0.1 RenameRule#PascalCase#,
39 /// Rename direct children to "camelCase" style.
40 CamelCaserust-analyzer cargo ident_case 1.0.1 RenameRule#CamelCase#,
41 /// Rename direct children to "snake_case" style, as commonly used for fields.
42 SnakeCaserust-analyzer cargo ident_case 1.0.1 RenameRule#SnakeCase#,
43 /// Rename direct children to "SCREAMING_SNAKE_CASE" style, as commonly used for constants.
44 ScreamingSnakeCaserust-analyzer cargo ident_case 1.0.1 RenameRule#ScreamingSnakeCase#,
45 /// Rename direct children to "kebab-case" style.
46 KebabCaserust-analyzer cargo ident_case 1.0.1 RenameRule#KebabCase#,
47 }
48
49 impl RenameRulerust-analyzer cargo ident_case 1.0.1 RenameRule# {
50 /// Change case of a `PascalCase` variant.
51 pub fn apply_to_variantrust-analyzer cargo ident_case 1.0.1 impl#[RenameRule]apply_to_variant().<S: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<str>>(&self, variant: S) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# {
52
53 let variant = variant.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().();
54 match *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().self {
55 Nonerust-analyzer cargo ident_case 1.0.1 RenameRule#None# | PascalCaserust-analyzer cargo ident_case 1.0.1 RenameRule#PascalCase# => variant.to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().(),
56 LowerCaserust-analyzer cargo ident_case 1.0.1 RenameRule#LowerCase# => variant.to_ascii_lowercaserust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]to_ascii_lowercase().(),
57 CamelCaserust-analyzer cargo ident_case 1.0.1 RenameRule#CamelCase# => variant[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()..to_ascii_lowercaserust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]to_ascii_lowercase().() rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Add<&str>`]add().+rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Add<&str>`]add(). rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Add<&str>`]add().&variant[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().,
58 SnakeCaserust-analyzer cargo ident_case 1.0.1 RenameRule#SnakeCase# => {
59 let mut snake = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().();
60 for (i, ch) in variant.char_indicesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]char_indices().() {
61 if i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().0 && ch.is_uppercaserust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/methods/impl#[char]is_uppercase().() {
62 snake.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().('_');
63 }
64 snake.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().(ch.to_ascii_lowercaserust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/methods/impl#[char]to_ascii_lowercase().());
65 }
66 snake
67 }
68 ScreamingSnakeCaserust-analyzer cargo ident_case 1.0.1 RenameRule#ScreamingSnakeCase# => SnakeCaserust-analyzer cargo ident_case 1.0.1 RenameRule#SnakeCase#.apply_to_variantrust-analyzer cargo ident_case 1.0.1 impl#[RenameRule]apply_to_variant().(variant).to_ascii_uppercaserust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]to_ascii_uppercase().(),
69 KebabCaserust-analyzer cargo ident_case 1.0.1 RenameRule#KebabCase# => SnakeCaserust-analyzer cargo ident_case 1.0.1 RenameRule#SnakeCase#.apply_to_variantrust-analyzer cargo ident_case 1.0.1 impl#[RenameRule]apply_to_variant().(variant).replacerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]replace().('_', "-"),
70 }
71 }
72
73 /// Change case of a `snake_case` field.
74 pub fn apply_to_fieldrust-analyzer cargo ident_case 1.0.1 impl#[RenameRule]apply_to_field().<S: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<str>>(&self, field: S) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# {
75
76 let field = field.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().();
77 match *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().self {
78 Nonerust-analyzer cargo ident_case 1.0.1 RenameRule#None# | LowerCaserust-analyzer cargo ident_case 1.0.1 RenameRule#LowerCase# | SnakeCaserust-analyzer cargo ident_case 1.0.1 RenameRule#SnakeCase# => field.to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().(),
79 PascalCaserust-analyzer cargo ident_case 1.0.1 RenameRule#PascalCase# => {
80 let mut pascal = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().();
81 let mut capitalize = true;
82 for ch in field.charsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]chars().() {
83 if ch rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]eq().'_' {
84 capitalize = true;
85 } else if capitalize {
86 pascal.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().(ch.to_ascii_uppercaserust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/methods/impl#[char]to_ascii_uppercase().());
87 capitalize = false;
88 } else {
89 pascal.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().(ch);
90 }
91 }
92 pascal
93 }
94 CamelCaserust-analyzer cargo ident_case 1.0.1 RenameRule#CamelCase# => {
95 let pascal = PascalCaserust-analyzer cargo ident_case 1.0.1 RenameRule#PascalCase#.apply_to_fieldrust-analyzer cargo ident_case 1.0.1 impl#[RenameRule]apply_to_field().(field);
96 pascal[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Index<I>`]index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#1]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Index<I>`]index()..to_ascii_lowercaserust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]to_ascii_lowercase().() rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Add<&str>`]add().+rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Add<&str>`]add(). rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Add<&str>`]add().&pascal[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Index<I>`]index().1..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Index<I>`]index().
97 }
98 ScreamingSnakeCaserust-analyzer cargo ident_case 1.0.1 RenameRule#ScreamingSnakeCase# => field.to_ascii_uppercaserust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]to_ascii_uppercase().(),
99 KebabCaserust-analyzer cargo ident_case 1.0.1 RenameRule#KebabCase# => field.replacerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]replace().('_', "-"),
100 }
101 }
102 }
103
104 impl FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr# for RenameRulerust-analyzer cargo ident_case 1.0.1 RenameRule# {
105 type Errrust-analyzer cargo ident_case 1.0.1 impl#[RenameRule][FromStr]Err# = ();
106
107 fn from_strrust-analyzer cargo ident_case 1.0.1 impl#[RenameRule][FromStr]from_str().(rename_all_str: &str) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo ident_case 1.0.1 impl#[RenameRule][FromStr], Selfrust-analyzer cargo ident_case 1.0.1 impl#[RenameRule][FromStr]::Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#Err#> {
108 match rename_all_str {
109 "lowercase" => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(LowerCaserust-analyzer cargo ident_case 1.0.1 RenameRule#LowerCase#),
110 "PascalCase" => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(PascalCaserust-analyzer cargo ident_case 1.0.1 RenameRule#PascalCase#),
111 "camelCase" => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(CamelCaserust-analyzer cargo ident_case 1.0.1 RenameRule#CamelCase#),
112 "snake_case" => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(SnakeCaserust-analyzer cargo ident_case 1.0.1 RenameRule#SnakeCase#),
113 "SCREAMING_SNAKE_CASE" => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(ScreamingSnakeCaserust-analyzer cargo ident_case 1.0.1 RenameRule#ScreamingSnakeCase#),
114 "kebab-case" => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(KebabCaserust-analyzer cargo ident_case 1.0.1 RenameRule#KebabCase#),
115 _ => Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(()),
116 }
117 }
118 }
119
120 impl Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default# for RenameRulerust-analyzer cargo ident_case 1.0.1 RenameRule# {
121 fn defaultrust-analyzer cargo ident_case 1.0.1 impl#[RenameRule][Default]default().() -> Selfrust-analyzer cargo ident_case 1.0.1 impl#[RenameRule][Default] {
122 RenameRulerust-analyzer cargo ident_case 1.0.1 RenameRule#::Nonerust-analyzer cargo ident_case 1.0.1 RenameRule#None#
123 }
124 }
125
126 #[cfg(test)]
127 mod testsrust-analyzer cargo ident_case 1.0.1 tests/ {
128 use superrust-analyzer cargo ident_case 1.0.1 crate/::RenameRulerust-analyzer cargo ident_case 1.0.1 RenameRule#::*;
129
130 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
131 fn rename_variantsrust-analyzer cargo ident_case 1.0.1 tests/rename_variants().() {
132 for &(original, lower, camel, snake, screaming, kebab) in
133 &[
134 ("Outcome", "outcome", "outcome", "outcome", "OUTCOME", "outcome"),
135 ("VeryTasty", "verytasty", "veryTasty", "very_tasty", "VERY_TASTY", "very-tasty"),
136 ("A", "a", "a", "a", "A", "a"),
137 ("Z42", "z42", "z42", "z42", "Z42", "z42"),
138 ] {
139 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(Nonerust-analyzer cargo ident_case 1.0.1 RenameRule#None#.apply_to_variantrust-analyzer cargo ident_case 1.0.1 impl#[RenameRule]apply_to_variant().(original), original);
140 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(LowerCaserust-analyzer cargo ident_case 1.0.1 RenameRule#LowerCase#.apply_to_variantrust-analyzer cargo ident_case 1.0.1 impl#[RenameRule]apply_to_variant().(original), lower);
141 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(PascalCaserust-analyzer cargo ident_case 1.0.1 RenameRule#PascalCase#.apply_to_variantrust-analyzer cargo ident_case 1.0.1 impl#[RenameRule]apply_to_variant().(original), original);
142 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(CamelCaserust-analyzer cargo ident_case 1.0.1 RenameRule#CamelCase#.apply_to_variantrust-analyzer cargo ident_case 1.0.1 impl#[RenameRule]apply_to_variant().(original), camel);
143 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(SnakeCaserust-analyzer cargo ident_case 1.0.1 RenameRule#SnakeCase#.apply_to_variantrust-analyzer cargo ident_case 1.0.1 impl#[RenameRule]apply_to_variant().(original), snake);
144 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(ScreamingSnakeCaserust-analyzer cargo ident_case 1.0.1 RenameRule#ScreamingSnakeCase#.apply_to_variantrust-analyzer cargo ident_case 1.0.1 impl#[RenameRule]apply_to_variant().(original), screaming);
145 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(KebabCaserust-analyzer cargo ident_case 1.0.1 RenameRule#KebabCase#.apply_to_variantrust-analyzer cargo ident_case 1.0.1 impl#[RenameRule]apply_to_variant().(original), kebab);
146 }
147 }
148
149 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
150 fn rename_fieldsrust-analyzer cargo ident_case 1.0.1 tests/rename_fields().() {
151 for &(original, pascal, camel, screaming, kebab) in
152 &[
153 ("outcome", "Outcome", "outcome", "OUTCOME", "outcome"),
154 ("very_tasty", "VeryTasty", "veryTasty", "VERY_TASTY", "very-tasty"),
155 ("_leading_under", "LeadingUnder", "leadingUnder", "_LEADING_UNDER", "-leading-under"),
156 ("double__under", "DoubleUnder", "doubleUnder", "DOUBLE__UNDER", "double--under"),
157 ("a", "A", "a", "A", "a"),
158 ("z42", "Z42", "z42", "Z42", "z42"),
159 ] {
160 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(Nonerust-analyzer cargo ident_case 1.0.1 RenameRule#None#.apply_to_fieldrust-analyzer cargo ident_case 1.0.1 impl#[RenameRule]apply_to_field().(original), original);
161 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(PascalCaserust-analyzer cargo ident_case 1.0.1 RenameRule#PascalCase#.apply_to_fieldrust-analyzer cargo ident_case 1.0.1 impl#[RenameRule]apply_to_field().(original), pascal);
162 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(CamelCaserust-analyzer cargo ident_case 1.0.1 RenameRule#CamelCase#.apply_to_fieldrust-analyzer cargo ident_case 1.0.1 impl#[RenameRule]apply_to_field().(original), camel);
163 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(SnakeCaserust-analyzer cargo ident_case 1.0.1 RenameRule#SnakeCase#.apply_to_fieldrust-analyzer cargo ident_case 1.0.1 impl#[RenameRule]apply_to_field().(original), original);
164 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(ScreamingSnakeCaserust-analyzer cargo ident_case 1.0.1 RenameRule#ScreamingSnakeCase#.apply_to_fieldrust-analyzer cargo ident_case 1.0.1 impl#[RenameRule]apply_to_field().(original), screaming);
165 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(KebabCaserust-analyzer cargo ident_case 1.0.1 RenameRule#KebabCase#.apply_to_fieldrust-analyzer cargo ident_case 1.0.1 impl#[RenameRule]apply_to_field().(original), kebab);
166 }
167 }
168 }