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

v2.x.x: Clarify how to export all the collected types into a typescript file with a given name #292

Open
cognivore opened this issue Nov 21, 2024 · 7 comments

Comments

@cognivore
Copy link

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 like TypeMap seem to be private, although I didn't validate it closely.

How?

  • For the latest published version, in the README documentation, point to the code that exports all collected typescript types into a file.

Sorry for a potential duplicate.

@cognivore cognivore changed the title Clarify how to export all the collected types into a typescript file with a given name v2.x.x: Clarify how to export all the collected types into a typescript file with a given name Nov 22, 2024
@cognivore
Copy link
Author

Got back to this before sleep, figured that export in serde_utils is behind export feature flag. My strategy when I get to the computer is —

  1. Enable "export" feature flag in Cargo.toml

specta_util::export().export_to(Typescript, my_path);

@cognivore
Copy link
Author

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.

@cognivore
Copy link
Author

Two Versions

Something cryptic is going on. My current hypothesis is that I have entered some dependency hell, which I shall try to remedy by trying to pin everything to github main branch.

@cognivore
Copy link
Author

Solution: use github version like so:

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 }

@oscartbeaumont
Copy link
Member

Thanks for pointing out the following. Going to reopen this issue until they are fixed:

  • README example is outdated.
  • This should be added to TypeCollection::export_to.
 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!

@cognivore
Copy link
Author

cognivore commented Nov 22, 2024

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.

@cognivore
Copy link
Author

I didn't forget about this, I had a very busy weekend, however. I'm still on it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants