Skip to content

Commit

Permalink
Add support for fields_rename_all container attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
dbeckwith committed Oct 14, 2024
1 parent 8a34b02 commit 17ab9de
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## vNEXT

* Add support for `#[serde(fields_rename_all)]` ([#35](https://github.com/dbeckwith/rust-typescript-type-def/issues/35)).

## v0.5.12

* Export `Blob` newtype wrapper around `Vec<u8>` which has the Typescript type [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob).
Expand Down
32 changes: 26 additions & 6 deletions derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ struct TypeDefInput {
#[darling(default)]
rename_all: Option<SpannedValue<String>>,
#[darling(default)]
rename_all_fields: Option<SpannedValue<String>>,
#[darling(default)]
rename: Option<SpannedValue<String>>,
#[darling(default)]
#[allow(dead_code)] // doesn't affect JSON
Expand Down Expand Up @@ -289,6 +291,7 @@ fn make_info_def(
content,
untagged,
rename_all,
rename_all_fields,
rename,
..
}: &TypeDefInput,
Expand Down Expand Up @@ -343,21 +346,35 @@ fn make_info_def(
match style {
ast::Style::Unit => type_expr_ident("null"),
ast::Style::Tuple => fields_to_type_expr(
fields, false, rename_all, generics, None,
fields,
false,
rename_all.as_ref(),
generics,
None,
),
ast::Style::Struct => {
if fields.is_empty() {
type_expr_object([], None)
} else {
fields_to_type_expr(
fields, true, rename_all, generics, None,
fields,
true,
rename_all.as_ref(),
generics,
None,
)
}
}
}
}
ast::Data::Enum(variants) => variants_to_type_expr(
variants, tag, content, untagged, rename_all, generics,
variants,
tag,
content,
untagged,
rename_all,
rename_all_fields,
generics,
),
},
generics
Expand All @@ -383,7 +400,7 @@ fn make_info_def(
fn fields_to_type_expr(
fields: &[TypeDefField],
named: bool,
rename_all: &Option<SpannedValue<String>>,
rename_all: Option<&SpannedValue<String>>,
generics: &Generics,
docs: Option<&Expr>,
) -> Expr {
Expand Down Expand Up @@ -490,6 +507,7 @@ fn variants_to_type_expr(
content: &Option<SpannedValue<String>>,
untagged: &SpannedValue<Flag>,
variant_rename_all: &Option<SpannedValue<String>>,
fields_rename_all: &Option<SpannedValue<String>>,
generics: &Generics,
) -> Expr {
type_expr_union(
Expand All @@ -505,9 +523,11 @@ fn variants_to_type_expr(
let variant_name = serde_rename_ident(
variant_name,
variant_rename,
variant_rename_all,
variant_rename_all.as_ref(),
false,
);
let field_rename_all =
field_rename_all.as_ref().or(fields_rename_all.as_ref());
match (tag, content, ***untagged) {
(None, None, false) => match style {
ast::Style::Unit => type_expr_string(
Expand Down Expand Up @@ -909,7 +929,7 @@ fn wrap_optional_docs(docs: Option<&Expr>) -> Expr {
fn serde_rename_ident(
ident: &Ident,
rename: &Option<SpannedValue<String>>,
rename_all: &Option<SpannedValue<String>>,
rename_all: Option<&SpannedValue<String>>,
is_field: bool,
) -> LitStr {
let span = ident.span();
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ pub use crate::impls::Blob;
/// |:-|:-:|
/// | [`#[serde(rename = "name")]`](https://serde.rs/container-attrs.html#rename) | ✓ |
/// | [`#[serde(rename_all = "...")]`](https://serde.rs/container-attrs.html#rename_all) | ✓ |
/// | [`#[serde(rename_all_fields = "...")]`](https://serde.rs/container-attrs.html#rename_all_fields) | ✓ |
/// | [`#[serde(deny_unknown_fields)]`](https://serde.rs/container-attrs.html#deny_unknown_fields) | ? |
/// | [`#[serde(tag = "type")]`](https://serde.rs/container-attrs.html#tag) | ✓ |
/// | [`#[serde(tag = "t", content = "c")]`](https://serde.rs/container-attrs.html#tag--content) | ✓ |
Expand Down
33 changes: 32 additions & 1 deletion tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,29 @@ mod derive {
f: Option<Test9>,
g: (),
h: std::net::IpAddr,
i: Test11,
},
C(Parent),
D,
E {},
F(),
}

#[derive(Serialize, TypeDef)]
#[serde(rename_all_fields = "kebab-case")]
enum Test11 {
A {
a_a: usize,
},
B {
b_a: usize,
},
#[serde(rename_all = "UPPERCASE")]
C {
c_a: usize,
},
}

#[test]
fn emit() {
assert_eq_str!(
Expand Down Expand Up @@ -221,6 +237,19 @@ export namespace types {
export type TEST_8 = {
};
export type Test9 = never;
export type Test11 = ({
"A": {
"a-a": types.Usize;
};
} | {
"B": {
"b-a": types.Usize;
};
} | {
"C": {
"C_A": types.Usize;
};
});
export type Test10 = ({
"type": "A";
"value": (types.Parent & {
Expand All @@ -238,6 +267,7 @@ export namespace types {
"F": (types.Test9 | null);
"G": null;
"H": string;
"I": types.Test11;
};
} | {
"type": "C";
Expand Down Expand Up @@ -285,9 +315,10 @@ export namespace types {
f: None,
g: (),
h: std::net::IpAddr::from_str("::1").unwrap(),
i: Test11::A { a_a: 0 },
})
.unwrap(),
r#"{"type":"B","value":{"A":[{"FOO_BAR":123,"a":"foo","b":null,"c":[true,false],"FFF":"f","g":{"Ok":"test"},"h":{"Err":1234},"i":["test"]},4,"bar"],"B":"cool-beans","C":{"B":[42,"baz"]},"D":null,"E":{},"F":null,"G":null,"H":"::1"}}"#
r#"{"type":"B","value":{"A":[{"FOO_BAR":123,"a":"foo","b":null,"c":[true,false],"FFF":"f","g":{"Ok":"test"},"h":{"Err":1234},"i":["test"]},4,"bar"],"B":"cool-beans","C":{"B":[42,"baz"]},"D":null,"E":{},"F":null,"G":null,"H":"::1","I":{"A":{"a-a":0}}}}"#
);
}

Expand Down

0 comments on commit 17ab9de

Please sign in to comment.