Skip to content
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
40 changes: 9 additions & 31 deletions editor/src/generate_ts_types.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,12 @@
/// 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]
/// Running this test will generate a `bindings.ts` file containing every type annotated with `specta::Type`.
#[test]
fn generate_ts_types() {
// 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}"));

// let mut file = File::create("../types.ts").unwrap();

// write!(file, "{export}").ok();
use crate::messages::prelude::FrontendMessage;
use specta::TypeCollection;
use specta_typescript::{BigIntExportBehavior, Typescript};

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(() =>
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)]
#[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