Aleph-Alpha / ts-rs

Generate TypeScript bindings from Rust types
MIT License
1.15k stars 115 forks source link

Option to rename source of `TS` trait #274

Closed aumetra closed 8 months ago

aumetra commented 8 months ago

Goal

The goal of this PR is to provide an option which allows for re-exports of the ts_rs::TS trait.
Similar to serde's #[serde(crate = "other_serde")] attribute.

Motivation is us evaluating ts-rs for generating TypeScript types directly without an intermediate JSON Schema step. We generate the derive annotations via an own procedural macro, which is why we need to re-export the crate.

Changes

This PR introduces the new crate_rename attribute to the derive macro.
The invocation looks similar to this:

#[derive(TS)]
#[ts(crate_rename = "::my_crate::ts_rs")]

Checklist

(Will add the documentation if this is actually a feature that would be accepted)

aumetra commented 8 months ago

This PR is missing like 90% of all renames. So far from complete.
Will complete and clean it up once it is clear whether this would be accepted.

escritorio-gustavo commented 8 months ago

Hey, thanks for the PR! I've left a couple of changes I'd like to see here, apart from those, I don't see an issue with adding this feature.

aumetra commented 8 months ago

I've left a couple of changes I'd like to see here, apart from those, I don't see an issue with adding this feature

Great! Then I will check on how to sync the same crate name across all the different code paths and will take care of your requested changes.

escritorio-gustavo commented 8 months ago

Great! Then I will check on how to sync the same crate name across all the different code paths

A few days ago I added a commit to add :: before the ts_rs path. Check out the changes in #267 to see all the places that will need updating

NyxCode commented 8 months ago

I'm open to adding this as well. 👍

NyxCode commented 8 months ago

Interestingly, this would come in handy in #276, allowing to #[derive(TS)] within ts-rs by using #[ts(crate = "")]

aumetra commented 8 months ago

Oh, you got there before I did. Cool!
For a test, do you think introducing a separate test crate, such as ts-test-rename would be sensible and then importing it as a dependency via

ts-rename = { package = "ts-rs", path = "../ts-rs" }

That way you can be 100% sure the rename code works. The test I made is a bit.. lackluster

escritorio-gustavo commented 8 months ago

Oh, you got there before I did. Cool! For a test, do you think introducing a separate test crate, such as ts-test-rename would be sensible and then importing it as a dependency via

ts-rename = { package = "ts-rs", path = "../ts-rs" }

That way you can be 100% sure the rename code works. The test I made is a bit.. lackluster

This is a very good idea! You can do that inside the e2e directory. Checkout e2e/workspace and e2e/dependencies for examples of how to set this up

NyxCode commented 8 months ago

Great work guys, awesome!
I've taken the liberty to make crate_rename private and replace its use with a getter, crate_rename() -> Path. I didn't love that we had these unwrap()s everywhere.

NyxCode commented 8 months ago

Interestingly, this would come in handy in https://github.com/Aleph-Alpha/ts-rs/pull/276, allowing to #[derive(TS)] within ts-rs by using #[ts(crate = "")]

While this doesn't allow #[ts(crate = "")], #[ts(crate = "crate")] works perfectly fine.

escritorio-gustavo commented 8 months ago

Great work guys, awesome! I've taken the liberty to make crate_rename private and replace its use with a getter, crate_rename() -> Path. I didn't love that we had these unwrap()s everywhere.

Nice! I didn't like having so many unwraps either, glad to see them gone!