| Crates.io | zorsh-gen-rs |
| lib.rs | zorsh-gen-rs |
| version | 0.1.4 |
| created_at | 2025-02-22 06:13:23.83201+00 |
| updated_at | 2025-02-23 02:04:16.725456+00 |
| description | Zorsh generator for Rust |
| homepage | https://zorsh.dev |
| repository | https://github.com/r-near/zorsh-gen-rs |
| max_upload_size | |
| id | 1565160 |
| size | 107,060 |
A code generator that turns Rust's Borsh structs into Zorsh TypeScript schemas, using Rust itself as the schema language.
zorsh-gen-rs takes your existing Rust types and automatically generates Zorsh schemas for TypeScript. One command, zero maintenance, complete type safety.
# Using npx (Node.js)
npx @zorsh/cli ./src/models ./generated
# Or using pnpm
pnpm dlx @zorsh/cli ./src/models ./generated
Your Rust types:
use borsh::{BorshDeserialize, BorshSerialize};
#[derive(BorshDeserialize, BorshSerialize)]
pub struct Player {
name: String,
score: u32,
inventory: HashMap<String, Vec<Item>>,
}
Automatically become Zorsh schemas:
import { b } from '@zorsh/zorsh';
export const PlayerSchema = b.struct({
name: b.string(),
score: b.u32(),
inventory: b.hashMap(b.string(), b.vec(ItemSchema))
});
Most serialization formats force you to define your data structures twice: once in your schema language (like .proto files for Protocol Buffers or SDL for GraphQL), and again in your programming language. With Borsh, your data structures are already defined in Rust - that's where they live. zorsh-gen-rs recognizes this and lets your existing Rust types act as the schema language for Zorsh, the TypeScript implementation of Borsh.
No duplicate definitions. No schema syncing. No extra maintenance. Just:
Choose the installation method that works best for your workflow:
# Install globally with npm
npm install -g @zorsh/cli
# Install as a dev dependency in your project
npm install --save-dev @zorsh/cli
# Or use without installing
npx @zorsh/cli
pnpm dlx @zorsh/cli
# Install from crates.io
cargo install zorsh-gen-rs
# Or build from source
git clone https://github.com/r-near/zorsh-gen-rs
cd zorsh-gen-rs
cargo install --path .
# Unix-like systems (Linux, macOS)
curl --proto '=https' --tlsv1.2 -LsSf https://github.com/r-near/zorsh-gen-rs/releases/latest/download/zorsh-gen-rs-installer.sh | sh
# Windows PowerShell
powershell -ExecutionPolicy Bypass -c "irm https://github.com/r-near/zorsh-gen-rs/releases/latest/download/zorsh-gen-rs-installer.ps1 | iex"
# Homebrew (macOS and Linux)
brew install r-near/tap/zorsh-gen-rs
Pre-built binaries are available for the following platforms:
Download the appropriate binary for your platform from the releases page.
# Basic usage
zorsh-gen-rs <input-dir> <output-dir>
# With options
zorsh-gen-rs --flat-output --only-annotated ./src/models ./generated
use zorsh_gen_rs::{ZorshGen, Config};
let config = Config::default();
let generator = ZorshGen::new(config);
// Convert a directory
generator.convert("./src/models", "./generated")?;
// Or convert a string
let zorsh_code = zorsh_gen_rs::convert_str(rust_code)?;
u8 through u128, i8 through i128, f32, f64)String and &strVec<T>, [T; N], HashMap<K, V>Option<T>zorsh-gen-rs preserves your Rust module structure in the generated TypeScript:
src/
models/
player.rs
items/
mod.rs
weapon.rs
Becomes:
generated/
models/
player.ts
items/
index.ts
weapon.ts
let config = Config {
// Only process structs with #[derive(BorshSerialize)]
only_annotated: true,
// Skip certain paths
ignored_patterns: vec!["tests/", "examples/"],
// Output structure (nested or flat)
output_structure: OutputStructure::Nested,
};
Contributions are welcome! Before you start:
Report bugs
Suggest features
Submit pull requests
MIT