Skip to content

Commit a106fe0

Browse files
committed
2.2.1: bug-fix for #[serde(tag)] outputting a string instead of a concatenated type list
1 parent 7203c65 commit a106fe0

File tree

7 files changed

+43
-9
lines changed

7 files changed

+43
-9
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "tsync"
33
description = "Generate typescript types from rust code."
4-
version = "2.2.0"
4+
version = "2.2.1"
55
readme = "README.md"
66
repository = "https://github.com/Wulf/tsync"
77
license = "MIT OR Apache-2.0"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<a href="https://crates.io/crates/tsync"><img src="https://img.shields.io/crates/v/tsync.svg?style=for-the-badge" height="20" alt="License: MIT OR Apache-2.0" /></a>
44

5-
A utility to generate typescript types from rust code.
5+
A utility to generate typescript types from rust code. Tries to conform to serde's serialized output.
66

77
# Install
88

src/to_typescript/enums.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,20 @@ impl super::ToTypescript for syn::ItemEnum {
3535
let casing = utils::get_attribute_arg("serde", "rename_all", &self.attrs);
3636
let casing = utils::parse_serde_case(casing);
3737

38+
// is_single means the enum has no variants with fields
39+
// i.e. `enum Foo { Bar, Baz }` rather than `enum Foo { Bar, Baz(String) }`
3840
let is_single = !self.variants.iter().any(|x| !x.fields.is_empty());
3941
state.write_comments(&comments, 0);
4042

41-
if is_single {
43+
// always use output the internally_tagged representation if the tag is present
44+
if let Some(tag_name) = utils::get_attribute_arg("serde", "tag", &self.attrs) {
45+
add_internally_tagged_enum(tag_name, self, state, casing, config.uses_type_interface)
46+
} else if is_single {
4247
if utils::has_attribute_arg("derive", "Serialize_repr", &self.attrs) {
4348
add_numeric_enum(self, state, casing, config)
4449
} else {
4550
add_enum(self, state, casing, config.uses_type_interface)
4651
}
47-
} else if let Some(tag_name) = utils::get_attribute_arg("serde", "tag", &self.attrs) {
48-
add_internally_tagged_enum(tag_name, self, state, casing, config.uses_type_interface)
4952
} else {
5053
add_externally_tagged_enum(self, state, casing, config.uses_type_interface)
5154
}
@@ -131,8 +134,16 @@ fn add_numeric_enum(
131134
casing: Option<Case>,
132135
config: &crate::BuildSettings,
133136
) {
134-
let declare = if config.uses_type_interface { "declare " } else { "export " };
135-
let const_ = if config.enable_const_enums { "const " } else { "" };
137+
let declare = if config.uses_type_interface {
138+
"declare "
139+
} else {
140+
"export "
141+
};
142+
let const_ = if config.enable_const_enums {
143+
"const "
144+
} else {
145+
""
146+
};
136147
state.types.push_str(&format!(
137148
"{declare}{const_}enum {interface_name} {{",
138149
interface_name = exported_struct.ident
@@ -289,7 +300,9 @@ fn add_externally_tagged_enum(
289300
// add discriminant
290301
state.types.push_str(&format!(" | {{ \"{}\":", field_name));
291302
for field in variant.fields {
292-
state.types.push_str(&format!(" {}", convert_type(&field.ty).ts_type,));
303+
state
304+
.types
305+
.push_str(&format!(" {}", convert_type(&field.ty).ts_type,));
293306
}
294307
state.types.push_str(" }");
295308
} else {

test/enum/rust.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,10 @@ enum AnimalTwo {
6464
DogLongExtra = 2,
6565
Cat,
6666
}
67+
68+
// Regression: if "serde(tag)" is specified, don't output a string.
69+
#[tsync]
70+
#[serde(tag = "type")]
71+
enum Tagged {
72+
Test // this should be { type: "Test" } in the TypeScript (not just the string "Test")
73+
}

test/enum/typescript.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,10 @@ type Animal =
6666

6767
type AnimalTwo =
6868
| "dog_long_extra" | "cat";
69+
70+
type Tagged =
71+
| Tagged__Test;
72+
73+
type Tagged__Test = {
74+
type: "Test";
75+
};

test/enum/typescript.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,10 @@ export type Animal =
6666

6767
export type AnimalTwo =
6868
| "dog_long_extra" | "cat";
69+
70+
export type Tagged =
71+
| Tagged__Test;
72+
73+
type Tagged__Test = {
74+
type: "Test";
75+
};

0 commit comments

Comments
 (0)