Skip to content

Commit

Permalink
Merge pull request #212 from escritorio-gustavo/inline_generics
Browse files Browse the repository at this point in the history
Implement inline generics
  • Loading branch information
escritorio-gustavo authored Jan 29, 2024
2 parents 5e9965b + 9bc2299 commit 1dc210b
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 6 deletions.
23 changes: 20 additions & 3 deletions macros/src/types/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn format_generics(deps: &mut Dependencies, generics: &Generics) -> TokenStr

pub fn format_type(ty: &Type, dependencies: &mut Dependencies, generics: &Generics) -> TokenStream {
// If the type matches one of the generic parameters, just pass the identifier:
if let Some(generic_ident) = generics
if let Some(generic) = generics
.params
.iter()
.filter_map(|param| match param {
Expand All @@ -55,9 +55,26 @@ pub fn format_type(ty: &Type, dependencies: &mut Dependencies, generics: &Generi
&& type_path.path.is_ident(&type_param.ident)
)
})
.map(|type_param| type_param.ident.to_string())
{
return quote!(#generic_ident.to_owned());
let generic_ident = generic.ident.clone();
let generic_ident_str = generic_ident.to_string();

if !generic.bounds.is_empty() || generic.default.is_some() {
return quote!(#generic_ident_str.to_owned());
}

return quote!(
match <#generic_ident>::name().as_str() {
// When exporting a generic, the default type used is `()`,
// which gives "null" when calling `.name()`. In this case, we
// want to preserve the type param's identifier as the name used
"null" => #generic_ident_str.to_owned(),

// If name is not "null", a type has been provided, so we use its
// name instead
x => x.to_owned()
}
);
}

// special treatment for arrays and tuples
Expand Down
54 changes: 51 additions & 3 deletions ts-rs/tests/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,6 @@ fn generic_struct() {
}

#[test]
#[ignore]
// https://github.com/Aleph-Alpha/ts-rs/issues/56 TODO
fn inline() {
#[derive(TS)]
struct Generic<T> {
Expand All @@ -167,7 +165,57 @@ fn inline() {
assert_eq!(Generic::<()>::decl(), "type Generic<T> = { t: T, }");
assert_eq!(
Container::decl(),
"type Container = { g: Generic<string>, gi: { t: string }, t: string, }"
"type Container = { g: Generic<string>, gi: { t: string, }, t: string, }"
);
}

#[test]
#[ignore = "We haven't figured out how to inline generics with bounds yet"]
fn inline_with_bounds() {
#[derive(TS)]
struct Generic<T: ToString> {
t: T,
}

#[derive(TS)]
struct Container {
g: Generic<String>,
#[ts(inline)]
gi: Generic<String>,
#[ts(flatten)]
t: Generic<u32>,
}

assert_eq!(Generic::<&'static str>::decl(), "type Generic<T> = { t: T, }");
assert_eq!(
Container::decl(),
"type Container = { g: Generic<string>, gi: { t: string, }, t: number, }"
// Actual output: { g: Generic<string>, gi: { t: T, }, t: T, }
);
}

#[test]
#[ignore = "We haven't figured out how to inline generics with defaults yet"]
fn inline_with_default() {
#[derive(TS)]
struct Generic<T = String> {
t: T,
}

#[derive(TS)]
struct Container {
g: Generic<String>,
#[ts(inline)]
gi: Generic<String>,
#[ts(flatten)]
t: Generic<u32>,
}

assert_eq!(Generic::<&'static str>::decl(), "type Generic<T = string> = { t: T, }");
assert_eq!(
Container::decl(),
"type Container = { g: Generic<string>, gi: { t: string, }, t: number, }"
// Actual output: { g: Generic<string>, gi: { t: T, }, t: T, }
);
}

Expand Down

0 comments on commit 1dc210b

Please sign in to comment.