Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement inline generics #212

Merged
merged 3 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading