https://github.com/nobane/reflect_to
Generate type information in other languages based on rust types.
https://github.com/nobane/reflect_to
python reflection rtti rust typescript
Last synced: 18 days ago
JSON representation
Generate type information in other languages based on rust types.
- Host: GitHub
- URL: https://github.com/nobane/reflect_to
- Owner: nobane
- License: mit
- Created: 2025-04-09T02:56:20.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2025-04-10T19:46:34.000Z (12 months ago)
- Last Synced: 2025-06-18T05:40:10.078Z (9 months ago)
- Topics: python, reflection, rtti, rust, typescript
- Language: Rust
- Homepage:
- Size: 71.3 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Reflect To
- Generate type information in other languages based on rust types.
- Adhere to `serde` serialization behaviors whenever possible.
- Builds upon a reusable [rtti](https://en.wikipedia.org/wiki/Run-time_type_information) foundation in rust.
- Supported language type conversions:
- typescript via `typescript`flag (on by default)
- python via `python` flag
- **NOTE**: Still an active WIP!
# Typescript Example
By using the `Reflect` derive:
```rs
use reflect_to::Reflect;
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, path::PathBuf};
#[derive(Reflect, Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct User {
email: String,
is_active: bool,
uploaded_files: Vec,
profile_image: Option,
settings: UserSettings,
status: UserStatus,
}
#[derive(Reflect, Serialize, Deserialize, Debug, Clone)]
pub enum UserStatus {
Offline,
Online { status: String },
Unknown(String),
}
#[derive(Reflect, Serialize, Deserialize, Debug, Clone)]
pub struct UserPost {
post_name: Option,
contents: Vec,
}
#[derive(Reflect, Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct UserSettings {
theme_path: PathBuf,
email_notifications: bool,
#[serde(rename = "custom")]
custom_settings: HashMap,
}
```
the following type information can be generated at runtime (for instance as part of a wasm build process):
```ts
export interface User {
email: string;
isActive: boolean;
uploadedFiles: UserPost[];
profileImage: string | null;
settings: UserSettings;
status: UserStatus;
}
export type UserStatus =
"Offline"
| { "Online": {
status: string;
} }
| { "Unknown": string }
export interface UserPost {
post_name: string | null;
contents: string[];
}
export interface UserSettings {
themePath: string
emailNotifications: boolean
custom: Record;
}
```
# Demo
Run one of the examples via
```
cargo run --example to_python
```
or
```
cargo run --example to_typescript
```
# Documentation
For now, the best documentation is the [typescript](crates/to_typescript/examples/to_typescript.rs) and [python](crates/to_python/examples/to_python.rs) examples.