-
-
Notifications
You must be signed in to change notification settings - Fork 42
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
v2.x.x: Clarify how to export all the collected types into a typescript file with a given name #292
Comments
Got back to this before sleep, figured that
specta_util::export().export_to(Typescript, my_path); |
This indeed has worked. Kind of. Correct code: if let Some(parent) = types_path.parent() {
std::fs::create_dir_all(parent)?;
}
dbg!(specta_util::export()).export_to(Typescript::default(), &types_path)?; However it doesn't quite work: λ rg Specta
src/timestamp.rs
3:use specta::Type as Specta;
6:#[derive(Debug, Clone, Copy, Serialize, Deserialize, Specta)]
src/state/task.rs
6:use specta::Type as Specta;
21:#[derive(Debug, Clone, Serialize, Deserialize, Specta)]
50:#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Specta)]
88:#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Specta)]
94:#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Specta)]
104:#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Specta)] As you see, we derive specta::Type for quite a lot of things. However, λ cat frontend/src/types/generated.ts
// This file has been generated by Specta. DO NOT EDIT. The file is empty sans the header. I shall continue debug. |
Solution: use specta = { git = "https://github.com/specta-rs/specta", features = ["chrono", "uuid", "url", "derive"] }
specta-typescript = { git = "https://github.com/specta-rs/specta" }
specta-util = { git = "https://github.com/specta-rs/specta", features = ["export"] } Then this code just works: if let Some(parent) = types_path.parent() {
std::fs::create_dir_all(parent)?;
}
specta_util::export().export_to(Typescript::default(), &types_path)?; λ cat frontend/src/types/generated.ts
// This file has been generated by Specta. DO NOT EDIT.
export type GameDevTech = "unity3d"
export type GeneralTech = "rust" | "scala" | "elixir" | "python" | "go"
export type Task = { name: string; technology: Technology; task_type: TaskType; description: string; last_updated: string }
export type TaskType = "Singleplayer" | "Multiplayer"
export type Technology = { gamedev: GameDevTech } | { general: GeneralTech } |
Thanks for pointing out the following. Going to reopen this issue until they are fixed:
if let Some(parent) = types_path.parent() {
std::fs::create_dir_all(parent)?;
} Will do myself or happy if you wanted to PR them. I suspect you had multiple versions of Specta in your project is thats the case. Great you got it fixed! |
Fwiw, I've installed latest versions of specta, specta-typescript and specta-utils from crates.io. Please don't bother with doing it yourself, I appreciate you spending the time to respond to my issues. I'll submit a PR by Monday. |
I didn't forget about this, I had a very busy weekend, however. I'm still on it. |
Why?
Reading examples, we see a pattern using
specta_util
, however in cargo version, a bunch of stuff there is marked as// TODO
and also relevant data structures likeTypeMap
seem to be private, although I didn't validate it closely.How?
Sorry for a potential duplicate.
The text was updated successfully, but these errors were encountered: