Working on implementing the typescript target

This commit is contained in:
Fabian Stamm
2025-05-26 23:54:41 +02:00
parent c8e72dbba8
commit 45ebb2c0d7
5 changed files with 472 additions and 53 deletions

View File

@ -63,28 +63,6 @@ impl RustCompiler {
}
}
fn to_snake(name: &str) -> String {
let mut result = String::new();
let mut last_upper = false;
for c in name.chars() {
if c.is_uppercase() {
if last_upper {
result.push(c.to_ascii_lowercase());
} else {
if !result.is_empty() {
result.push('_');
}
result.push(c.to_ascii_lowercase());
}
last_upper = true;
} else {
result.push(c);
last_upper = false;
}
}
result
}
fn generate_service_lib(ctx: &CompileContext, ir: &IR) -> Result<()> {
let mut f = FileGenerator::new();
let mut fc = FileGenerator::new();
@ -96,32 +74,40 @@ impl RustCompiler {
for step in ir.steps.iter() {
match step {
Step::Type(def) => {
f.a0(format!("mod {};", Self::to_snake(&def.name)));
f.a0(format!("mod {};", CompileContext::to_snake(&def.name)));
f.a(
0,
format!("pub use {}::{};", Self::to_snake(&def.name), def.name),
format!(
"pub use {}::{};",
CompileContext::to_snake(&def.name),
def.name
),
);
}
Step::Enum(def) => {
f.a0(format!("mod {};", Self::to_snake(&def.name)));
f.a0(format!("mod {};", CompileContext::to_snake(&def.name)));
f.a(
0,
format!("pub use {}::{};", Self::to_snake(&def.name), def.name),
format!(
"pub use {}::{};",
CompileContext::to_snake(&def.name),
def.name
),
);
}
Step::Service(def) => {
fs.a0(format!("mod {};", Self::to_snake(&def.name)));
fs.a0(format!("mod {};", CompileContext::to_snake(&def.name)));
fs.a0(format!(
"pub use {}::{{ {}, {}Handler }};",
Self::to_snake(&def.name),
CompileContext::to_snake(&def.name),
def.name,
def.name
));
fc.a0(format!("mod {};", Self::to_snake(&def.name)));
fc.a0(format!("mod {};", CompileContext::to_snake(&def.name)));
fc.a0(format!(
"pub use {}::{};",
Self::to_snake(&def.name),
CompileContext::to_snake(&def.name),
def.name,
));
}
@ -131,9 +117,9 @@ impl RustCompiler {
f.a0("pub mod server;");
f.a0("pub mod client;");
ctx.write_file("src/lib.rs", f.get_content())?;
ctx.write_file("src/server/mod.rs", fs.get_content())?;
ctx.write_file("src/client/mod.rs", fc.get_content())?;
ctx.write_file("src/lib.rs", &f.get_content())?;
ctx.write_file("src/server/mod.rs", &fs.get_content())?;
ctx.write_file("src/client/mod.rs", &fc.get_content())?;
Ok(())
}
@ -304,8 +290,11 @@ impl RustCompiler {
f.a0("}");
ctx.write_file(
&format!("src/server/{}.rs", Self::to_snake(&definition.name)),
f.into_content(),
&format!(
"src/server/{}.rs",
CompileContext::to_snake(&definition.name)
),
&f.into_content(),
)?;
Ok(())
@ -396,8 +385,11 @@ impl RustCompiler {
f.a0("}");
ctx.write_file(
&format!("src/client/{}.rs", Self::to_snake(&definition.name)),
f.into_content(),
&format!(
"src/client/{}.rs",
CompileContext::to_snake(&definition.name)
),
&f.into_content(),
)?;
Ok(())
@ -428,14 +420,14 @@ impl Compile for RustCompiler {
fn start(&mut self, ctx: &mut CompileContext) -> anyhow::Result<()> {
ctx.write_file(
"Cargo.toml",
include_str!("../../templates/Rust/Cargo.toml")
&include_str!("../../templates/Rust/Cargo.toml")
.to_owned()
.replace("__name__", &self.crate_name),
)?;
ctx.write_file(
"src/base_lib.rs",
include_str!("../../templates/Rust/src/lib.rs").to_owned(),
&include_str!("../../templates/Rust/src/lib.rs").to_owned(),
)?;
Ok(())
@ -518,8 +510,8 @@ impl Compile for RustCompiler {
f.a0("}");
ctx.write_file(
&format!("src/{}.rs", Self::to_snake(&definition.name)),
f.into_content(),
&format!("src/{}.rs", CompileContext::to_snake(&definition.name)),
&f.into_content(),
)?;
Ok(())
@ -544,8 +536,8 @@ impl Compile for RustCompiler {
f.a0("}");
ctx.write_file(
&format!("src/{}.rs", Self::to_snake(&definition.name)),
f.into_content(),
&format!("src/{}.rs", CompileContext::to_snake(&definition.name)),
&f.into_content(),
)?;
Ok(())