| 1 | #[allow(deprecated, unused_imports)] |
| 2 | 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#; |
| 3 | |
| 4 | use heckrust-analyzer cargo heck 0.5.0 crate/::{ |
| 5 | ToKebabCaserust-analyzer cargo heck 0.5.0 kebab/ToKebabCase#, ToLowerCamelCaserust-analyzer cargo heck 0.5.0 lower_camel/ToLowerCamelCase#, ToShoutyKebabCaserust-analyzer cargo heck 0.5.0 shouty_kebab/ToShoutyKebabCase#, ToShoutySnakeCaserust-analyzer cargo heck 0.5.0 shouty_snake/ToShoutySnakeCase#, ToSnakeCaserust-analyzer cargo heck 0.5.0 snake/ToSnakeCase#, ToTrainCaserust-analyzer cargo heck 0.5.0 train/ToTrainCase#, |
| 6 | ToUpperCamelCaserust-analyzer cargo heck 0.5.0 upper_camel/ToUpperCamelCase#, |
| 7 | }; |
| 8 | |
| 9 | use selfrust-analyzer cargo postgres-derive 0.4.9 case/::RenameRulerust-analyzer cargo postgres-derive 0.4.9 case/RenameRule#::*; |
| 10 | |
| 11 | /// The different possible ways to change case of fields in a struct, or variants in an enum. |
| 12 | #[allow(clippy::enum_variant_names)] |
| 13 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:)] |
| 14 | pub enum RenameRulerust-analyzer cargo postgres-derive 0.4.9 case/RenameRule# { |
| 15 | /// Rename direct children to "lowercase" style. |
| 16 | LowerCaserust-analyzer cargo postgres-derive 0.4.9 case/RenameRule#LowerCase#, |
| 17 | /// Rename direct children to "UPPERCASE" style. |
| 18 | UpperCaserust-analyzer cargo postgres-derive 0.4.9 case/RenameRule#UpperCase#, |
| 19 | /// Rename direct children to "PascalCase" style, as typically used for |
| 20 | /// enum variants. |
| 21 | PascalCaserust-analyzer cargo postgres-derive 0.4.9 case/RenameRule#PascalCase#, |
| 22 | /// Rename direct children to "camelCase" style. |
| 23 | CamelCaserust-analyzer cargo postgres-derive 0.4.9 case/RenameRule#CamelCase#, |
| 24 | /// Rename direct children to "snake_case" style, as commonly used for |
| 25 | /// fields. |
| 26 | SnakeCaserust-analyzer cargo postgres-derive 0.4.9 case/RenameRule#SnakeCase#, |
| 27 | /// Rename direct children to "SCREAMING_SNAKE_CASE" style, as commonly |
| 28 | /// used for constants. |
| 29 | ScreamingSnakeCaserust-analyzer cargo postgres-derive 0.4.9 case/RenameRule#ScreamingSnakeCase#, |
| 30 | /// Rename direct children to "kebab-case" style. |
| 31 | KebabCaserust-analyzer cargo postgres-derive 0.4.9 case/RenameRule#KebabCase#, |
| 32 | /// Rename direct children to "SCREAMING-KEBAB-CASE" style. |
| 33 | ScreamingKebabCaserust-analyzer cargo postgres-derive 0.4.9 case/RenameRule#ScreamingKebabCase#, |
| 34 | |
| 35 | /// Rename direct children to "Train-Case" style. |
| 36 | TrainCaserust-analyzer cargo postgres-derive 0.4.9 case/RenameRule#TrainCase#, |
| 37 | } |
| 38 | |
| 39 | pub const RENAME_RULESrust-analyzer cargo postgres-derive 0.4.9 case/RENAME_RULES.: &[&str] = &[ |
| 40 | "lowercase", |
| 41 | "UPPERCASE", |
| 42 | "PascalCase", |
| 43 | "camelCase", |
| 44 | "snake_case", |
| 45 | "SCREAMING_SNAKE_CASE", |
| 46 | "kebab-case", |
| 47 | "SCREAMING-KEBAB-CASE", |
| 48 | "Train-Case", |
| 49 | ]; |
| 50 | |
| 51 | impl RenameRulerust-analyzer cargo postgres-derive 0.4.9 case/RenameRule# { |
| 52 | pub fn from_strrust-analyzer cargo postgres-derive 0.4.9 case/impl#[RenameRule]from_str().(rule: &str) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<RenameRulerust-analyzer cargo postgres-derive 0.4.9 case/RenameRule#> { |
| 53 | match rule { |
| 54 | "lowercase" => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(LowerCaserust-analyzer cargo postgres-derive 0.4.9 case/RenameRule#LowerCase#), |
| 55 | "UPPERCASE" => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(UpperCaserust-analyzer cargo postgres-derive 0.4.9 case/RenameRule#UpperCase#), |
| 56 | "PascalCase" => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(PascalCaserust-analyzer cargo postgres-derive 0.4.9 case/RenameRule#PascalCase#), |
| 57 | "camelCase" => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(CamelCaserust-analyzer cargo postgres-derive 0.4.9 case/RenameRule#CamelCase#), |
| 58 | "snake_case" => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(SnakeCaserust-analyzer cargo postgres-derive 0.4.9 case/RenameRule#SnakeCase#), |
| 59 | "SCREAMING_SNAKE_CASE" => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(ScreamingSnakeCaserust-analyzer cargo postgres-derive 0.4.9 case/RenameRule#ScreamingSnakeCase#), |
| 60 | "kebab-case" => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(KebabCaserust-analyzer cargo postgres-derive 0.4.9 case/RenameRule#KebabCase#), |
| 61 | "SCREAMING-KEBAB-CASE" => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(ScreamingKebabCaserust-analyzer cargo postgres-derive 0.4.9 case/RenameRule#ScreamingKebabCase#), |
| 62 | "Train-Case" => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(TrainCaserust-analyzer cargo postgres-derive 0.4.9 case/RenameRule#TrainCase#), |
| 63 | _ => Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 64 | } |
| 65 | } |
| 66 | /// Apply a renaming rule to an enum or struct field, returning the version expected in the source. |
| 67 | pub fn apply_to_fieldrust-analyzer cargo postgres-derive 0.4.9 case/impl#[RenameRule]apply_to_field().(&self, variant: &str) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# { |
| 68 | match *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().self { |
| 69 | LowerCaserust-analyzer cargo postgres-derive 0.4.9 case/RenameRule#LowerCase# => variant.to_lowercaserust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]to_lowercase().(), |
| 70 | UpperCaserust-analyzer cargo postgres-derive 0.4.9 case/RenameRule#UpperCase# => variant.to_uppercaserust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]to_uppercase().(), |
| 71 | PascalCaserust-analyzer cargo postgres-derive 0.4.9 case/RenameRule#PascalCase# => variant.to_upper_camel_caserust-analyzer cargo heck 0.5.0 upper_camel/impl#[str][ToUpperCamelCase]to_upper_camel_case().(), |
| 72 | CamelCaserust-analyzer cargo postgres-derive 0.4.9 case/RenameRule#CamelCase# => variant.to_lower_camel_caserust-analyzer cargo heck 0.5.0 lower_camel/impl#[str][ToLowerCamelCase]to_lower_camel_case().(), |
| 73 | SnakeCaserust-analyzer cargo postgres-derive 0.4.9 case/RenameRule#SnakeCase# => variant.to_snake_caserust-analyzer cargo heck 0.5.0 snake/impl#[str][ToSnakeCase]to_snake_case().(), |
| 74 | ScreamingSnakeCaserust-analyzer cargo postgres-derive 0.4.9 case/RenameRule#ScreamingSnakeCase# => variant.to_shouty_snake_caserust-analyzer cargo heck 0.5.0 shouty_snake/impl#[str][ToShoutySnakeCase]to_shouty_snake_case().(), |
| 75 | KebabCaserust-analyzer cargo postgres-derive 0.4.9 case/RenameRule#KebabCase# => variant.to_kebab_caserust-analyzer cargo heck 0.5.0 kebab/impl#[str][ToKebabCase]to_kebab_case().(), |
| 76 | ScreamingKebabCaserust-analyzer cargo postgres-derive 0.4.9 case/RenameRule#ScreamingKebabCase# => variant.to_shouty_kebab_caserust-analyzer cargo heck 0.5.0 shouty_kebab/impl#[str][ToShoutyKebabCase]to_shouty_kebab_case().(), |
| 77 | TrainCaserust-analyzer cargo postgres-derive 0.4.9 case/RenameRule#TrainCase# => variant.to_train_caserust-analyzer cargo heck 0.5.0 train/impl#[str][ToTrainCase]to_train_case().(), |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 83 | fn rename_fieldrust-analyzer cargo postgres-derive 0.4.9 case/rename_field().() { |
| 84 | for &(original, lower, upper, camel, snake, screaming, kebab, screaming_kebab) in &[ |
| 85 | ( |
| 86 | "Outcome", "outcome", "OUTCOME", "outcome", "outcome", "OUTCOME", "outcome", "OUTCOME", |
| 87 | ), |
| 88 | ( |
| 89 | "VeryTasty", |
| 90 | "verytasty", |
| 91 | "VERYTASTY", |
| 92 | "veryTasty", |
| 93 | "very_tasty", |
| 94 | "VERY_TASTY", |
| 95 | "very-tasty", |
| 96 | "VERY-TASTY", |
| 97 | ), |
| 98 | ("A", "a", "A", "a", "a", "A", "a", "A"), |
| 99 | ("Z42", "z42", "Z42", "z42", "z42", "Z42", "z42", "Z42"), |
| 100 | ] { |
| 101 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(LowerCaserust-analyzer cargo postgres-derive 0.4.9 case/RenameRule#LowerCase#.apply_to_fieldrust-analyzer cargo postgres-derive 0.4.9 case/impl#[RenameRule]apply_to_field().(original), lower); |
| 102 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(UpperCaserust-analyzer cargo postgres-derive 0.4.9 case/RenameRule#UpperCase#.apply_to_fieldrust-analyzer cargo postgres-derive 0.4.9 case/impl#[RenameRule]apply_to_field().(original), upper); |
| 103 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(PascalCaserust-analyzer cargo postgres-derive 0.4.9 case/RenameRule#PascalCase#.apply_to_fieldrust-analyzer cargo postgres-derive 0.4.9 case/impl#[RenameRule]apply_to_field().(original), original); |
| 104 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(CamelCaserust-analyzer cargo postgres-derive 0.4.9 case/RenameRule#CamelCase#.apply_to_fieldrust-analyzer cargo postgres-derive 0.4.9 case/impl#[RenameRule]apply_to_field().(original), camel); |
| 105 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(SnakeCaserust-analyzer cargo postgres-derive 0.4.9 case/RenameRule#SnakeCase#.apply_to_fieldrust-analyzer cargo postgres-derive 0.4.9 case/impl#[RenameRule]apply_to_field().(original), snake); |
| 106 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(ScreamingSnakeCaserust-analyzer cargo postgres-derive 0.4.9 case/RenameRule#ScreamingSnakeCase#.apply_to_fieldrust-analyzer cargo postgres-derive 0.4.9 case/impl#[RenameRule]apply_to_field().(original), screaming); |
| 107 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(KebabCaserust-analyzer cargo postgres-derive 0.4.9 case/RenameRule#KebabCase#.apply_to_fieldrust-analyzer cargo postgres-derive 0.4.9 case/impl#[RenameRule]apply_to_field().(original), kebab); |
| 108 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(ScreamingKebabCaserust-analyzer cargo postgres-derive 0.4.9 case/RenameRule#ScreamingKebabCase#.apply_to_fieldrust-analyzer cargo postgres-derive 0.4.9 case/impl#[RenameRule]apply_to_field().(original), screaming_kebab); |
| 109 | } |
| 110 | } |