Skip to content
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
61 changes: 60 additions & 1 deletion src/to_typescript/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ impl super::ToTypescript for syn::ItemEnum {
let is_single = !self.variants.iter().any(|x| !x.fields.is_empty());
state.write_comments(&comments, 0);

// Handle untagged enum if serde has the tag untagged
if utils::get_attribute_arg("serde", "untagged", &self.attrs).is_some() {
add_untagged_tagged_enum(self, state, casing, config.uses_type_interface);
}
// always use output the internally_tagged representation if the tag is present
if let Some(tag_name) = utils::get_attribute_arg("serde", "tag", &self.attrs) {
else if let Some(tag_name) = utils::get_attribute_arg("serde", "tag", &self.attrs) {
let content_name = utils::get_attribute_arg("serde", "content", &self.attrs);
add_internally_tagged_enum(
tag_name,
Expand Down Expand Up @@ -398,3 +402,58 @@ fn add_externally_tagged_enum(
}
state.types.push_str(";\n");
}

fn add_untagged_tagged_enum(
exported_struct: syn::ItemEnum,
state: &mut BuildState,
casing: Option<Case>,
uses_type_interface: bool,
) {
let export = if uses_type_interface { "" } else { "export " };
let generics = utils::extract_struct_generics(exported_struct.generics.clone());

// Write type name and generics
state.types.push_str(&format!(
"{export}type {interface_name}{generics} =",
interface_name = exported_struct.ident,
generics = utils::format_generics(&generics)
));

// Loop over each variant of the enum
for variant in exported_struct.variants {
state.types.push('\n');
// Copy comments from rust
let comments = utils::get_comments(variant.attrs);
state.write_comments(&comments, 2);

// Unnamed fields:
// ```rs
// enum Data {
// Value1(i32)
// }
// ```
if let syn::Fields::Unnamed(fields) = &variant.fields {
// add discriminant
state.types.push_str(&format!(" | "));
super::structs::process_tuple_fields(fields.clone(), state);
state.types.push_str("");
}
// Named fields:
// ```rs
// enum Data {
// Value1 { v: i32 }
// }
// ```
else {
// add discriminant
state.types.push_str(&format!(" | {{\n"));

super::structs::process_fields(variant.fields, state, 6, casing, true);

state
.types
.push_str(&format!("{}}}", utils::build_indentation(4)));
}
}
state.types.push_str(";\n");
}
20 changes: 20 additions & 0 deletions test/issue-65-untagged-enums/rust.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/// test/rust.rs
use tsync::tsync;

#[derive(Serialize, Deserialize)]
#[tsync]
#[serde(untagged)]
enum Message {
ValueOne(i32, i32),
Value2(i32),
}

#[derive(Serialize, Deserialize)]
#[tsync]
#[serde(untagged)]
enum Message2<V, G> {
ValueOne { a: V, b: G },
Value2 { c: V },
Value3(G),
Value3(Vec<G>),
}
8 changes: 8 additions & 0 deletions test/issue-65-untagged-enums/tsync.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"

cd $SCRIPT_DIR

cargo run -- -i rust.rs -o typescript.d.ts
cargo run -- -i rust.rs -o typescript.ts
16 changes: 16 additions & 0 deletions test/issue-65-untagged-enums/typescript.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* This file is generated and managed by tsync */

type Message =
| [ number, number ]
| number;

type Message2<V, G> =
| {
a: V;
b: G;
}
| {
c: V;
}
| G
| Array<G>;
16 changes: 16 additions & 0 deletions test/issue-65-untagged-enums/typescript.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* This file is generated and managed by tsync */

export type Message =
| [ number, number ]
| number;

export type Message2<V, G> =
| {
a: V;
b: G;
}
| {
c: V;
}
| G
| Array<G>;
1 change: 1 addition & 0 deletions test/test_all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ cd $SCRIPT_DIR
./issue-43/tsync.sh
./issue-55/tsync.sh
./issue-58/tsync.sh
./issue-65-untagged-enums/tsync.sh
./raw_identifiers/tsync.sh
Loading