From 68f2f2252a95fe540bd901afebb81cf5429db034 Mon Sep 17 00:00:00 2001 From: Gustavo Shigueo Date: Fri, 5 Jul 2024 14:47:18 -0300 Subject: [PATCH] Change `HashMap` to export mapped types (#339) --- ts-rs/src/lib.rs | 4 +- ts-rs/tests/integration/generics.rs | 856 ++++++++++---------- ts-rs/tests/integration/hashmap.rs | 144 ++-- ts-rs/tests/integration/indexmap.rs | 40 +- ts-rs/tests/integration/issue_168.rs | 4 +- ts-rs/tests/integration/issue_338.rs | 37 + ts-rs/tests/integration/issue_70.rs | 154 ++-- ts-rs/tests/integration/lifetimes.rs | 72 +- ts-rs/tests/integration/main.rs | 1 + ts-rs/tests/integration/self_referential.rs | 376 ++++----- ts-rs/tests/integration/serde_json.rs | 130 +-- 11 files changed, 928 insertions(+), 890 deletions(-) create mode 100644 ts-rs/tests/integration/issue_338.rs diff --git a/ts-rs/src/lib.rs b/ts-rs/src/lib.rs index 58ff6e0..efa128a 100644 --- a/ts-rs/src/lib.rs +++ b/ts-rs/src/lib.rs @@ -896,11 +896,11 @@ impl TS for HashMap { } fn name() -> String { - format!("{{ [key: {}]: {} }}", K::name(), V::name()) + format!("{{ [key in {}]?: {} }}", K::name(), V::name()) } fn inline() -> String { - format!("{{ [key: {}]: {} }}", K::inline(), V::inline()) + format!("{{ [key in {}]?: {} }}", K::inline(), V::inline()) } fn visit_dependencies(v: &mut impl TypeVisitor) diff --git a/ts-rs/tests/integration/generics.rs b/ts-rs/tests/integration/generics.rs index fdf4ec2..4140672 100644 --- a/ts-rs/tests/integration/generics.rs +++ b/ts-rs/tests/integration/generics.rs @@ -1,428 +1,428 @@ -#![allow(clippy::box_collection, clippy::enum_variant_names, dead_code)] -#![allow(dead_code)] - -use std::{ - collections::{BTreeMap, HashSet}, - fmt::Debug, - rc::Rc, -}; - -use ts_rs::TS; - -#[derive(TS)] -#[ts(export, export_to = "generics/")] -struct Generic -where - T: TS, -{ - value: T, - values: Vec, -} - -#[derive(TS)] -#[ts(export, export_to = "generics/")] -struct GenericAutoBound { - value: T, - values: Vec, -} - -#[derive(TS)] -#[ts(export, export_to = "generics/")] -struct GenericAutoBound2 -where - T: PartialEq, -{ - value: T, - values: Vec, -} - -#[derive(TS)] -#[ts(export, export_to = "generics/")] -struct Container { - foo: Generic, - bar: Box>>, - baz: Box>>>, -} - -macro_rules! declare { - ($(#[$meta:meta])* $name:ident { $($fident:ident: $t:ty),+ $(,)? }) => { - $(#[$meta])* - struct $name { - $(pub $fident: $t),+ - } - } -} - -declare! { - #[derive(TS)] - #[ts(export, export_to = "generics/")] - TypeGroup { - foo: Vec, - } -} - -#[test] -fn test() { - assert_eq!( - TypeGroup::decl(), - "type TypeGroup = { foo: Array, };", - ); - - assert_eq!( - Generic::<()>::decl(), - "type Generic = { value: T, values: Array, };" - ); - - assert_eq!( - GenericAutoBound::<()>::decl(), - "type GenericAutoBound = { value: T, values: Array, };" - ); - - assert_eq!( - GenericAutoBound2::<()>::decl(), - "type GenericAutoBound2 = { value: T, values: Array, };" - ); - - assert_eq!( - Container::decl(), - "type Container = { foo: Generic, bar: Array>, baz: { [key: string]: Generic }, };" - ); -} - -#[derive(TS)] -#[ts(export, export_to = "generics/")] -enum GenericEnum { - A(A), - B(B, B, B), - C(Vec), - D(Vec>>), - E { a: A, b: B, c: C }, - X(Vec), - Y(i32), - Z(Vec>), -} - -#[test] -fn generic_enum() { - assert_eq!( - GenericEnum::<(), (), ()>::decl(), - r#"type GenericEnum = { "A": A } | { "B": [B, B, B] } | { "C": Array } | { "D": Array>> } | { "E": { a: A, b: B, c: C, } } | { "X": Array } | { "Y": number } | { "Z": Array> };"# - ) -} - -#[derive(TS)] -#[ts(export, export_to = "generics/")] -struct NewType(Vec>); - -#[test] -fn generic_newtype() { - assert_eq!( - NewType::<()>::decl(), - r#"type NewType = Array>;"# - ); -} - -#[derive(TS)] -#[ts(export, export_to = "generics/")] -struct Tuple(T, Vec, Vec>); - -#[test] -fn generic_tuple() { - assert_eq!( - Tuple::<()>::decl(), - r#"type Tuple = [T, Array, Array>];"# - ); -} - -#[derive(TS)] -#[ts(export, export_to = "generics/")] -struct Struct { - a: T, - b: (T, T), - c: (T, (T, T)), - d: [T; 3], - e: [(T, T); 3], - f: Vec, - g: Vec>, - h: Vec<[(T, T); 3]>, -} - -#[test] -fn generic_struct() { - assert_eq!( - Struct::<()>::decl(), - "type Struct = { a: T, b: [T, T], c: [T, [T, T]], d: [T, T, T], e: [[T, T], [T, T], [T, T]], f: Array, g: Array>, h: Array<[[T, T], [T, T], [T, T]]>, };" - ) -} - -#[derive(TS)] -#[ts(export, export_to = "generics/")] -struct GenericInline { - t: T, -} - -#[derive(TS)] -#[ts(export, export_to = "generics/")] -struct ContainerInline { - g: GenericInline, - #[ts(inline)] - gi: GenericInline, - #[ts(flatten)] - t: GenericInline>, -} - -#[test] -fn inline() { - assert_eq!( - GenericInline::<()>::decl(), - "type GenericInline = { t: T, };" - ); - assert_eq!( - ContainerInline::decl(), - "type ContainerInline = { g: GenericInline, gi: { t: string, }, t: Array, };" - ); -} - -#[derive(TS)] -#[ts(export, export_to = "generics/")] -struct GenericWithBounds { - t: T, -} - -#[derive(TS)] -#[ts(export, export_to = "generics/")] -struct ContainerWithBounds { - g: GenericWithBounds, - - #[ts(inline)] - gi: GenericWithBounds, - - #[ts(flatten)] - t: GenericWithBounds, -} - -#[test] -fn inline_with_bounds() { - assert_eq!( - GenericWithBounds::<&'static str>::decl(), - "type GenericWithBounds = { t: T, };" - ); - assert_eq!( - ContainerWithBounds::decl(), - "type ContainerWithBounds = { g: GenericWithBounds, gi: { t: string, }, t: number, };" - ); -} - -#[derive(TS)] -#[ts(export, export_to = "generics/")] -struct GenericWithDefault { - t: T, -} - -#[derive(TS)] -#[ts(export, export_to = "generics/")] -struct ContainerWithDefault { - g: GenericWithDefault, - - #[ts(inline)] - gi: GenericWithDefault, - - #[ts(flatten)] - t: GenericWithDefault, -} - -#[test] -fn inline_with_default() { - assert_eq!( - GenericWithDefault::<()>::decl(), - "type GenericWithDefault = { t: T, };" - ); - assert_eq!( - ContainerWithDefault::decl(), - "type ContainerWithDefault = { g: GenericWithDefault, gi: { t: string, }, t: number, };" - ); -} - -#[derive(TS)] -#[ts(export, export_to = "generics/")] -struct ADefault { - t: T, -} - -#[derive(TS)] -#[ts(export, export_to = "generics/")] -struct BDefault>> { - u: U, -} - -#[derive(TS)] -#[ts(export, export_to = "generics/")] -struct YDefault { - a1: ADefault, - a2: ADefault, -} - -#[test] -fn default() { - assert_eq!( - ADefault::<()>::decl(), - "type ADefault = { t: T, };" - ); - - assert_eq!( - BDefault::<()>::decl(), - "type BDefault | null> = { u: U, };" - ); - assert!(BDefault::<()>::dependencies() - .iter() - .any(|dep| dep.ts_name == "ADefault")); - - assert_eq!( - YDefault::decl(), - "type YDefault = { a1: ADefault, a2: ADefault, };" - ) -} - -#[derive(TS)] -#[ts(export, export_to = "generics/")] -struct ATraitBounds { - t: T, -} - -#[derive(TS)] -#[ts(export, export_to = "generics/")] -struct BTraitBounds(T); - -#[derive(TS)] -#[ts(export, export_to = "generics/")] -enum CTraitBounds { - A { t: T }, - B(T), - C, - D(T, K), -} - -// Types with const generics can't be exported -#[derive(TS)] -struct DTraitBounds { - t: [T; N], -} - -#[test] -fn trait_bounds() { - assert_eq!( - ATraitBounds::::decl(), - "type ATraitBounds = { t: T, };" - ); - - assert_eq!( - BTraitBounds::<&'static str>::decl(), - "type BTraitBounds = T;" - ); - - assert_eq!( - CTraitBounds::<&'static str, i32>::decl(), - r#"type CTraitBounds = { "A": { t: T, } } | { "B": T } | "C" | { "D": [T, K] };"# - ); - - let ty = format!( - "type DTraitBounds = {{ t: [{}], }};", - "T, ".repeat(41).trim_end_matches(", ") - ); - assert_eq!(DTraitBounds::<&str, 41>::decl(), ty) -} - -#[derive(TS)] -#[ts(export, export_to = "generics/")] -struct T0 { - t0: T, -} - -#[derive(TS)] -#[ts(export, export_to = "generics/")] -struct P0 { - p0: T, -} - -#[derive(TS)] -#[ts(export, export_to = "generics/")] -struct T1 { - t0: T, -} - -#[derive(TS)] -#[ts(export, export_to = "generics/")] -struct P1 { - p0: T, -} - -#[derive(TS)] -#[ts(export, export_to = "generics/")] -struct Parent { - a: T1>, - b: T1>>>, - c: T1>, -} - -#[derive(TS)] -#[ts(export, export_to = "generics/")] -struct GenericParent { - a_t: T1>, - b_t: T1>>>, - c_t: T1>, - a_null: T1>, - b_null: T1>>>, - c_null: T1>, -} - -#[test] -fn deeply_nested() { - assert_eq!( - Parent::inline(), - "{ a: T1>, b: T1>>>, c: T1>, }" - ); - assert_eq!( - GenericParent::<()>::decl(), - "type GenericParent = { \ - a_t: T1>, \ - b_t: T1>>>, \ - c_t: T1>, \ - a_null: T1>, \ - b_null: T1>>>, \ - c_null: T1>, \ - };" - ); -} - -#[derive(TS)] -#[ts(export, export_to = "generics/")] -struct SomeType(String); - -#[derive(TS)] -#[ts(export, export_to = "generics/")] -enum MyEnum { - VariantA(A), - VariantB(B), -} - -#[derive(TS)] -#[ts(export, export_to = "generics/")] -struct ParentEnum { - e: MyEnum, - #[ts(inline)] - e1: MyEnum, -} - -#[test] -fn inline_generic_enum() { - // This fails! - // The #[ts(inline)] seems to inline recursively, so not only the definition of `MyEnum`, but - // also the definition of `SomeType`. - assert_eq!( - ParentEnum::decl(), - "type ParentEnum = { \ - e: MyEnum, \ - e1: { \"VariantA\": number } | { \"VariantB\": SomeType }, \ - };" - ); -} +#![allow(clippy::box_collection, clippy::enum_variant_names, dead_code)] +#![allow(dead_code)] + +use std::{ + collections::{BTreeMap, HashSet}, + fmt::Debug, + rc::Rc, +}; + +use ts_rs::TS; + +#[derive(TS)] +#[ts(export, export_to = "generics/")] +struct Generic +where + T: TS, +{ + value: T, + values: Vec, +} + +#[derive(TS)] +#[ts(export, export_to = "generics/")] +struct GenericAutoBound { + value: T, + values: Vec, +} + +#[derive(TS)] +#[ts(export, export_to = "generics/")] +struct GenericAutoBound2 +where + T: PartialEq, +{ + value: T, + values: Vec, +} + +#[derive(TS)] +#[ts(export, export_to = "generics/")] +struct Container { + foo: Generic, + bar: Box>>, + baz: Box>>>, +} + +macro_rules! declare { + ($(#[$meta:meta])* $name:ident { $($fident:ident: $t:ty),+ $(,)? }) => { + $(#[$meta])* + struct $name { + $(pub $fident: $t),+ + } + } +} + +declare! { + #[derive(TS)] + #[ts(export, export_to = "generics/")] + TypeGroup { + foo: Vec, + } +} + +#[test] +fn test() { + assert_eq!( + TypeGroup::decl(), + "type TypeGroup = { foo: Array, };", + ); + + assert_eq!( + Generic::<()>::decl(), + "type Generic = { value: T, values: Array, };" + ); + + assert_eq!( + GenericAutoBound::<()>::decl(), + "type GenericAutoBound = { value: T, values: Array, };" + ); + + assert_eq!( + GenericAutoBound2::<()>::decl(), + "type GenericAutoBound2 = { value: T, values: Array, };" + ); + + assert_eq!( + Container::decl(), + "type Container = { foo: Generic, bar: Array>, baz: { [key in string]?: Generic }, };" + ); +} + +#[derive(TS)] +#[ts(export, export_to = "generics/")] +enum GenericEnum { + A(A), + B(B, B, B), + C(Vec), + D(Vec>>), + E { a: A, b: B, c: C }, + X(Vec), + Y(i32), + Z(Vec>), +} + +#[test] +fn generic_enum() { + assert_eq!( + GenericEnum::<(), (), ()>::decl(), + r#"type GenericEnum = { "A": A } | { "B": [B, B, B] } | { "C": Array } | { "D": Array>> } | { "E": { a: A, b: B, c: C, } } | { "X": Array } | { "Y": number } | { "Z": Array> };"# + ) +} + +#[derive(TS)] +#[ts(export, export_to = "generics/")] +struct NewType(Vec>); + +#[test] +fn generic_newtype() { + assert_eq!( + NewType::<()>::decl(), + r#"type NewType = Array>;"# + ); +} + +#[derive(TS)] +#[ts(export, export_to = "generics/")] +struct Tuple(T, Vec, Vec>); + +#[test] +fn generic_tuple() { + assert_eq!( + Tuple::<()>::decl(), + r#"type Tuple = [T, Array, Array>];"# + ); +} + +#[derive(TS)] +#[ts(export, export_to = "generics/")] +struct Struct { + a: T, + b: (T, T), + c: (T, (T, T)), + d: [T; 3], + e: [(T, T); 3], + f: Vec, + g: Vec>, + h: Vec<[(T, T); 3]>, +} + +#[test] +fn generic_struct() { + assert_eq!( + Struct::<()>::decl(), + "type Struct = { a: T, b: [T, T], c: [T, [T, T]], d: [T, T, T], e: [[T, T], [T, T], [T, T]], f: Array, g: Array>, h: Array<[[T, T], [T, T], [T, T]]>, };" + ) +} + +#[derive(TS)] +#[ts(export, export_to = "generics/")] +struct GenericInline { + t: T, +} + +#[derive(TS)] +#[ts(export, export_to = "generics/")] +struct ContainerInline { + g: GenericInline, + #[ts(inline)] + gi: GenericInline, + #[ts(flatten)] + t: GenericInline>, +} + +#[test] +fn inline() { + assert_eq!( + GenericInline::<()>::decl(), + "type GenericInline = { t: T, };" + ); + assert_eq!( + ContainerInline::decl(), + "type ContainerInline = { g: GenericInline, gi: { t: string, }, t: Array, };" + ); +} + +#[derive(TS)] +#[ts(export, export_to = "generics/")] +struct GenericWithBounds { + t: T, +} + +#[derive(TS)] +#[ts(export, export_to = "generics/")] +struct ContainerWithBounds { + g: GenericWithBounds, + + #[ts(inline)] + gi: GenericWithBounds, + + #[ts(flatten)] + t: GenericWithBounds, +} + +#[test] +fn inline_with_bounds() { + assert_eq!( + GenericWithBounds::<&'static str>::decl(), + "type GenericWithBounds = { t: T, };" + ); + assert_eq!( + ContainerWithBounds::decl(), + "type ContainerWithBounds = { g: GenericWithBounds, gi: { t: string, }, t: number, };" + ); +} + +#[derive(TS)] +#[ts(export, export_to = "generics/")] +struct GenericWithDefault { + t: T, +} + +#[derive(TS)] +#[ts(export, export_to = "generics/")] +struct ContainerWithDefault { + g: GenericWithDefault, + + #[ts(inline)] + gi: GenericWithDefault, + + #[ts(flatten)] + t: GenericWithDefault, +} + +#[test] +fn inline_with_default() { + assert_eq!( + GenericWithDefault::<()>::decl(), + "type GenericWithDefault = { t: T, };" + ); + assert_eq!( + ContainerWithDefault::decl(), + "type ContainerWithDefault = { g: GenericWithDefault, gi: { t: string, }, t: number, };" + ); +} + +#[derive(TS)] +#[ts(export, export_to = "generics/")] +struct ADefault { + t: T, +} + +#[derive(TS)] +#[ts(export, export_to = "generics/")] +struct BDefault>> { + u: U, +} + +#[derive(TS)] +#[ts(export, export_to = "generics/")] +struct YDefault { + a1: ADefault, + a2: ADefault, +} + +#[test] +fn default() { + assert_eq!( + ADefault::<()>::decl(), + "type ADefault = { t: T, };" + ); + + assert_eq!( + BDefault::<()>::decl(), + "type BDefault | null> = { u: U, };" + ); + assert!(BDefault::<()>::dependencies() + .iter() + .any(|dep| dep.ts_name == "ADefault")); + + assert_eq!( + YDefault::decl(), + "type YDefault = { a1: ADefault, a2: ADefault, };" + ) +} + +#[derive(TS)] +#[ts(export, export_to = "generics/")] +struct ATraitBounds { + t: T, +} + +#[derive(TS)] +#[ts(export, export_to = "generics/")] +struct BTraitBounds(T); + +#[derive(TS)] +#[ts(export, export_to = "generics/")] +enum CTraitBounds { + A { t: T }, + B(T), + C, + D(T, K), +} + +// Types with const generics can't be exported +#[derive(TS)] +struct DTraitBounds { + t: [T; N], +} + +#[test] +fn trait_bounds() { + assert_eq!( + ATraitBounds::::decl(), + "type ATraitBounds = { t: T, };" + ); + + assert_eq!( + BTraitBounds::<&'static str>::decl(), + "type BTraitBounds = T;" + ); + + assert_eq!( + CTraitBounds::<&'static str, i32>::decl(), + r#"type CTraitBounds = { "A": { t: T, } } | { "B": T } | "C" | { "D": [T, K] };"# + ); + + let ty = format!( + "type DTraitBounds = {{ t: [{}], }};", + "T, ".repeat(41).trim_end_matches(", ") + ); + assert_eq!(DTraitBounds::<&str, 41>::decl(), ty) +} + +#[derive(TS)] +#[ts(export, export_to = "generics/")] +struct T0 { + t0: T, +} + +#[derive(TS)] +#[ts(export, export_to = "generics/")] +struct P0 { + p0: T, +} + +#[derive(TS)] +#[ts(export, export_to = "generics/")] +struct T1 { + t0: T, +} + +#[derive(TS)] +#[ts(export, export_to = "generics/")] +struct P1 { + p0: T, +} + +#[derive(TS)] +#[ts(export, export_to = "generics/")] +struct Parent { + a: T1>, + b: T1>>>, + c: T1>, +} + +#[derive(TS)] +#[ts(export, export_to = "generics/")] +struct GenericParent { + a_t: T1>, + b_t: T1>>>, + c_t: T1>, + a_null: T1>, + b_null: T1>>>, + c_null: T1>, +} + +#[test] +fn deeply_nested() { + assert_eq!( + Parent::inline(), + "{ a: T1>, b: T1>>>, c: T1>, }" + ); + assert_eq!( + GenericParent::<()>::decl(), + "type GenericParent = { \ + a_t: T1>, \ + b_t: T1>>>, \ + c_t: T1>, \ + a_null: T1>, \ + b_null: T1>>>, \ + c_null: T1>, \ + };" + ); +} + +#[derive(TS)] +#[ts(export, export_to = "generics/")] +struct SomeType(String); + +#[derive(TS)] +#[ts(export, export_to = "generics/")] +enum MyEnum { + VariantA(A), + VariantB(B), +} + +#[derive(TS)] +#[ts(export, export_to = "generics/")] +struct ParentEnum { + e: MyEnum, + #[ts(inline)] + e1: MyEnum, +} + +#[test] +fn inline_generic_enum() { + // This fails! + // The #[ts(inline)] seems to inline recursively, so not only the definition of `MyEnum`, but + // also the definition of `SomeType`. + assert_eq!( + ParentEnum::decl(), + "type ParentEnum = { \ + e: MyEnum, \ + e1: { \"VariantA\": number } | { \"VariantB\": SomeType }, \ + };" + ); +} diff --git a/ts-rs/tests/integration/hashmap.rs b/ts-rs/tests/integration/hashmap.rs index e6070b0..6d662f1 100644 --- a/ts-rs/tests/integration/hashmap.rs +++ b/ts-rs/tests/integration/hashmap.rs @@ -1,72 +1,72 @@ -#![allow(dead_code)] - -use std::collections::{BTreeMap, HashMap, HashSet}; - -use ts_rs::TS; - -#[derive(TS)] -#[ts(export, export_to = "hashmap/")] -struct Hashes { - map: HashMap, - set: HashSet, -} - -#[test] -fn hashmap() { - assert_eq!( - Hashes::decl(), - "type Hashes = { map: { [key: string]: string }, set: Array, };" - ) -} - -struct CustomHasher {} - -type CustomHashMap = HashMap; -type CustomHashSet = HashSet; - -#[derive(TS)] -#[ts(export, export_to = "hashmap/")] -struct HashesHasher { - map: CustomHashMap, - set: CustomHashSet, -} - -#[test] -fn hashmap_with_custom_hasher() { - assert_eq!( - HashesHasher::decl(), - "type HashesHasher = { map: { [key: string]: string }, set: Array, };" - ) -} - -#[derive(TS, Eq, PartialEq, Hash)] -#[ts(export, export_to = "hashmap/")] -struct CustomKey(String); - -#[derive(TS)] -#[ts(export, export_to = "hashmap/")] -struct CustomValue; - -#[derive(TS)] -#[ts(export, export_to = "hashmap/")] -struct HashMapWithCustomTypes { - map: HashMap, -} - -#[derive(TS)] -#[ts(export, export_to = "hashmap/")] -struct BTreeMapWithCustomTypes { - map: BTreeMap, -} - -#[test] -fn with_custom_types() { - assert_eq!( - HashMapWithCustomTypes::inline(), - BTreeMapWithCustomTypes::inline() - ); - assert_eq!( - HashMapWithCustomTypes::decl(), - "type HashMapWithCustomTypes = { map: { [key: CustomKey]: CustomValue }, };" - ); -} +#![allow(dead_code)] + +use std::collections::{BTreeMap, HashMap, HashSet}; + +use ts_rs::TS; + +#[derive(TS)] +#[ts(export, export_to = "hashmap/")] +struct Hashes { + map: HashMap, + set: HashSet, +} + +#[test] +fn hashmap() { + assert_eq!( + Hashes::decl(), + "type Hashes = { map: { [key in string]?: string }, set: Array, };" + ) +} + +struct CustomHasher {} + +type CustomHashMap = HashMap; +type CustomHashSet = HashSet; + +#[derive(TS)] +#[ts(export, export_to = "hashmap/")] +struct HashesHasher { + map: CustomHashMap, + set: CustomHashSet, +} + +#[test] +fn hashmap_with_custom_hasher() { + assert_eq!( + HashesHasher::decl(), + "type HashesHasher = { map: { [key in string]?: string }, set: Array, };" + ) +} + +#[derive(TS, Eq, PartialEq, Hash)] +#[ts(export, export_to = "hashmap/")] +struct CustomKey(String); + +#[derive(TS)] +#[ts(export, export_to = "hashmap/")] +struct CustomValue; + +#[derive(TS)] +#[ts(export, export_to = "hashmap/")] +struct HashMapWithCustomTypes { + map: HashMap, +} + +#[derive(TS)] +#[ts(export, export_to = "hashmap/")] +struct BTreeMapWithCustomTypes { + map: BTreeMap, +} + +#[test] +fn with_custom_types() { + assert_eq!( + HashMapWithCustomTypes::inline(), + BTreeMapWithCustomTypes::inline() + ); + assert_eq!( + HashMapWithCustomTypes::decl(), + "type HashMapWithCustomTypes = { map: { [key in CustomKey]?: CustomValue }, };" + ); +} diff --git a/ts-rs/tests/integration/indexmap.rs b/ts-rs/tests/integration/indexmap.rs index 9af682a..cb5f88e 100644 --- a/ts-rs/tests/integration/indexmap.rs +++ b/ts-rs/tests/integration/indexmap.rs @@ -1,20 +1,20 @@ -#![allow(dead_code)] -#![cfg(feature = "indexmap-impl")] - -use indexmap::{IndexMap, IndexSet}; -use ts_rs::TS; - -#[derive(TS)] -#[ts(export, export_to = "indexmap/")] -struct Indexes { - map: IndexMap, - set: IndexSet, -} - -#[test] -fn indexmap() { - assert_eq!( - Indexes::decl(), - "type Indexes = { map: { [key: string]: string }, set: Array, };" - ) -} +#![allow(dead_code)] +#![cfg(feature = "indexmap-impl")] + +use indexmap::{IndexMap, IndexSet}; +use ts_rs::TS; + +#[derive(TS)] +#[ts(export, export_to = "indexmap/")] +struct Indexes { + map: IndexMap, + set: IndexSet, +} + +#[test] +fn indexmap() { + assert_eq!( + Indexes::decl(), + "type Indexes = { map: { [key in string]?: string }, set: Array, };" + ) +} diff --git a/ts-rs/tests/integration/issue_168.rs b/ts-rs/tests/integration/issue_168.rs index a82a580..732ba9b 100644 --- a/ts-rs/tests/integration/issue_168.rs +++ b/ts-rs/tests/integration/issue_168.rs @@ -38,13 +38,13 @@ fn issue_168() { FooInlined::export_to_string().unwrap(), "// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.\n\ \n\ - export type FooInlined = { map: { [key: number]: { map: { [key: number]: { map: { [key: number]: string }, } }, } }, };\n" + export type FooInlined = { map: { [key in number]?: { map: { [key in number]?: { map: { [key in number]?: string }, } }, } }, };\n" ); assert_eq!( Foo::export_to_string().unwrap(), "// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.\n\ import type { Bar } from \"./Bar\";\n\ \n\ - export type Foo = { map: { [key: number]: Bar }, };\n" + export type Foo = { map: { [key in number]?: Bar }, };\n" ); } diff --git a/ts-rs/tests/integration/issue_338.rs b/ts-rs/tests/integration/issue_338.rs new file mode 100644 index 0000000..c50c94c --- /dev/null +++ b/ts-rs/tests/integration/issue_338.rs @@ -0,0 +1,37 @@ +use std::collections::HashMap; +use ts_rs::TS; + +#[derive(TS)] +#[ts(export, export_to = "issue_338/")] +pub struct MyType { + pub my_field_0: bool, + pub my_field_1: HashMap, +} + +#[derive(TS)] +#[ts(export, export_to = "issue_338/")] +pub enum MyEnum { + Variant0, + Variant1, + Variant2, + Variant3, +} + +#[derive(TS)] +#[ts(export, export_to = "issue_338/")] +pub struct MyStruct { + pub my_field_0: bool, + pub my_field_1: u32, + pub my_field_2: Option, + pub my_field_3: Option, + pub my_field_4: Option, + pub my_field_5: String, +} + +#[test] +fn test() { + assert_eq!( + MyType::inline(), + "{ my_field_0: boolean, my_field_1: { [key in MyEnum]?: MyStruct }, }" + ); +} diff --git a/ts-rs/tests/integration/issue_70.rs b/ts-rs/tests/integration/issue_70.rs index 4db0ee8..4078b95 100644 --- a/ts-rs/tests/integration/issue_70.rs +++ b/ts-rs/tests/integration/issue_70.rs @@ -1,77 +1,77 @@ -#![allow(unused)] - -use std::collections::HashMap; - -use ts_rs::TS; - -type TypeAlias = HashMap; - -#[derive(TS)] -#[ts(export, export_to = "issue_70/")] -enum Enum { - A(TypeAlias), - B(HashMap), -} - -#[derive(TS)] -#[ts(export, export_to = "issue_70/")] -struct Struct { - a: TypeAlias, - b: HashMap, -} - -#[test] -fn issue_70() { - assert_eq!( - Enum::decl(), - "type Enum = { \"A\": { [key: string]: string } } | { \"B\": { [key: string]: string } };" - ); - assert_eq!( - Struct::decl(), - "type Struct = { a: { [key: string]: string }, b: { [key: string]: string }, };" - ); -} - -#[derive(TS)] -#[ts(export, export_to = "issue_70/")] -struct GenericType { - foo: T, - bar: U, -} - -type GenericAlias = GenericType<(A, String), Vec<(B, i32)>>; - -#[derive(TS)] -#[ts(export, export_to = "issue_70/")] -struct Container { - a: GenericAlias, Vec>, - b: GenericAlias, -} - -#[derive(TS)] -#[ts(export, export_to = "issue_70/")] -struct GenericContainer { - a: GenericAlias, - b: GenericAlias, - c: GenericAlias>, -} - -#[test] -fn generic() { - assert_eq!( - Container::decl(), - "type Container = { \ - a: GenericType<[Array, string], Array<[Array, number]>>, \ - b: GenericType<[string, string], Array<[string, number]>>, \ - };" - ); - - assert_eq!( - GenericContainer::<(), ()>::decl(), - "type GenericContainer = { \ - a: GenericType<[string, string], Array<[string, number]>>, \ - b: GenericType<[A, string], Array<[B, number]>>, \ - c: GenericType<[A, string], Array<[GenericType<[A, string], Array<[B, number]>>, number]>>, \ - };" - ); -} +#![allow(unused)] + +use std::collections::HashMap; + +use ts_rs::TS; + +type TypeAlias = HashMap; + +#[derive(TS)] +#[ts(export, export_to = "issue_70/")] +enum Enum { + A(TypeAlias), + B(HashMap), +} + +#[derive(TS)] +#[ts(export, export_to = "issue_70/")] +struct Struct { + a: TypeAlias, + b: HashMap, +} + +#[test] +fn issue_70() { + assert_eq!( + Enum::decl(), + "type Enum = { \"A\": { [key in string]?: string } } | { \"B\": { [key in string]?: string } };" + ); + assert_eq!( + Struct::decl(), + "type Struct = { a: { [key in string]?: string }, b: { [key in string]?: string }, };" + ); +} + +#[derive(TS)] +#[ts(export, export_to = "issue_70/")] +struct GenericType { + foo: T, + bar: U, +} + +type GenericAlias = GenericType<(A, String), Vec<(B, i32)>>; + +#[derive(TS)] +#[ts(export, export_to = "issue_70/")] +struct Container { + a: GenericAlias, Vec>, + b: GenericAlias, +} + +#[derive(TS)] +#[ts(export, export_to = "issue_70/")] +struct GenericContainer { + a: GenericAlias, + b: GenericAlias, + c: GenericAlias>, +} + +#[test] +fn generic() { + assert_eq!( + Container::decl(), + "type Container = { \ + a: GenericType<[Array, string], Array<[Array, number]>>, \ + b: GenericType<[string, string], Array<[string, number]>>, \ + };" + ); + + assert_eq!( + GenericContainer::<(), ()>::decl(), + "type GenericContainer = { \ + a: GenericType<[string, string], Array<[string, number]>>, \ + b: GenericType<[A, string], Array<[B, number]>>, \ + c: GenericType<[A, string], Array<[GenericType<[A, string], Array<[B, number]>>, number]>>, \ + };" + ); +} diff --git a/ts-rs/tests/integration/lifetimes.rs b/ts-rs/tests/integration/lifetimes.rs index 93f0cee..41ec73a 100644 --- a/ts-rs/tests/integration/lifetimes.rs +++ b/ts-rs/tests/integration/lifetimes.rs @@ -1,36 +1,36 @@ -#![allow(dead_code)] - -use ts_rs::TS; - -#[derive(TS)] -#[ts(export, export_to = "lifetimes/")] -struct S<'a> { - s: &'a str, -} - -#[derive(TS)] -#[ts(export, export_to = "lifetimes/")] -struct B<'a, T: 'a> { - a: &'a T, -} - -#[derive(TS)] -#[ts(export, export_to = "lifetimes/")] -struct A<'a> { - a: &'a &'a &'a Vec, //Multiple References - b: &'a Vec>, //Nesting - c: &'a std::collections::HashMap, //Multiple type args -} - -#[test] -fn contains_borrow() { - assert_eq!(S::decl(), "type S = { s: string, };") -} - -#[test] -fn contains_borrow_type_args() { - assert_eq!( - A::decl(), - "type A = { a: Array, b: Array>, c: { [key: string]: boolean }, };" - ); -} +#![allow(dead_code)] + +use ts_rs::TS; + +#[derive(TS)] +#[ts(export, export_to = "lifetimes/")] +struct S<'a> { + s: &'a str, +} + +#[derive(TS)] +#[ts(export, export_to = "lifetimes/")] +struct B<'a, T: 'a> { + a: &'a T, +} + +#[derive(TS)] +#[ts(export, export_to = "lifetimes/")] +struct A<'a> { + a: &'a &'a &'a Vec, //Multiple References + b: &'a Vec>, //Nesting + c: &'a std::collections::HashMap, //Multiple type args +} + +#[test] +fn contains_borrow() { + assert_eq!(S::decl(), "type S = { s: string, };") +} + +#[test] +fn contains_borrow_type_args() { + assert_eq!( + A::decl(), + "type A = { a: Array, b: Array>, c: { [key in string]?: boolean }, };" + ); +} diff --git a/ts-rs/tests/integration/main.rs b/ts-rs/tests/integration/main.rs index 59ba728..b905527 100644 --- a/ts-rs/tests/integration/main.rs +++ b/ts-rs/tests/integration/main.rs @@ -25,6 +25,7 @@ mod issue_168; mod issue_232; mod issue_308; mod issue_317; +mod issue_338; mod issue_70; mod issue_80; mod leading_colon; diff --git a/ts-rs/tests/integration/self_referential.rs b/ts-rs/tests/integration/self_referential.rs index 067f007..0df808b 100644 --- a/ts-rs/tests/integration/self_referential.rs +++ b/ts-rs/tests/integration/self_referential.rs @@ -1,188 +1,188 @@ -#![allow(dead_code)] -use std::{collections::HashMap, sync::Arc}; - -#[cfg(feature = "serde-compat")] -use serde::Serialize; -use ts_rs::TS; - -#[derive(TS)] -#[ts(export, export_to = "self_referential/")] -struct HasT { - t: &'static T<'static>, -} - -#[derive(TS)] -#[ts(export, export_to = "self_referential/")] -struct T<'a> { - t_box: Box>, - self_box: Box, - - t_ref: &'a T<'a>, - self_ref: &'a Self, - - t_arc: Arc>, - self_arc: Arc, - - #[ts(inline)] - has_t: HasT, -} - -#[test] -fn named() { - assert_eq!( - T::decl(), - "type T = { \ - t_box: T, \ - self_box: T, \ - t_ref: T, \ - self_ref: T, \ - t_arc: T, \ - self_arc: T, \ - has_t: { t: T, }, \ - };" - ); -} - -#[derive(TS)] -#[ts(export, export_to = "self_referential/", rename = "E")] -enum ExternallyTagged { - A(Box), - B(&'static ExternallyTagged), - C(Box), - D(&'static Self), - E( - Box, - Box, - &'static ExternallyTagged, - &'static Self, - ), - F { - a: Box, - b: &'static ExternallyTagged, - c: HashMap, - d: Option>, - #[ts(optional = nullable)] - e: Option>, - #[ts(optional)] - f: Option>, - }, - - G( - Vec, - [&'static ExternallyTagged; 1024], - HashMap, - ), -} - -#[test] -fn enum_externally_tagged() { - assert_eq!( - ExternallyTagged::decl(), - "type E = { \"A\": E } | \ - { \"B\": E } | \ - { \"C\": E } | \ - { \"D\": E } | \ - { \"E\": [E, E, E, E] } | \ - { \"F\": { a: E, b: E, c: { [key: string]: E }, d: E | null, e?: E | null, f?: E, } } | \ - { \"G\": [Array, Array, { [key: string]: E }] };" - ); -} - -#[derive(TS)] -#[cfg_attr(feature = "serde-compat", derive(Serialize))] -#[ts(rename = "I")] -#[cfg_attr(feature = "serde-compat", serde(tag = "tag"))] -#[cfg_attr(not(feature = "serde-compat"), ts(tag = "tag"))] -enum InternallyTagged { - A(Box), - B(&'static InternallyTagged), - C(Box), - D(&'static Self), - E(Vec), - F { - a: Box, - b: &'static InternallyTagged, - c: HashMap, - d: Option<&'static InternallyTagged>, - #[ts(optional = nullable)] - e: Option<&'static InternallyTagged>, - #[ts(optional)] - f: Option<&'static InternallyTagged>, - }, -} - -// NOTE: The generated type is actually not valid TS here, since the indirections rust enforces for recursive types -// gets lost during the translation to TypeScript (e.g "Box" => "T"). -#[test] -fn enum_internally_tagged() { - assert_eq!( - InternallyTagged::decl(), - "type I = { \"tag\": \"A\" } & I | \ - { \"tag\": \"B\" } & I | \ - { \"tag\": \"C\" } & I | \ - { \"tag\": \"D\" } & I | \ - { \"tag\": \"E\" } & Array | \ - { \"tag\": \"F\", a: I, b: I, c: { [key: I]: I }, d: I | null, e?: I | null, f?: I, };" - ); -} - -#[derive(TS)] -#[cfg_attr(feature = "serde-compat", derive(Serialize))] -#[ts(export, export_to = "self_referential/", rename = "A")] -#[cfg_attr(feature = "serde-compat", serde(tag = "tag", content = "content"))] -#[cfg_attr(not(feature = "serde-compat"), ts(tag = "tag", content = "content"))] -enum AdjacentlyTagged { - A(Box), - B(&'static AdjacentlyTagged), - C(Box), - D(&'static Self), - E(Vec), - F { - a: Box, - b: &'static AdjacentlyTagged, - c: HashMap, - d: Option<&'static AdjacentlyTagged>, - #[ts(optional = nullable)] - e: Option<&'static AdjacentlyTagged>, - #[ts(optional)] - f: Option<&'static AdjacentlyTagged>, - }, - G( - Vec, - [&'static AdjacentlyTagged; 4], - HashMap, - ), -} - -// NOTE: The generated type is actually not valid TS here, since the indirections rust enforces for recursive types -// gets lost during the translation to TypeScript (e.g "Box" => "T"). -#[test] -fn enum_adjacently_tagged() { - assert_eq!( - AdjacentlyTagged::decl(), - "type A = { \"tag\": \"A\", \"content\": A } | \ - { \"tag\": \"B\", \"content\": A } | \ - { \"tag\": \"C\", \"content\": A } | \ - { \"tag\": \"D\", \"content\": A } | \ - { \"tag\": \"E\", \"content\": Array } | \ - { \ - \"tag\": \"F\", \ - \"content\": { \ - a: A, \ - b: A, \ - c: { [key: string]: A }, \ - d: A | null, \ - e?: A | null, \ - f?: A, \ - } \ - } | \ - { \ - \"tag\": \"G\", \ - \"content\": [\ - Array, \ - [A, A, A, A], \ - { [key: string]: A }\ - ] \ - };" - ); -} +#![allow(dead_code)] +use std::{collections::HashMap, sync::Arc}; + +#[cfg(feature = "serde-compat")] +use serde::Serialize; +use ts_rs::TS; + +#[derive(TS)] +#[ts(export, export_to = "self_referential/")] +struct HasT { + t: &'static T<'static>, +} + +#[derive(TS)] +#[ts(export, export_to = "self_referential/")] +struct T<'a> { + t_box: Box>, + self_box: Box, + + t_ref: &'a T<'a>, + self_ref: &'a Self, + + t_arc: Arc>, + self_arc: Arc, + + #[ts(inline)] + has_t: HasT, +} + +#[test] +fn named() { + assert_eq!( + T::decl(), + "type T = { \ + t_box: T, \ + self_box: T, \ + t_ref: T, \ + self_ref: T, \ + t_arc: T, \ + self_arc: T, \ + has_t: { t: T, }, \ + };" + ); +} + +#[derive(TS)] +#[ts(export, export_to = "self_referential/", rename = "E")] +enum ExternallyTagged { + A(Box), + B(&'static ExternallyTagged), + C(Box), + D(&'static Self), + E( + Box, + Box, + &'static ExternallyTagged, + &'static Self, + ), + F { + a: Box, + b: &'static ExternallyTagged, + c: HashMap, + d: Option>, + #[ts(optional = nullable)] + e: Option>, + #[ts(optional)] + f: Option>, + }, + + G( + Vec, + [&'static ExternallyTagged; 1024], + HashMap, + ), +} + +#[test] +fn enum_externally_tagged() { + assert_eq!( + ExternallyTagged::decl(), + "type E = { \"A\": E } | \ + { \"B\": E } | \ + { \"C\": E } | \ + { \"D\": E } | \ + { \"E\": [E, E, E, E] } | \ + { \"F\": { a: E, b: E, c: { [key in string]?: E }, d: E | null, e?: E | null, f?: E, } } | \ + { \"G\": [Array, Array, { [key in string]?: E }] };" + ); +} + +#[derive(TS)] +#[cfg_attr(feature = "serde-compat", derive(Serialize))] +#[ts(rename = "I")] +#[cfg_attr(feature = "serde-compat", serde(tag = "tag"))] +#[cfg_attr(not(feature = "serde-compat"), ts(tag = "tag"))] +enum InternallyTagged { + A(Box), + B(&'static InternallyTagged), + C(Box), + D(&'static Self), + E(Vec), + F { + a: Box, + b: &'static InternallyTagged, + c: HashMap, + d: Option<&'static InternallyTagged>, + #[ts(optional = nullable)] + e: Option<&'static InternallyTagged>, + #[ts(optional)] + f: Option<&'static InternallyTagged>, + }, +} + +// NOTE: The generated type is actually not valid TS here, since the indirections rust enforces for recursive types +// gets lost during the translation to TypeScript (e.g "Box" => "T"). +#[test] +fn enum_internally_tagged() { + assert_eq!( + InternallyTagged::decl(), + "type I = { \"tag\": \"A\" } & I | \ + { \"tag\": \"B\" } & I | \ + { \"tag\": \"C\" } & I | \ + { \"tag\": \"D\" } & I | \ + { \"tag\": \"E\" } & Array | \ + { \"tag\": \"F\", a: I, b: I, c: { [key in I]?: I }, d: I | null, e?: I | null, f?: I, };" + ); +} + +#[derive(TS)] +#[cfg_attr(feature = "serde-compat", derive(Serialize))] +#[ts(export, export_to = "self_referential/", rename = "A")] +#[cfg_attr(feature = "serde-compat", serde(tag = "tag", content = "content"))] +#[cfg_attr(not(feature = "serde-compat"), ts(tag = "tag", content = "content"))] +enum AdjacentlyTagged { + A(Box), + B(&'static AdjacentlyTagged), + C(Box), + D(&'static Self), + E(Vec), + F { + a: Box, + b: &'static AdjacentlyTagged, + c: HashMap, + d: Option<&'static AdjacentlyTagged>, + #[ts(optional = nullable)] + e: Option<&'static AdjacentlyTagged>, + #[ts(optional)] + f: Option<&'static AdjacentlyTagged>, + }, + G( + Vec, + [&'static AdjacentlyTagged; 4], + HashMap, + ), +} + +// NOTE: The generated type is actually not valid TS here, since the indirections rust enforces for recursive types +// gets lost during the translation to TypeScript (e.g "Box" => "T"). +#[test] +fn enum_adjacently_tagged() { + assert_eq!( + AdjacentlyTagged::decl(), + "type A = { \"tag\": \"A\", \"content\": A } | \ + { \"tag\": \"B\", \"content\": A } | \ + { \"tag\": \"C\", \"content\": A } | \ + { \"tag\": \"D\", \"content\": A } | \ + { \"tag\": \"E\", \"content\": Array } | \ + { \ + \"tag\": \"F\", \ + \"content\": { \ + a: A, \ + b: A, \ + c: { [key in string]?: A }, \ + d: A | null, \ + e?: A | null, \ + f?: A, \ + } \ + } | \ + { \ + \"tag\": \"G\", \ + \"content\": [\ + Array, \ + [A, A, A, A], \ + { [key in string]?: A }\ + ] \ + };" + ); +} diff --git a/ts-rs/tests/integration/serde_json.rs b/ts-rs/tests/integration/serde_json.rs index 29e32c7..fc0a113 100644 --- a/ts-rs/tests/integration/serde_json.rs +++ b/ts-rs/tests/integration/serde_json.rs @@ -1,65 +1,65 @@ -#![cfg(feature = "serde_json")] -#![allow(unused)] - -use ts_rs::TS; - -#[derive(TS)] -#[ts(export, export_to = "serde_json_impl/")] -struct UsingSerdeJson { - num: serde_json::Number, - map1: serde_json::Map, - map2: serde_json::Map, - map3: serde_json::Map>, - map4: serde_json::Map, - map5: serde_json::Map, - any: serde_json::Value, -} - -#[test] -fn using_serde_json() { - assert_eq!(serde_json::Number::inline(), "number"); - assert_eq!( - serde_json::Map::::inline(), - "{ [key: string]: number }" - ); - assert_eq!( - serde_json::Value::decl(), - "type JsonValue = number | string | Array | { [key: string]: JsonValue };", - ); - - assert_eq!( - UsingSerdeJson::decl(), - "type UsingSerdeJson = { \ - num: number, \ - map1: { [key: string]: number }, \ - map2: { [key: string]: UsingSerdeJson }, \ - map3: { [key: string]: { [key: string]: number } }, \ - map4: { [key: string]: number }, \ - map5: { [key: string]: JsonValue }, \ - any: JsonValue, \ - };" - ) -} - -#[derive(TS)] -#[ts(export, export_to = "serde_json_impl/")] -struct InlinedValue { - #[ts(inline)] - any: serde_json::Value, -} - -#[test] -fn inlined_value() { - assert_eq!( - InlinedValue::decl(), - "type InlinedValue = { \ - any: number | string | Array | { [key: string]: JsonValue }, \ - };" - ); -} - -#[derive(TS)] -#[ts(export, export_to = "serde_json_impl/")] -struct Simple { - json: serde_json::Value, -} +#![cfg(feature = "serde_json")] +#![allow(unused)] + +use ts_rs::TS; + +#[derive(TS)] +#[ts(export, export_to = "serde_json_impl/")] +struct UsingSerdeJson { + num: serde_json::Number, + map1: serde_json::Map, + map2: serde_json::Map, + map3: serde_json::Map>, + map4: serde_json::Map, + map5: serde_json::Map, + any: serde_json::Value, +} + +#[test] +fn using_serde_json() { + assert_eq!(serde_json::Number::inline(), "number"); + assert_eq!( + serde_json::Map::::inline(), + "{ [key in string]?: number }" + ); + assert_eq!( + serde_json::Value::decl(), + "type JsonValue = number | string | Array | { [key in string]?: JsonValue };", + ); + + assert_eq!( + UsingSerdeJson::decl(), + "type UsingSerdeJson = { \ + num: number, \ + map1: { [key in string]?: number }, \ + map2: { [key in string]?: UsingSerdeJson }, \ + map3: { [key in string]?: { [key in string]?: number } }, \ + map4: { [key in string]?: number }, \ + map5: { [key in string]?: JsonValue }, \ + any: JsonValue, \ + };" + ) +} + +#[derive(TS)] +#[ts(export, export_to = "serde_json_impl/")] +struct InlinedValue { + #[ts(inline)] + any: serde_json::Value, +} + +#[test] +fn inlined_value() { + assert_eq!( + InlinedValue::decl(), + "type InlinedValue = { \ + any: number | string | Array | { [key in string]?: JsonValue }, \ + };" + ); +} + +#[derive(TS)] +#[ts(export, export_to = "serde_json_impl/")] +struct Simple { + json: serde_json::Value, +}