lib

Core libraries for Radroots
git clone https://radroots.dev/git/lib.git
Log | Files | Refs | README | LICENSE

commit f305b40e83ffb987c22c94870d7b814abfe3c4a2
parent b1cc1f35c081d62d90cb8c5848bd3e88c841fd9b
Author: triesap <tyson@radroots.org>
Date:   Sat, 21 Feb 2026 01:52:50 +0000

core: add ts-rs feature and build export hook

Diffstat:
MCargo.lock | 1+
Mcrates/core/Cargo.toml | 3+++
Acrates/core/build.rs | 39+++++++++++++++++++++++++++++++++++++++
3 files changed, 43 insertions(+), 0 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock @@ -2649,6 +2649,7 @@ dependencies = [ "rust_decimal_macros", "serde", "serde_json", + "ts-rs", "typeshare", ] diff --git a/crates/core/Cargo.toml b/crates/core/Cargo.toml @@ -5,18 +5,21 @@ edition.workspace = true authors = ["Radroots Authors"] rust-version.workspace = true license.workspace = true +build = "build.rs" [features] default = ["std", "serde", "typeshare"] std = [] serde = ["dep:serde", "rust_decimal/serde"] typeshare = ["dep:typeshare"] +ts-rs = ["dep:ts-rs"] [dependencies] rust_decimal = { workspace = true, default-features = false } rust_decimal_macros = { workspace = true } serde = { workspace = true, default-features = false, features = ["alloc", "derive"], optional = true } typeshare = { workspace = true, optional = true } +ts-rs = { workspace = true, optional = true } [dev-dependencies] serde_json = { workspace = true } diff --git a/crates/core/build.rs b/crates/core/build.rs @@ -0,0 +1,39 @@ +use std::{ + env, fs, + path::{Path, PathBuf}, +}; + +fn workspace_root(manifest_dir: &Path) -> PathBuf { + let parent = manifest_dir.parent().unwrap_or(manifest_dir); + if parent.file_name().and_then(|name| name.to_str()) == Some("crates") { + parent.parent().unwrap_or(parent).to_path_buf() + } else { + parent.to_path_buf() + } +} + +fn export_dir(crate_name: &str) -> PathBuf { + if let Some(export_dir) = env::var_os("RADROOTS_TS_RS_EXPORT_DIR") { + return PathBuf::from(export_dir); + } + let manifest_dir = PathBuf::from( + env::var("CARGO_MANIFEST_DIR").expect("missing required env var CARGO_MANIFEST_DIR"), + ); + workspace_root(&manifest_dir) + .join("target") + .join("ts-rs") + .join(crate_name) +} + +fn main() { + println!("cargo:rerun-if-changed=build.rs"); + if env::var_os("CARGO_FEATURE_TS_RS").is_some() { + let out_dir = export_dir("core"); + println!("cargo:rustc-env=TS_RS_EXPORT_DIR={}", out_dir.display()); + println!("cargo:rerun-if-env-changed=RADROOTS_TS_RS_EXPORT_DIR"); + if !out_dir.exists() { + fs::create_dir_all(&out_dir).expect("create TS export dir"); + } + println!("cargo:rerun-if-changed=src"); + } +}