Fix typescript not generating ESM and add default values for rust types!

This commit is contained in:
Fabian Stamm
2025-12-09 17:48:44 +01:00
parent b069237b91
commit ef8e97b15a
8 changed files with 33 additions and 14 deletions

View File

@ -88,6 +88,7 @@ impl CompileContext {
pub fn write_file(&self, filename: &str, content: &str) -> Result<()> {
let res_path = self.output_folder.clone().join(filename);
let res_dir = res_path.parent().context("Path has no parent!")?;
log::debug!("Writing to file {:?}", res_path);
std::fs::create_dir_all(res_dir)?;
std::fs::write(res_path, content)?;
Ok(())

View File

@ -4,6 +4,7 @@ use std::{
};
use anyhow::Result;
use log::debug;
use crate::{
compile::{Compile, CompileContext},
@ -51,6 +52,7 @@ pub fn compile<T: Compile>(ir: IR, output: &str) -> Result<()> {
for step in ir.steps.iter() {
match step {
crate::ir::Step::Type(definition) => {
debug!("Generating type {}", definition.name);
match compiler.generate_type(&mut ctx, &definition) {
Ok(_) => (),
Err(err) => {
@ -59,6 +61,7 @@ pub fn compile<T: Compile>(ir: IR, output: &str) -> Result<()> {
}
}
crate::ir::Step::Enum(definition) => {
debug!("Generating enum {}", definition.name);
match compiler.generate_enum(&mut ctx, &definition) {
Ok(_) => (),
Err(err) => {
@ -67,6 +70,7 @@ pub fn compile<T: Compile>(ir: IR, output: &str) -> Result<()> {
}
}
crate::ir::Step::Service(definition) => {
debug!("Generating service {}", definition.name);
match compiler.generate_service(&mut ctx, &definition) {
Ok(_) => (),
Err(err) => {

View File

@ -463,7 +463,17 @@ impl Compile for RustCompiler {
self.add_dependencies(&mut f, &definition.depends)?;
f.a0("#[derive(Clone, Debug, Serialize, Deserialize)]");
let only_optional = definition
.fields
.iter()
.find(|f| !f.typ.is_optional())
.is_none();
let derive_default_none = if only_optional { ", Default" } else { "" };
f.a0(format!(
"#[derive(Clone, Debug, Serialize, Deserialize{})]",
derive_default_none
));
f.a0(format!("pub struct {} {{", definition.name));
for field in definition.fields.iter() {
f.a(1, "#[allow(non_snake_case)]");

View File

@ -435,12 +435,8 @@ import {{ VerificationError }} from \"./ts_base{esm}\";
}
impl<F: Flavour> Compile for TypeScriptCompiler<F> {
fn new(options: &BTreeMap<String, String>) -> Result<Self> {
let flavour = options
.get("flavour")
.cloned()
.unwrap_or_else(|| "node".to_string());
info!("TypeScript target initialized with flavour: {}", flavour);
fn new(_options: &BTreeMap<String, String>) -> Result<Self> {
info!("TypeScript target initialized with flavour: {}", F::name());
Ok(TypeScriptCompiler {
flavour: std::marker::PhantomData,
})