commit 3c92cc8fb4b33d1aac8d4487e95b8238adcbeb4f
parent fd602003e3bf97b53ce41355cecb358c514abf76
Author: triesap <tyson@radroots.org>
Date: Wed, 12 Nov 2025 20:43:50 +0000
workspace: add wasm sql guards and optional `ts-rs` schema exports
Diffstat:
11 files changed, 120 insertions(+), 89 deletions(-)
diff --git a/Cargo.toml b/Cargo.toml
@@ -1,20 +1,20 @@
[workspace]
members = [
- "core",
- "events",
- "events-codec",
- "events-indexed",
- "identity",
- "log",
- "net",
- "net-core",
- "nostr",
- "runtime",
- "sql-wasm-bridge",
- "sql-wasm-core",
- "tangle-schema",
- "trade",
- "types",
+ "core",
+ "events",
+ "events-codec",
+ "events-indexed",
+ "identity",
+ "log",
+ "net",
+ "net-core",
+ "nostr",
+ "runtime",
+ "sql-wasm-bridge",
+ "sql-wasm-core",
+ "tangle-schema",
+ "trade",
+ "types",
]
resolver = "2"
@@ -77,3 +77,4 @@ uuid = { version = "1.16.0" }
uniffi = { version = "0.29.4" }
wasm-bindgen = { version = "0.2" }
wasm-bindgen-futures = { version = "0.4" }
+rusqlite = { version = "0.31", default-features = false }
diff --git a/Makefile b/Makefile
@@ -1,18 +1,23 @@
-.PHONY: all bindings clean help
-
+.PHONY: all bindings bindings-tangle-schema bindings-types clean help
SHELL := /bin/bash
.SHELLFLAGS := -e -o pipefail -c
+TS_RS_FEATURE ?= ts-rs
all: bindings
-bindings:
- cd tangle-schema && cargo test && cd bindings/ts && yarn build
- @echo "Building tangle-sql-core bindings"
- cd tangle-sql-core && cargo test && cd bindings/ts && yarn build
- @echo "Building types bindings"
- cd types && cargo test && cd bindings/ts && yarn build
+bindings: bindings-tangle-schema bindings-types
@echo "All bindings built successfully."
+bindings-tangle-schema:
+ @echo "Building tangle-schema bindings"
+ @(cd tangle-schema && cargo test --features $(TS_RS_FEATURE))
+ @(cd tangle-schema/bindings/ts && yarn build)
+
+bindings-types:
+ @echo "Building types bindings"
+ @(cd types && cargo test --features $(TS_RS_FEATURE))
+ @(cd types/bindings/ts && yarn build)
+
clean:
cargo clean
diff --git a/sql-wasm-bridge/src/lib.rs b/sql-wasm-bridge/src/lib.rs
@@ -1,3 +1,5 @@
+#![cfg(target_arch = "wasm32")]
+
use wasm_bindgen::JsValue;
use wasm_bindgen::prelude::*;
diff --git a/sql-wasm-core/src/error.rs b/sql-wasm-core/src/error.rs
@@ -1,5 +1,4 @@
use thiserror::Error;
-use wasm_bindgen::JsValue;
#[derive(Error, Debug, Clone)]
pub enum SqlWasmError {
@@ -25,12 +24,15 @@ impl SqlWasmError {
SqlWasmError::Internal => "ERR_INTERNAL",
}
}
+}
- pub fn to_js_value(self) -> JsValue {
+#[cfg(target_arch = "wasm32")]
+impl SqlWasmError {
+ pub fn to_js_value(self) -> wasm_bindgen::JsValue {
let o = serde_json::json!({
"code": self.code(),
"message": self.to_string()
});
- JsValue::from_str(&o.to_string())
+ wasm_bindgen::JsValue::from_str(&o.to_string())
}
}
diff --git a/sql-wasm-core/src/lib.rs b/sql-wasm-core/src/lib.rs
@@ -1,28 +1,34 @@
+#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;
pub mod error;
pub mod utils;
+#[cfg(target_arch = "wasm32")]
#[wasm_bindgen(js_name = exec_sql)]
pub fn exec_sql(sql: &str, params_json: &str) -> JsValue {
radroots_sql_wasm_bridge::exec(sql, params_json)
}
+#[cfg(target_arch = "wasm32")]
#[wasm_bindgen(js_name = query_sql)]
pub fn query_sql(sql: &str, params_json: &str) -> JsValue {
radroots_sql_wasm_bridge::query(sql, params_json)
}
+#[cfg(target_arch = "wasm32")]
#[wasm_bindgen(js_name = begin_tx)]
pub fn begin_tx() {
radroots_sql_wasm_bridge::begin_tx()
}
+#[cfg(target_arch = "wasm32")]
#[wasm_bindgen(js_name = commit_tx)]
pub fn commit_tx() {
radroots_sql_wasm_bridge::commit_tx()
}
+#[cfg(target_arch = "wasm32")]
#[wasm_bindgen(js_name = rollback_tx)]
pub fn rollback_tx() {
radroots_sql_wasm_bridge::rollback_tx()
diff --git a/tangle-schema/Cargo.toml b/tangle-schema/Cargo.toml
@@ -12,8 +12,9 @@ crate-type = ["rlib"]
[features]
default = []
+ts-rs = ["dep:ts-rs"]
[dependencies]
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
-ts-rs = { workspace = true }
+ts-rs = { workspace = true, optional = true }
diff --git a/tangle-schema/build.rs b/tangle-schema/build.rs
@@ -1,11 +1,13 @@
-use std::{fs, path::Path};
+use std::{env, fs, path::Path};
fn main() {
- println!("cargo:rustc-env=TS_RS_EXPORT_DIR=./bindings/ts/src");
- let out_dir = Path::new("bindings/ts/src");
- if !out_dir.exists() {
- fs::create_dir_all(out_dir).expect("create TS export dir");
- }
- println!("cargo:rerun-if-changed=src");
println!("cargo:rerun-if-changed=build.rs");
+ if env::var_os("CARGO_FEATURE_TS_RS").is_some() {
+ println!("cargo:rustc-env=TS_RS_EXPORT_DIR=./bindings/ts/src");
+ let out_dir = Path::new("bindings/ts/src");
+ if !out_dir.exists() {
+ fs::create_dir_all(out_dir).expect("create TS export dir");
+ }
+ println!("cargo:rerun-if-changed=src");
+ }
}
diff --git a/tangle-schema/src/tables/log_error.rs b/tangle-schema/src/tables/log_error.rs
@@ -1,9 +1,10 @@
use serde::{Deserialize, Serialize};
-use serde_json::Value;
+#[cfg(feature = "ts-rs")]
use ts_rs::TS;
-#[derive(Serialize, Deserialize, TS)]
-#[ts(export, export_to = "types.ts")]
+#[cfg_attr(feature = "ts-rs", derive(TS))]
+#[cfg_attr(feature = "ts-rs", ts(export, export_to = "types.ts"))]
+#[derive(Serialize, Deserialize)]
pub struct LogError {
pub id: String,
pub created_at: String,
@@ -18,8 +19,9 @@ pub struct LogError {
pub data: Option<String>,
}
-#[derive(Clone, Deserialize, Serialize, TS)]
-#[ts(export, export_to = "types.ts")]
+#[cfg_attr(feature = "ts-rs", derive(TS))]
+#[cfg_attr(feature = "ts-rs", ts(export, export_to = "types.ts"))]
+#[derive(Clone, Deserialize, Serialize)]
pub struct ILogErrorFields {
pub error: String,
pub message: String,
@@ -31,57 +33,60 @@ pub struct ILogErrorFields {
pub data: Option<String>,
}
-#[derive(Clone, Deserialize, Serialize, TS)]
-#[ts(export, export_to = "types.ts")]
+#[cfg_attr(feature = "ts-rs", derive(TS))]
+#[cfg_attr(feature = "ts-rs", ts(export, export_to = "types.ts"))]
+#[derive(Clone, Deserialize, Serialize)]
pub struct ILogErrorFieldsPartial {
- #[ts(optional, type = "string | null")]
- pub error: Option<Value>,
- #[ts(optional, type = "string | null")]
- pub message: Option<Value>,
- #[ts(optional, type = "string | null")]
- pub stack_trace: Option<Value>,
- #[ts(optional, type = "string | null")]
- pub cause: Option<Value>,
- #[ts(optional, type = "string | null")]
- pub app_system: Option<Value>,
- #[ts(optional, type = "string | null")]
- pub app_version: Option<Value>,
- #[ts(optional, type = "string | null")]
- pub nostr_pubkey: Option<Value>,
- #[ts(optional, type = "string | null")]
- pub data: Option<Value>,
+ #[cfg_attr(feature = "ts-rs", ts(optional, type = "string | null"))]
+ pub error: Option<serde_json::Value>,
+ #[cfg_attr(feature = "ts-rs", ts(optional, type = "string | null"))]
+ pub message: Option<serde_json::Value>,
+ #[cfg_attr(feature = "ts-rs", ts(optional, type = "string | null"))]
+ pub stack_trace: Option<serde_json::Value>,
+ #[cfg_attr(feature = "ts-rs", ts(optional, type = "string | null"))]
+ pub cause: Option<serde_json::Value>,
+ #[cfg_attr(feature = "ts-rs", ts(optional, type = "string | null"))]
+ pub app_system: Option<serde_json::Value>,
+ #[cfg_attr(feature = "ts-rs", ts(optional, type = "string | null"))]
+ pub app_version: Option<serde_json::Value>,
+ #[cfg_attr(feature = "ts-rs", ts(optional, type = "string | null"))]
+ pub nostr_pubkey: Option<serde_json::Value>,
+ #[cfg_attr(feature = "ts-rs", ts(optional, type = "string | null"))]
+ pub data: Option<serde_json::Value>,
}
-#[derive(Clone, Deserialize, Serialize, TS)]
-#[ts(export, export_to = "types.ts")]
+#[cfg_attr(feature = "ts-rs", derive(TS))]
+#[cfg_attr(feature = "ts-rs", ts(export, export_to = "types.ts"))]
+#[derive(Clone, Deserialize, Serialize)]
pub struct ILogErrorFieldsFilter {
- #[ts(optional)]
+ #[cfg_attr(feature = "ts-rs", ts(optional))]
pub id: Option<String>,
- #[ts(optional)]
+ #[cfg_attr(feature = "ts-rs", ts(optional))]
pub created_at: Option<String>,
- #[ts(optional)]
+ #[cfg_attr(feature = "ts-rs", ts(optional))]
pub updated_at: Option<String>,
- #[ts(optional)]
+ #[cfg_attr(feature = "ts-rs", ts(optional))]
pub error: Option<String>,
- #[ts(optional)]
+ #[cfg_attr(feature = "ts-rs", ts(optional))]
pub message: Option<String>,
- #[ts(optional)]
+ #[cfg_attr(feature = "ts-rs", ts(optional))]
pub stack_trace: Option<String>,
- #[ts(optional)]
+ #[cfg_attr(feature = "ts-rs", ts(optional))]
pub cause: Option<String>,
- #[ts(optional)]
+ #[cfg_attr(feature = "ts-rs", ts(optional))]
pub app_system: Option<String>,
- #[ts(optional)]
+ #[cfg_attr(feature = "ts-rs", ts(optional))]
pub app_version: Option<String>,
- #[ts(optional)]
+ #[cfg_attr(feature = "ts-rs", ts(optional))]
pub nostr_pubkey: Option<String>,
- #[ts(optional)]
+ #[cfg_attr(feature = "ts-rs", ts(optional))]
pub data: Option<String>,
}
-#[derive(Clone, Serialize, Deserialize, TS)]
+#[cfg_attr(feature = "ts-rs", derive(TS))]
+#[cfg_attr(feature = "ts-rs", ts(export, export_to = "types.ts"))]
+#[derive(Clone, Serialize, Deserialize)]
#[serde(untagged)]
-#[ts(export, export_to = "types.ts")]
pub enum LogErrorQueryBindValues {
Id { id: String },
NostrPubkey { nostr_pubkey: String },
diff --git a/types/Cargo.toml b/types/Cargo.toml
@@ -7,7 +7,11 @@ rust-version.workspace = true
license.workspace = true
build = "build.rs"
+[features]
+default = []
+ts-rs = ["dep:ts-rs"]
+
[dependencies]
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
-ts-rs = { workspace = true }
-\ No newline at end of file
+ts-rs = { workspace = true, optional = true }
diff --git a/types/build.rs b/types/build.rs
@@ -1,13 +1,13 @@
-use std::{fs, path::Path};
+use std::{env, fs, path::Path};
fn main() {
- println!("cargo:rustc-env=TS_RS_EXPORT_DIR=./bindings/ts/src");
-
- let out_dir = Path::new("bindings/ts/src");
- if !out_dir.exists() {
- fs::create_dir_all(out_dir).expect("Failed to create TS export directory");
- }
-
- println!("cargo:rerun-if-changed=src");
println!("cargo:rerun-if-changed=build.rs");
+ if env::var_os("CARGO_FEATURE_TS_RS").is_some() {
+ println!("cargo:rustc-env=TS_RS_EXPORT_DIR=./bindings/ts/src");
+ let out_dir = Path::new("bindings/ts/src");
+ if !out_dir.exists() {
+ fs::create_dir_all(out_dir).expect("Failed to create TS export directory");
+ }
+ println!("cargo:rerun-if-changed=src");
+ }
}
diff --git a/types/src/types.rs b/types/src/types.rs
@@ -1,20 +1,24 @@
use serde::Serialize;
+#[cfg(feature = "ts-rs")]
use ts_rs::TS;
-#[derive(Serialize, TS)]
-#[ts(export, export_to = "types.ts")]
+#[cfg_attr(feature = "ts-rs", derive(TS))]
+#[cfg_attr(feature = "ts-rs", ts(export, export_to = "types.ts"))]
+#[derive(Serialize)]
pub struct IResult<T> {
pub result: T,
}
-#[derive(Serialize, TS)]
-#[ts(export, export_to = "types.ts")]
+#[cfg_attr(feature = "ts-rs", derive(TS))]
+#[cfg_attr(feature = "ts-rs", ts(export, export_to = "types.ts"))]
+#[derive(Serialize)]
pub struct IResultList<T> {
pub results: Vec<T>,
}
-#[derive(Serialize, TS)]
-#[ts(export, export_to = "types.ts")]
+#[cfg_attr(feature = "ts-rs", derive(TS))]
+#[cfg_attr(feature = "ts-rs", ts(export, export_to = "types.ts"))]
+#[derive(Serialize)]
pub struct IResultPass {
pub pass: bool,
}