1#![doc = include_str!("readme.md")]
2#![warn(missing_docs)]
3#![feature(new_range_api)]
4
5pub use oak_idl::ast;
6
7use std::fs;
8
9pub mod converter;
11
12pub mod types;
14
15pub mod type_checker;
17
18pub mod debug;
20
21pub fn parse(idl: &str) -> Result<oak_idl::ast::IdlRoot, String> {
30 use oak_idl::{builder::IdlBuilder, language::IdlLanguage};
31 use oak_core::{builder::Builder, parser::session::ParseSession, source::SourceText};
32
33 let language = IdlLanguage::default();
34 let builder = IdlBuilder::new(&language);
35 let source = SourceText::new(idl.to_string());
36 let mut cache = ParseSession::default();
37 let result = builder.build(&source, &[], &mut cache);
38 result.result.map_err(|e| format!("{:?}", e))
39}
40
41pub fn parse_file(file_path: &str) -> Result<oak_idl::ast::IdlRoot, String> {
50 if file_path.is_empty() {
51 return Err("Empty file path".to_string());
52 }
53
54 match fs::read_to_string(file_path) {
55 Ok(content) => {
56 if content.trim().is_empty() {
57 return Err("Empty WebIDL file".to_string());
58 }
59 parse(&content)
60 }
61 Err(error) => {
62 let error_msg = format!("Failed to read file '{}': {}", file_path, error);
63 Err(error_msg)
64 }
65 }
66}
67
68pub fn convert_to_typescript(root: &oak_idl::ast::IdlRoot) -> String {
76 converter::convert(root)
77}