Finish implementation of typescript generator.

The implementation is still untested and will
have issues
This commit is contained in:
Fabian Stamm
2025-05-27 19:58:40 +02:00
parent 0e73f7b5b3
commit ffacba2e96
9 changed files with 381 additions and 279 deletions

View File

@ -1,13 +1,47 @@
use std::{
error::Error,
fmt::{Debug, Display},
};
use anyhow::Result;
use crate::{
compile::{Compile, CompileContext},
ir::Definition,
IR,
};
pub mod rust;
pub mod typescript;
#[derive(Debug, Clone)]
pub struct CompilerError<D: Definition> {
pub message: String,
pub definition: D,
}
impl<D: Definition> CompilerError<D> {
fn new(msg: &str, definition: D) -> Self {
Self {
message: format!("{}: {}", msg, definition.get_name()),
definition,
}
}
}
impl<D: Definition> Error for CompilerError<D> {}
impl<D: Definition> Display for CompilerError<D> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"CompilerError: {} at {:?}",
self.message,
self.definition.get_position()
)
}
}
pub fn compile<T: Compile>(ir: IR, output: &str) -> Result<()> {
let mut ctx = CompileContext::new(output);
let mut compiler = T::new(&ir.options)?;
@ -15,10 +49,29 @@ pub fn compile<T: Compile>(ir: IR, output: &str) -> Result<()> {
for step in ir.steps.iter() {
match step {
crate::ir::Step::Type(definition) => compiler.generate_type(&mut ctx, &definition)?,
crate::ir::Step::Enum(definition) => compiler.generate_enum(&mut ctx, &definition)?,
crate::ir::Step::Type(definition) => {
match compiler.generate_type(&mut ctx, &definition) {
Ok(_) => (),
Err(err) => {
return Err(CompilerError::new(&err.to_string(), definition.clone()).into())
}
}
}
crate::ir::Step::Enum(definition) => {
match compiler.generate_enum(&mut ctx, &definition) {
Ok(_) => (),
Err(err) => {
return Err(CompilerError::new(&err.to_string(), definition.clone()).into())
}
}
}
crate::ir::Step::Service(definition) => {
compiler.generate_service(&mut ctx, &definition)?
match compiler.generate_service(&mut ctx, &definition) {
Ok(_) => (),
Err(err) => {
return Err(CompilerError::new(&err.to_string(), definition.clone()).into())
}
}
}
}
}