#![doc = include_str!("readme.md")]
#![warn(missing_docs)]
#![feature(new_range_api)]
pub use oak_idl::ast;
use std::fs;
pub mod converter;
pub mod types;
pub mod type_checker;
pub mod debug;
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))
}
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)
}
}
}
pub fn convert_to_typescript(root: &oak_idl::ast::IdlRoot) -> String {
converter::convert(root)
}