Skip to content

Commit

Permalink
Support kebab case
Browse files Browse the repository at this point in the history
closes #239
  • Loading branch information
oscartbeaumont committed Jun 3, 2024
1 parent f3bc75e commit fadef54
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
36 changes: 20 additions & 16 deletions macros/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,22 +89,22 @@ impl Attribute {

pub fn parse_inflection(&self) -> Result<Inflection> {
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",
Expand Down Expand Up @@ -278,6 +278,8 @@ pub enum Inflection {
Snake,
Pascal,
ScreamingSnake,
Kebab,
ScreamingKebab,
}

impl Inflection {
Expand All @@ -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(),
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions tests/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down Expand Up @@ -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,
}

0 comments on commit fadef54

Please sign in to comment.