# schema-bridge
A minimal, practical Rust library for generating TypeScript type definitions from Rust types.
## Features
- **Simple to use**: Just derive `SchemaBridge` on 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`:
```toml
[dependencies]
schema-bridge = "0.2"
serde = { version = "1.0", features = ["derive"] }
```
## Quick Start
```rust
use schema_bridge::{SchemaBridge, export_types};
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, SchemaBridge)]
struct User {
name: String,
age: i32,
email: Option<String>,
}
#[derive(Serialize, Deserialize, SchemaBridge)]
enum Status {
Active,
Inactive,
}
fn main() {
export_types!("bindings.ts", User, Status).unwrap();
}
```
This generates `bindings.ts`:
```typescript
// This file is auto-generated by schema-bridge
export type User = {
name: string;
age: number;
## Supported Types
### Primitive Types
| `String` | `string` |
| `bool` | `boolean` |
| `char` | `string` |
| `()` (unit) | `null` |
### Numeric Types
All numeric types map to TypeScript's `number`:
| `i8`, `i16`, `i32`, `i64`, `i128`, `isize` | `number` |
| `u8`, `u16`, `u32`, `u64`, `u128`, `usize` | `number` |
| `f32`, `f64` | `number` |
### Collection Types
| `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:
| `Box<T>` | `T` | `Box<String>` → `string` |
| `Rc<T>` | `T` | `Rc<i32>` → `number` |
| `Arc<T>` | `T` | `Arc<String>` → `string` |
### Path Types
| `PathBuf` | `string` |
### Result and Union Types
| `Result<T, E>` | `T \| E` | `Result<String, String>` → `string \| string` |
### Tuple Types
Tuples (up to 6 elements) map to TypeScript tuple types:
| `(T,)` | `[T]` |
| `(T1, T2)` | `[T1, T2]` |
| `(T1, T2, T3)` | `[T1, T2, T3]` |
| ... | ... |
| `(T1, ..., T6)` | `[T1, ..., T6]` |
## Advanced Examples
### Complex Types
```rust
use schema_bridge::SchemaBridge;
use std::collections::{HashMap, HashSet};
use std::path::PathBuf;
use std::sync::Arc;
#[derive(SchemaBridge)]
pub struct ComplexType {
// Numeric types
pub id: u64, // number
pub score: f32, // number
// Collections
pub tags: HashSet<String>, // string[]
pub metadata: HashMap<String, i32>, // Record<string, number>
// Smart pointers
pub shared: Arc<String>, // string
// Tuples
pub coordinates: (f64, f64), // [number, number]
// Paths
pub file_path: PathBuf, // string
// Complex combinations
pub data: Option<Vec<Arc<String>>>, // string[] | null
}
```
Generates:
```typescript
export type ComplexType = {
id: number;
score: number;
tags: string[];
metadata: Record<string, number>;
shared: string;
coordinates: [number, number];
file_path: string;
```
### Serde Attributes
schema-bridge respects serde attributes:
```rust
use schema_bridge::SchemaBridge;
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, SchemaBridge)]
#[serde(rename_all = "camelCase")]
pub struct ApiResponse {
user_name: String, // becomes userName in TypeScript
is_active: bool, // becomes isActive in TypeScript
}
```
## 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.