#reflection #typescript #default #convert #python

reflect_to

Run-time type reflection and conversion

5 releases

new 0.1.6 Apr 10, 2025
0.1.5 Apr 9, 2025
0.1.4 Apr 9, 2025
0.1.3 Apr 9, 2025
0.1.2 Apr 9, 2025

#2117 in Encoding

Download history 395/week @ 2025-04-04

398 downloads per month

MIT license

80KB
1.5K SLoC

Reflect To

  • Generate type information in other languages based on rust types.
    • Adhere to serde serialization behaviors whenever possible.
  • Builds upon a reusable rtti foundation in rust.
  • Supported language type conversions:
    • typescript via typescriptflag (on by default)
    • python via python flag
      • NOTE: Still an active WIP!

Typescript Example

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>;
}

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 and python examples.

Dependencies

~2.4–4MB
~70K SLoC