-
Notifications
You must be signed in to change notification settings - Fork 115
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
Avoiding the testcase workaround #62
Comments
Hey! match identifier.as_str() {
"i8" => "number".to_string(),
"u8" => "number".to_string(),
"i16" => "number".to_string(),
"u16" => "number".to_string(),
...
"NaiveDateTime" => "Date".to_string(),
"DateTime" => "Date".to_string(),
...
_ => identifier.to_string()
} While this probably works for simple usecases, it's really limiting since the tool has no idea what the types are - it just echoes their identifier to typescript.
More concretely: #[derive(TS)]
#[ts(rename = "SomeType")]
struct T {
a: i32,
#[ts(flatten)]
b: Something,
}
impl TS for T {
...
fn name() -> String {
"SomeType".to_owned()
}
fn inline() -> String {
format!("{ a: {}, {} }", <i32 as TS>::name(), <Something as TS>::inline_flattened())
}
...
} So, tldr: |
Good points, thanks for taking the time to explain. You aren't just parsing the tokens, you're re-inserting the tokens into the source code to get the compiler to do important work on them. So the external parser won't work. If we cannot pull type generation out of the code, I'll try to find a workaround with feature gates. The goal is that
I'll play around with it as time permits and check back with you. |
Awesome, thanks. Really apreciate it. |
Success.
Running Running
This works best if the feature "export_bindings" is not set by default, but that's a breaking change. Otherwise we'd have to tell users to manually disable the feature when specifying the ts-rs dep. But I'm wondering, how far should we go here? Right now, the macro derives are still generated outside of tests. We could disable that to avoid the compile time penalty. Are you aware of any users consuming the traits other than for exporting? Depending on the answer, we might turn the whole crate inert when |
You can also opt for a more manual approach and write lines of macro_rules! export {
($target:expr, $($types:ty),*) => {
{
let target = $target;
fn _export(target: &mut impl ::std::io::Write) -> ::std::result::Result<(), ::std::io::Error> {
$(
writeln!(target, "export {}\n", <$types as ::ts_rs::TS>::decl())?;
)*
Ok(())
}
_export(target)
}
};
}
fn some_function() {
let mut target = std::fs::File::create("my-bindings.ts").unwrap();
let _ = export! {
&mut target,
SomeType,
AnotherType,
/// ...
}
} |
@Heliozoa with |
This has been inactive for a very long time and there have not been any PRs aiming at implementing this. |
Hello,
as you're aware, hiding the code generation in testcases is a bit of a hack and probably annoying for anyone actually using their tests for other matters. Like testing.
If you haven't seen it, https://github.com/Wulf/tsync is taking a different approach: Instead of generating a meaningful
#[derive(TS)]
at build time, it will parse your source code independently of the compiler. But both tsync and ts-rs use thesyn
crate, and will parse asyn::Item::Struct
/::Enum
into type information.Their approach has a few advantages:
#[derive(..)]
(though that is probably minor)But let's not hide the disadvantages:
tsync
binary. This could be avoided by letting users add a one-liner[[bin]]
or[[example]]
to their project and calling that. (an example has the advantage that you can put its deps into dev-dependencies)I was wondering if it'd be useful to better abstract the type parsing away from the type consumption. We create a function taking a
syn::Item
and returning astruct DerivedTS
(or something more generic than it).Using that function, we could implement both the actual
#[derive(TS)]
and testcase generation, but we could also implement an external parser similar to tsync for projects where that's a better fit.Do you think that'd be useful? Am I overlooking something important? Is this something I should hack on the next slow weekend, or over the holidays?
The text was updated successfully, but these errors were encountered: