removed non-serious cases and added serde aliases

This commit is contained in:
Tim 'Piepmatz' Hesse 2024-06-13 12:18:45 +02:00
parent b41bfc7134
commit d750d4942a
4 changed files with 23 additions and 15 deletions

View File

@ -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(),

View File

@ -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};

View File

@ -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};

View File

@ -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"]
}