typescript-webidl 0.0.2

WebIDL parser and TypeScript converter for Rusty TypeScript
Documentation
#![doc = include_str!("readme.md")]
#![warn(missing_docs)]
#![feature(new_range_api)]

pub use oak_idl::ast;

use std::fs;

/// WebIDL 到 TypeScript 转换模块
pub mod converter;

/// WebIDL 类型定义模块
pub mod types;

/// WebIDL 类型检查模块
pub mod type_checker;

/// 调试模块
pub mod debug;

/// 解析 WebIDL 字符串
///
/// # 参数
/// - `idl`: 要解析的 WebIDL 字符串
///
/// # 返回值
/// - `Ok(oak_idl::ast::IdlRoot)`: 解析成功,返回 WebIDL AST
/// - `Err(String)`: 解析失败,返回错误信息
pub fn parse(idl: &str) -> Result<oak_idl::ast::IdlRoot, String> {
    use oak_idl::{builder::IdlBuilder, language::IdlLanguage};
    use oak_core::{builder::Builder, parser::session::ParseSession, source::SourceText};
    
    let language = IdlLanguage::default();
    let builder = IdlBuilder::new(&language);
    let source = SourceText::new(idl.to_string());
    let mut cache = ParseSession::default();
    let result = builder.build(&source, &[], &mut cache);
    result.result.map_err(|e| format!("{:?}", e))
}

/// 从文件读取并解析 WebIDL
///
/// # 参数
/// - `file_path`: WebIDL 文件路径
///
/// # 返回值
/// - `Ok(oak_idl::ast::IdlRoot)`: 读取和解析成功,返回 WebIDL AST
/// - `Err(String)`: 读取或解析失败,返回错误信息
pub fn parse_file(file_path: &str) -> Result<oak_idl::ast::IdlRoot, String> {
    if file_path.is_empty() {
        return Err("Empty file path".to_string());
    }

    match fs::read_to_string(file_path) {
        Ok(content) => {
            if content.trim().is_empty() {
                return Err("Empty WebIDL file".to_string());
            }
            parse(&content)
        }
        Err(error) => {
            let error_msg = format!("Failed to read file '{}': {}", file_path, error);
            Err(error_msg)
        }
    }
}

/// 将 WebIDL AST 转换为 TypeScript 类型定义
///
/// # 参数
/// - `root`: WebIDL AST 根节点
///
/// # 返回值
/// - 生成的 TypeScript 类型定义字符串
pub fn convert_to_typescript(root: &oak_idl::ast::IdlRoot) -> String {
    converter::convert(root)
}