From 30913deb9cc21390d0b74eae5fcf7df04e5cee2e Mon Sep 17 00:00:00 2001 From: Jeremy Drouillard Date: Wed, 20 Jan 2021 22:01:35 -0800 Subject: [PATCH] Enum supports named fields --- macros/src/types/mod.rs | 7 ++++++- ts-rs/tests/union_with_data.rs | 27 ++++++++++++++++++++++++--- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/macros/src/types/mod.rs b/macros/src/types/mod.rs index f200de2..1eb6929 100644 --- a/macros/src/types/mod.rs +++ b/macros/src/types/mod.rs @@ -93,7 +93,12 @@ fn format_variant( quote!{<#ty as ts_rs::TS>::inline(0)} } }, - _ => panic!("Don't support variants with these kind of fields") + Fields::Named(named) => { + let ty = named::named(&name, rename_all, &named)?.inline; + + quote!(#ty) + } + } }); Ok(()) diff --git a/ts-rs/tests/union_with_data.rs b/ts-rs/tests/union_with_data.rs index f49030b..9661f3d 100644 --- a/ts-rs/tests/union_with_data.rs +++ b/ts-rs/tests/union_with_data.rs @@ -2,18 +2,39 @@ use ts_rs::TS; +#[derive(TS)] +struct Bar { + field: i32 +} + +#[derive(TS)] +struct Foo { + bar: Bar +} + #[derive(TS)] enum SimpleEnum { A(String), B(i64), C, - D(String, i64) + D(String, i64), + E(Foo), + F {a: i32, b: String} } #[test] fn test_simple_enum() { + assert_eq!(Foo::decl(), +r#"export interface Foo { + bar: Bar, +}"#); assert_eq!( SimpleEnum::decl(), - r#"export type SimpleEnum = string | number | "C" | [string, number];"# - ) +r#"export type SimpleEnum = string | number | "C" | [string, number] | { + bar: Bar, +} | { + a: number, + b: string, +};"# + ); }