Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rename_all support for enum named variants #80

Merged
merged 3 commits into from
Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion crates/specta/macros/src/type/attr/variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::utils::*;

#[derive(Default)]
pub struct VariantAttr {
pub rename_all: Option<Inflection>,
pub rename: Option<String>,
pub skip: bool,
}
Expand All @@ -21,15 +22,24 @@ impl VariantAttr {
Ok(result)
}

fn merge(&mut self, VariantAttr { rename, skip }: VariantAttr) {
fn merge(
&mut self,
VariantAttr {
rename,
rename_all,
skip,
}: VariantAttr,
) {
self.rename = self.rename.take().or(rename);
self.rename_all = self.rename_all.take().or(rename_all);
self.skip = self.skip || skip;
}
}

impl_parse! {
VariantAttr(input, out) {
"rename" => out.rename = Some(parse_assign_str(input)?),
"rename_all" => out.rename_all = Some(parse_assign_inflection(input)?),
"skip" => out.skip = true,
}
}
Expand All @@ -38,6 +48,7 @@ impl_parse! {
impl_parse! {
SerdeVariantAttr(input, out) {
"rename" => out.0.rename = Some(parse_assign_str(input)?),
"rename_all" => out.0.rename_all = Some(parse_assign_inflection(input)?),
"skip" => out.0.skip = true,
"skip_serializing" => out.0.skip = true,
"skip_deserializing" => out.0.skip = true,
Expand Down
10 changes: 7 additions & 3 deletions crates/specta/macros/src/type/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,13 @@ pub fn parse_enum(
false,
);

let field_name = field_attrs
.rename
.unwrap_or_else(|| unraw_raw_ident(field.ident.as_ref().unwrap()));
let field_ident_str = unraw_raw_ident(field.ident.as_ref().unwrap());

let field_name = match (field_attrs.rename, attrs.rename_all) {
(Some(name), _) => name,
(_, Some(inflection)) => inflection.apply(&field_ident_str),
(_, _) => field_ident_str,
};

quote!(#crate_ref::ObjectField {
name: #field_name.to_string(),
Expand Down
25 changes: 12 additions & 13 deletions crates/specta/src/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,30 @@ impl EnumType {
let indexes = self
.variants
.iter()
.rev()
.filter(|v| match self.repr {
EnumRepr::External => match v {
EnumVariant::Unnamed(v) if v.fields.len() == 1 => true,
EnumVariant::Named(_) => true,
_ => false,
EnumVariant::Unnamed(v) if v.fields.len() == 1 => false,
EnumVariant::Named(_) => false,
_ => true,
},
EnumRepr::Untagged => match v {
EnumVariant::Unit(_) => true,
EnumVariant::Named(_) => true,
_ => false,
EnumVariant::Unit(_) => false,
EnumVariant::Named(_) => false,
_ => true,
},
EnumRepr::Adjacent { .. } => true,
EnumRepr::Adjacent { .. } => false,
EnumRepr::Internal { .. } => match v {
EnumVariant::Unit(_) => true,
EnumVariant::Named(_) => true,
_ => false,
EnumVariant::Unit(_) => false,
EnumVariant::Named(_) => false,
_ => true,
},
})
.enumerate()
.map(|(i, _)| i)
.collect::<Vec<_>>();

indexes.into_iter().for_each(|i| {
self.variants.swap_remove(i);
indexes.into_iter().rev().for_each(|i| {
self.variants.remove(i);
});
}
}
Expand Down
7 changes: 5 additions & 2 deletions crates/specta/tests/ts_rs/union.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ enum SimpleEnum {
#[specta(rename = "asdf")]
A,
B,
C,
#[specta(rename_all = "camelCase")]
C {
enum_field: (),
},
}

#[test]
Expand All @@ -20,6 +23,6 @@ fn test_empty() {
fn test_simple_enum() {
assert_eq!(
ts_export::<SimpleEnum>().unwrap(),
r#"export type SimpleEnum = "asdf" | "B" | "C""#
r#"export type SimpleEnum = "asdf" | "B" | { C: { enumField: null } }"#
)
}