diff --git a/crates/nu-derive-value/src/attributes.rs b/crates/nu-derive-value/src/attributes.rs index 2fcad49b1e..9f7b15f561 100644 --- a/crates/nu-derive-value/src/attributes.rs +++ b/crates/nu-derive-value/src/attributes.rs @@ -29,22 +29,26 @@ impl ContainerAttributes { let ident = meta.path.require_ident()?; match ident.to_string().as_str() { "rename_all" => { + // The matched case are all useful variants from `convert_case` with aliases + // that `serde` uses. let case: LitStr = meta.value()?.parse()?; let case = match case.value().as_str() { - "UPPER CASE" => Case::Upper, - "lower case" => Case::Lower, + "UPPER CASE" | "UPPER WITH SPACES CASE" => Case::Upper, + "lower case" | "lower with spaces case" => Case::Lower, "Title Case" => Case::Title, - "tOGGLE cASE" => Case::Toggle, "camelCase" => Case::Camel, "PascalCase" | "UpperCamelCase" => Case::Pascal, "snake_case" => Case::Snake, "UPPER_SNAKE_CASE" | "SCREAMING_SNAKE_CASE" => Case::UpperSnake, "kebab-case" => Case::Kebab, - "COBOL-CASE" | "UPPER-KEBAB-CASE" => Case::Cobol, + "COBOL-CASE" | "UPPER-KEBAB-CASE" | "SCREAMING-KEBA-CASE" => { + Case::Cobol + } "Train-Case" => Case::Train, - "flatcase" => Case::Flat, - "UPPERFLATCASE" => Case::UpperFlat, - "aLtErNaTiNg CaSe" => Case::Alternating, + "flatcase" | "lowercase" => Case::Flat, + "UPPERFLATCASE" | "UPPERCASE" => Case::UpperFlat, + // Although very funny, we don't support `Case::{Toggle, Alternating}`, + // as we see no real benefit. c => { err = Err(DeriveError::InvalidAttributeValue { value_span: case.span(), diff --git a/crates/nu-protocol/src/value/from_value.rs b/crates/nu-protocol/src/value/from_value.rs index d96938adfd..1033eb03b2 100644 --- a/crates/nu-protocol/src/value/from_value.rs +++ b/crates/nu-protocol/src/value/from_value.rs @@ -27,8 +27,11 @@ use std::{ /// The expected value representation will be the name of the variant as a [`Value::String`]. /// By default, variant names will be expected in ["snake_case"](convert_case::Case::Snake). /// You can customize the case conversion using `#[nu_value(rename_all = "kebab-case")]` on the enum. -/// All deterministic case conversions provided by [`convert_case::Case`] are supported by -/// specifying the case name followed by "case". +/// All deterministic and useful case conversions provided by [`convert_case::Case`] are supported +/// by specifying the case name followed by "case". +/// Also all values for +/// [`#[serde(rename_all = "...")]`](https://serde.rs/container-attrs.html#rename_all) are valid +/// here. /// /// ``` /// # use nu_protocol::{FromValue, Value, ShellError}; diff --git a/crates/nu-protocol/src/value/into_value.rs b/crates/nu-protocol/src/value/into_value.rs index adb8cdc487..c27b19b651 100644 --- a/crates/nu-protocol/src/value/into_value.rs +++ b/crates/nu-protocol/src/value/into_value.rs @@ -18,8 +18,11 @@ use crate::{Record, ShellError, Span, Value}; /// The resulting value representation will be the name of the variant as a [`Value::String`]. /// By default, variant names will be converted to ["snake_case"](convert_case::Case::Snake). /// You can customize the case conversion using `#[nu_value(rename_all = "kebab-case")]` on the enum. -/// All deterministic case conversions provided by [`convert_case::Case`] are supported by -/// specifying the case name followed by "case". +/// All deterministic and useful case conversions provided by [`convert_case::Case`] are supported +/// by specifying the case name followed by "case". +/// Also all values for +/// [`#[serde(rename_all = "...")]`](https://serde.rs/container-attrs.html#rename_all) are valid +/// here. /// /// ``` /// # use nu_protocol::{IntoValue, Value, Span}; diff --git a/crates/nu-protocol/src/value/test_derive.rs b/crates/nu-protocol/src/value/test_derive.rs index aecb2237d1..1865a05418 100644 --- a/crates/nu-protocol/src/value/test_derive.rs +++ b/crates/nu-protocol/src/value/test_derive.rs @@ -327,7 +327,7 @@ fn enum_incorrect_type() { assert!(res.is_err()); } -// Generate the `Enum` from before but with all possible `rename_all` variants +// Generate the `Enum` from before but with all possible `rename_all` variants. macro_rules! enum_rename_all { ($($ident:ident: $case:literal => [$a1:literal, $b2:literal, $c3:literal]),*) => { $( @@ -374,7 +374,6 @@ enum_rename_all! { Upper: "UPPER CASE" => ["ALPHA ONE", "BETA TWO", "CHARLIE THREE"], Lower: "lower case" => ["alpha one", "beta two", "charlie three"], Title: "Title Case" => ["Alpha One", "Beta Two", "Charlie Three"], - Toggle: "tOGGLE cASE" => ["aLPHA oNE", "bETA tWO", "cHARLIE tHREE"], Camel: "camelCase" => ["alphaOne", "betaTwo", "charlieThree"], Pascal: "PascalCase" => ["AlphaOne", "BetaTwo", "CharlieThree"], Snake: "snake_case" => ["alpha_one", "beta_two", "charlie_three"], @@ -383,6 +382,5 @@ enum_rename_all! { Cobol: "COBOL-CASE" => ["ALPHA-ONE", "BETA-TWO", "CHARLIE-THREE"], Train: "Train-Case" => ["Alpha-One", "Beta-Two", "Charlie-Three"], Flat: "flatcase" => ["alphaone", "betatwo", "charliethree"], - UpperFlat: "UPPERFLATCASE" => ["ALPHAONE", "BETATWO", "CHARLIETHREE"], - Alternating: "aLtErNaTiNg CaSe" => ["aLpHa OnE", "bEtA tWo", "cHaRlIe ThReE"] + UpperFlat: "UPPERFLATCASE" => ["ALPHAONE", "BETATWO", "CHARLIETHREE"] }