Skip to content

Commit

Permalink
Implement inline generics
Browse files Browse the repository at this point in the history
  • Loading branch information
escritorio-gustavo committed Jan 26, 2024
1 parent a3ac452 commit 64544e0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
17 changes: 14 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,20 @@ 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() {
"null" => #generic_ident_str.to_owned(),
x => x.to_owned()
}
);
}

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

#[test]
#[ignore]
// #[ignore]
// https://github.com/Aleph-Alpha/ts-rs/issues/56 TODO
fn inline() {
#[derive(TS)]
Expand All @@ -167,7 +167,7 @@ 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, }"
);
}

Expand Down

0 comments on commit 64544e0

Please sign in to comment.