Skip to content

Commit

Permalink
allow for flattening of generic parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
NyxCode committed Jun 27, 2024
1 parent 96487d4 commit adf35bd
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ impl DerivedTS {
type WithoutGenerics = #generics;
fn name() -> String { stringify!(#generics).to_owned() }
fn inline() -> String { panic!("{} cannot be inlined", #name) }
fn inline_flattened() -> String { panic!("{} cannot be flattened", #name) }
fn inline_flattened() -> String { stringify!(#generics).to_owned() }
fn decl() -> String { panic!("{} cannot be declared", #name) }
fn decl_concrete() -> String { panic!("{} cannot be declared", #name) }
}
Expand Down
49 changes: 49 additions & 0 deletions ts-rs/tests/integration/generics_flatten.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use ts_rs_macros::TS;

// https://github.com/Aleph-Alpha/ts-rs/issues/335
#[derive(TS)]
#[ts(export, export_to = "generics/flatten/")]
struct Item<D> {
id: String,
#[ts(flatten)]
inner: D,
}

#[derive(TS)]
#[ts(export, export_to = "generics/flatten/")]
struct TwoParameters<A, B> {
id: String,
#[ts(flatten)]
a: A,
#[ts(flatten)]
b: B,
ab: (A, B),
}

#[derive(TS)]
#[ts(export, export_to = "generics/flatten/")]
enum Enum<A, B> {
A {
#[ts(flatten)]
a: A,
},
B {
#[ts(flatten)]
b: B,
},
AB(A, B),
}

#[test]
fn flattened_generic_parameters() {
use ts_rs::TS;

#[derive(TS)]
struct Inner {
x: i32,
}

assert_eq!(Item::<()>::decl(), "type Item<D> = { id: string, } & D;");
assert_eq!(TwoParameters::<(), ()>::decl(), "type TwoParameters<A, B> = { id: string, ab: [A, B], } & A & B;");
assert_eq!(Enum::<(), ()>::decl(), "type Enum<A, B> = { \"A\": A } | { \"B\": B } | { \"AB\": [A, B] };");
}
1 change: 1 addition & 0 deletions ts-rs/tests/integration/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ mod flatten;
mod generic_fields;
mod generic_without_import;
mod generics;
mod generics_flatten;
mod hashmap;
mod hashset;
mod imports;
Expand Down

0 comments on commit adf35bd

Please sign in to comment.