Clone the repo and navigate to the subdirectory containing the files you want to transpile types.

Here's a basic script that achieves this by reading the files, looking for struct and enum definitions in Rust, and then converting them into TypeScript interfaces and enums. This script assumes the Rust files are locally available on your file system.

Python Script Setup

  1. Dependencies: This script uses the re module for regex operations, which is built-in and doesn't require external dependencies.
  2. Assumptions:

Python Script

import os
import re

def rust_to_ts(rust_code):
    # Define a basic mapping from Rust types to TypeScript types
    type_map = {
        'i32': 'number',
        'u32': 'number',
        'i64': 'bigint',
        'u64': 'bigint',
        'f32': 'number',
        'f64': 'number',
        'bool': 'boolean',
        'String': 'string',
        '&str': 'string',
        'Vec': 'Array',
        'Option': 'null |'  # Requires further handling to determine specific type
    }

    # Helper function to map Rust types to TypeScript types
    def map_type(rust_type):
        # Handle generic types like Vec<T> or Option<T>
        if '<' in rust_type and '>' in rust_type:
            base_type = rust_type.split('<')[0]
            inner_type = rust_type.split('<')[1].split('>')[0]
            ts_inner_type = map_type(inner_type)
            if base_type in type_map:
                return f"{type_map[base_type]}<{ts_inner_type}>"
            return f"{base_type}<{ts_inner_type}>"
        return type_map.get(rust_type, rust_type)

    # Convert Rust structs to TypeScript interfaces
    structs = re.findall(r"pub struct (\\w+)\\s*{([^}]*)}", rust_code, re.S)
    ts_structs = []
    for name, fields in structs:
        ts_fields = []
        for field in fields.strip().split('\\n'):
            if field.strip():
                fname, ftype = [part.strip() for part in field.strip().split(':')]
                ftype = ftype.replace(';', '').strip()
                ts_type = map_type(ftype)  # Map Rust types to TS types
                ts_fields.append(f"{fname}: {ts_type};")
        ts_struct = f"interface {name} {{\\n  " + "\\n  ".join(ts_fields) + "\\n}"
        ts_structs.append(ts_struct)
    
    # Convert Rust enums to TypeScript enums
    enums = re.findall(r"pub enum (\\w+)\\s*{([^}]*)}", rust_code, re.S)
    ts_enums = []
    for name, members in enums:
        ts_members = []
        for member in members.strip().split(','):
            if member.strip():
                ts_members.append(member.strip())
        ts_enum = f"enum {name} {{\\n  " + ",\\n  ".join(ts_members) + "\\n}"
        ts_enums.append(ts_enum)

    return ts_structs + ts_enums

def scan_rust_files(directory):
    for filename in os.listdir(directory):
        if filename.endswith('.rs'):
            with open(os.path.join(directory, filename), 'r', encoding='utf-8') as file:
                rust_code = file.read()
                ts_definitions = rust_to_ts(rust_code)
                for definition in ts_definitions:
                    print(definition)
                    print()

# Example usage
scan_rust_files('path_to_your_rust_files')

Explanation

Adjustments Needed

This script provides a basic framework for your task and can be expanded to handle more complex scenarios or integrated into a larger toolchain as needed.