Crates.io | reflect_to |
lib.rs | reflect_to |
version | |
source | src |
created_at | 2025-04-09 06:48:32.204746+00 |
updated_at | 2025-04-10 19:47:14.262736+00 |
description | Run-time type reflection and conversion |
homepage | |
repository | |
max_upload_size | |
id | 1626301 |
Cargo.toml error: | TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
serde
serialization behaviors whenever possible.typescript
flag (on by default)python
flag
By using the Reflect
derive:
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<UserPost>,
profile_image: Option<String>,
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<String>,
contents: Vec<String>,
}
#[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<String, String>,
}
the following type information can be generated at runtime (for instance as part of a wasm build process):
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<string, string>;
}
Run one of the examples via
cargo run --example to_python
or
cargo run --example to_typescript
For now, the best documentation is the typescript and python examples.