Enum supports named fields

This commit is contained in:
Jeremy Drouillard
2021-01-20 22:01:35 -08:00
parent 9b5cc52a6b
commit 30913deb9c
2 changed files with 30 additions and 4 deletions
+6 -1
View File
@@ -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(())
+24 -3
View File
@@ -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,
};"#
);
}