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
- Comprehensive std support: Extensive support for Rust standard library types
- Minimal dependencies: No heavy tooling required
Installation
Add to your Cargo.toml:
[]
= "0.2"
= { = "1.0", = ["derive"] }
Quick Start
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";
Supported Types
Primitive Types
| Rust Type | TypeScript Type |
|---|---|
String |
string |
bool |
boolean |
char |
string |
() (unit) |
null |
Numeric Types
All numeric types map to TypeScript's number:
| Rust Type | TypeScript Type |
|---|---|
i8, i16, i32, i64, i128, isize |
number |
u8, u16, u32, u64, u128, usize |
number |
f32, f64 |
number |
Collection Types
| Rust Type | TypeScript Type | Example |
|---|---|---|
Vec<T> |
T[] |
Vec<string> → string[] |
Option<T> |
T | null |
Option<i32> → number | null |
HashMap<K, V> |
Record<K, V> |
HashMap<String, i32> → Record<string, number> |
BTreeMap<K, V> |
Record<K, V> |
BTreeMap<String, bool> → Record<string, boolean> |
HashSet<T> |
T[] |
HashSet<String> → string[] |
BTreeSet<T> |
T[] |
BTreeSet<i32> → number[] |
Smart Pointers
Smart pointers are transparent - they map to their inner type:
| Rust Type | TypeScript Type | Example |
|---|---|---|
Box<T> |
T |
Box<String> → string |
Rc<T> |
T |
Rc<i32> → number |
Arc<T> |
T |
Arc<String> → string |
Path Types
| Rust Type | TypeScript Type |
|---|---|
PathBuf |
string |
Result and Union Types
| Rust Type | TypeScript Type | Example |
|---|---|---|
Result<T, E> |
T | E |
Result<String, String> → string | string |
Tuple Types
Tuples (up to 6 elements) map to TypeScript tuple types:
| Rust Type | TypeScript Type |
|---|---|
(T,) |
[T] |
(T1, T2) |
[T1, T2] |
(T1, T2, T3) |
[T1, T2, T3] |
| ... | ... |
(T1, ..., T6) |
[T1, ..., T6] |
Advanced Examples
Complex Types
use SchemaBridge;
use ;
use PathBuf;
use Arc;
Generates:
export type ComplexType = {
id: number;
score: number;
tags: string[];
metadata: Record<string, number>;
shared: string;
coordinates: [number, number];
file_path: string;
data: string[] | null;
};
Serde Attributes
schema-bridge respects serde attributes:
use SchemaBridge;
use ;
Architecture
This library is organized as a workspace with the following crates:
- schema-bridge: Main crate (re-exports everything)
- schema-bridge-core: Core traits and types
- schema-bridge-macro: Derive macro implementation
License
This project is licensed under the MIT License.