From fadef54000e131008ff6e9dd7efe2ba36479c077 Mon Sep 17 00:00:00 2001 From: Oscar Beaumont Date: Mon, 3 Jun 2024 11:10:02 +0800 Subject: [PATCH] Support kebab case closes #239 --- macros/src/utils.rs | 36 ++++++++++++++++++++---------------- tests/ts.rs | 9 +++++++++ 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/macros/src/utils.rs b/macros/src/utils.rs index 39f2f13..92414c4 100644 --- a/macros/src/utils.rs +++ b/macros/src/utils.rs @@ -89,22 +89,22 @@ impl Attribute { pub fn parse_inflection(&self) -> Result { match &self.value { - Some(AttributeValue::Lit(Lit::Str(lit))) => { - Ok(match lit.value().to_lowercase().replace('_', "").as_str() { - "lowercase" => Inflection::Lower, - "uppercase" => Inflection::Upper, - "camelcase" => Inflection::Camel, - "snakecase" => Inflection::Snake, - "pascalcase" => Inflection::Pascal, - "screamingsnakecase" => Inflection::ScreamingSnake, - _ => { - return Err(syn::Error::new_spanned( - lit, - "specta: found string literal containing an unsupported inflection", - )) - } - }) - } + Some(AttributeValue::Lit(Lit::Str(lit))) => Ok(match lit.value().as_str() { + "lowercase" => Inflection::Lower, + "UPPERCASE" => Inflection::Upper, + "PascalCase" => Inflection::Pascal, + "camelCase" => Inflection::Camel, + "snake_case" => Inflection::Snake, + "SCREAMING_SNAKE_CASE" => Inflection::ScreamingSnake, + "kebab-case" => Inflection::Kebab, + "SCREAMING-KEBAB-CASE" => Inflection::ScreamingKebab, + _ => { + return Err(syn::Error::new_spanned( + lit, + "specta: found string literal containing an unsupported inflection", + )) + } + }), _ => Err(syn::Error::new( self.value_span(), "specta: expected string literal containing an inflection", @@ -278,6 +278,8 @@ pub enum Inflection { Snake, Pascal, ScreamingSnake, + Kebab, + ScreamingKebab, } impl Inflection { @@ -291,6 +293,8 @@ impl Inflection { Inflection::Snake => string.to_snake_case(), Inflection::Pascal => string.to_pascal_case(), Inflection::ScreamingSnake => string.to_screaming_snake_case(), + Inflection::Kebab => string.to_kebab_case(), + Inflection::ScreamingKebab => string.to_kebab_case().to_uppercase(), } } } diff --git a/tests/ts.rs b/tests/ts.rs index 55cf0fe..28db099 100644 --- a/tests/ts.rs +++ b/tests/ts.rs @@ -278,6 +278,9 @@ fn typescript_types() { r#"{ type: "A" } | { type: "B"; data: string }"# ); + // https://github.com/oscartbeaumont/specta/issues/239 + assert_ts!(KebabCase, r#"{ "test-ing": string }"#); + // https://github.com/oscartbeaumont/specta/issues/90 assert_ts!(RenameWithWeirdCharsField, r#"{ "@odata.context": string }"#); assert_ts!( @@ -656,3 +659,9 @@ pub enum SkippedFieldWithinVariant { A(#[serde(skip)] String), B(String), } + +#[derive(Type)] +#[serde(rename_all = "kebab-case")] +pub struct KebabCase { + test_ing: String, +}