schema-bridge
A minimal, practical Rust library for generating TypeScript type definitions from Rust types.
Features
- Simple to use: Just derive
SchemaBridgeon your types - Serde compatible: Works with serde attributes
- Minimal dependencies: No heavy tooling required
- Workspace friendly: Organized as a clean workspace
Quick Start
Add to your Cargo.toml:
[]
= { = "path/to/schema-bridge/crates/schema-bridge" }
= { = "1.0", = ["derive"] }
Define your types and export them:
use ;
use ;
This generates bindings.ts:
// This file is auto-generated by schema-bridge
export type User = { name: string; age: number; email: string | null; };
export type Status = 'Active' | 'Inactive';
Architecture
The library is structured as a workspace with three crates:
- schema-bridge: Main entry point (facade)
- schema-bridge-core: Core traits and types
- schema-bridge-macro: Procedural macro for
#[derive(SchemaBridge)]
This follows the pattern used by popular libraries like serde.
Supported Types
- Primitives:
String,i32,f64,bool, etc. - Containers:
Vec<T>,Option<T> - Structs with named fields
- Enums (simple variants)
- Newtype pattern:
struct Wrapper(InnerType)- delegates to wrapped type - Tuple structs:
struct Point(f64, f64)- generates TypeScript tuples - Serde attributes:
#[serde(rename_all = "...")]for name transformations
Serde Attribute Support
The library respects #[serde(rename_all)] attributes on enums:
Supported transformations:
snake_case:MyVariant→my_variantcamelCase:MyVariant→myVariantPascalCase:MyVariant→MyVariant(no change)SCREAMING_SNAKE_CASE:MyVariant→MY_VARIANTkebab-case:MyVariant→my-variant
String Conversion Support
Generate Display and FromStr implementations for easy string conversion:
// Enable Display + FromStr
// Usage:
let style = Brainstorm;
let s = style.to_string; // "brainstorm"
let parsed: TalkStyle = "casual".parse.unwrap; // TalkStyle::Casual
Perfect for:
- String-based APIs
- Command-line arguments
- Configuration parsing
- URL parameters
Newtype Pattern for External Types
Perfect for wrapping external types you don't control:
// Wrap an external enum
;
// Wrap a primitive for type safety
; // Generates: export type UserId = string;
Use with Tauri
Perfect for Tauri applications where you need to keep Rust and TypeScript types in sync:
// In build.rs or a separate build tool
use export_types;
Or for more control:
use ;
License
MIT OR Apache-2.0