Skip to content

Reenable Specta integration #2432

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
24 changes: 24 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ fern = { version = "0.7", features = ["colored"] }
num_enum = "0.7"
num-derive = "0.4"
num-traits = { version = "0.2", default-features = false, features = ["i128"] }
specta = { version = "2.0.0-rc.22", features = [
"glam",
specta = { version = "=2.0.0-rc.22", features = [
"derive",
# "typescript",
"glam",
] }
specta-typescript = { version = "=0.0.9" }
syn = { version = "2.0", default-features = false, features = [
"full",
"derive",
Expand Down
1 change: 1 addition & 0 deletions editor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ futures = { workspace = true }
glam = { workspace = true, features = ["serde", "debug-glam-assert"] }
derivative = { workspace = true }
specta = { workspace = true }
specta-typescript = { workspace = true }
image = { workspace = true, features = ["bmp", "png"] }
dyn-any = { workspace = true }
num_enum = { workspace = true }
Expand Down
35 changes: 8 additions & 27 deletions editor/src/generate_ts_types.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,15 @@
/// Running this test will generate a `types.ts` file at the root of the repo,
/// containing every type annotated with `specta::Type`
// #[cfg(all(test, feature = "specta-export"))]
#[ignore]

#[test]
fn generate_ts_types() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed this but it's probally not needed with the wasm exporting solution (hence why it's still #[ignore]ed)

// TODO: Un-comment this out when we figure out how to reenable the "typescript` Specta feature flag

// use crate::messages::prelude::FrontendMessage;
// use specta::ts::{export_named_datatype, BigIntExportBehavior, ExportConfig};
// use specta::{NamedType, TypeMap};
// use std::fs::File;
// use std::io::Write;

// let config = ExportConfig::new().bigint(BigIntExportBehavior::Number);

// let mut type_map = TypeMap::default();

// let datatype = FrontendMessage::definition_named_data_type(&mut type_map);

// let mut export = String::new();

// export += &export_named_datatype(&config, &datatype, &type_map).unwrap();

// type_map
// .iter()
// .map(|(_, v)| v)
// .flat_map(|v| export_named_datatype(&config, v, &type_map))
// .for_each(|e| export += &format!("\n\n{e}"));
use specta::TypeCollection;
use specta_typescript::{BigIntExportBehavior, Typescript};

// let mut file = File::create("../types.ts").unwrap();
use crate::messages::prelude::FrontendMessage;

// write!(file, "{export}").ok();
Typescript::default()
.bigint(BigIntExportBehavior::Number)
.export_to("../frontend/src/bindings.ts", TypeCollection::default().register::<FrontendMessage>())
.unwrap();
}
276 changes: 276 additions & 0 deletions frontend/bindings_from_node.ts

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions frontend/wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ web-sys = { workspace = true, features = [
"HtmlCanvasElement",
"IdleRequestOptions",
] }
specta = { workspace = true }
specta-typescript = { workspace = true }

# Optional workspace dependencies
ron = { workspace = true, optional = true }
Expand Down
7 changes: 7 additions & 0 deletions frontend/wasm/export.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import fs from "node:fs";
import path from "node:path";
import graphite, { get_specta_types } from "./pkg/graphite_wasm.js";

graphite(fs.readFileSync(path.join(import.meta.dirname, './pkg/graphite_wasm_bg.wasm'))).then(() =>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We import the wasm file and then call the get_specta_types function and save the result to the disk.

By doing this we don't need to build a separate x86/ARM binary specially for exporting the types. Commonly this would be done via cargo test. By having it be included in the binary makes it way quicker for Graphite's specific setup.

fs.writeFileSync("bindings_from_node.ts", get_specta_types())
);
12 changes: 12 additions & 0 deletions frontend/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ pub fn init_graphite() {
log::set_max_level(log::LevelFilter::Debug);
}

#[cfg(debug_assertions)]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We expose a wasm function that returns a string with all the Specta types.

This is only enabled in development so Rust can strip more for production builds.

#[wasm_bindgen]
pub fn get_specta_types() -> Result<String, String> {
use specta::TypeCollection;
use specta_typescript::{BigIntExportBehavior, Typescript};

Typescript::default()
.bigint(BigIntExportBehavior::Number)
.export(&TypeCollection::default().register::<FrontendMessage>())
.map_err(|err| err.to_string())
}

/// When a panic occurs, notify the user and log the error to the JS console before the backend dies
pub fn panic_hook(info: &panic::PanicHookInfo) {
let info = info.to_string();
Expand Down
Loading