Fix typescript not generating ESM and add default values for rust types!
This commit is contained in:
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -561,7 +561,7 @@ checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "jrpc-cli"
|
name = "jrpc-cli"
|
||||||
version = "0.1.2"
|
version = "0.1.3"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"clap",
|
"clap",
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
[package]
|
[package]
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
name = "jrpc-cli"
|
name = "jrpc-cli"
|
||||||
version = "0.1.2"
|
version = "0.1.3"
|
||||||
|
|
||||||
[workspace]
|
[workspace]
|
||||||
resolver = "2"
|
resolver = "2"
|
||||||
|
|||||||
@ -88,6 +88,7 @@ impl CompileContext {
|
|||||||
pub fn write_file(&self, filename: &str, content: &str) -> Result<()> {
|
pub fn write_file(&self, filename: &str, content: &str) -> Result<()> {
|
||||||
let res_path = self.output_folder.clone().join(filename);
|
let res_path = self.output_folder.clone().join(filename);
|
||||||
let res_dir = res_path.parent().context("Path has no parent!")?;
|
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::create_dir_all(res_dir)?;
|
||||||
std::fs::write(res_path, content)?;
|
std::fs::write(res_path, content)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
@ -4,6 +4,7 @@ use std::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
use log::debug;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
compile::{Compile, CompileContext},
|
compile::{Compile, CompileContext},
|
||||||
@ -51,6 +52,7 @@ pub fn compile<T: Compile>(ir: IR, output: &str) -> Result<()> {
|
|||||||
for step in ir.steps.iter() {
|
for step in ir.steps.iter() {
|
||||||
match step {
|
match step {
|
||||||
crate::ir::Step::Type(definition) => {
|
crate::ir::Step::Type(definition) => {
|
||||||
|
debug!("Generating type {}", definition.name);
|
||||||
match compiler.generate_type(&mut ctx, &definition) {
|
match compiler.generate_type(&mut ctx, &definition) {
|
||||||
Ok(_) => (),
|
Ok(_) => (),
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
@ -59,6 +61,7 @@ pub fn compile<T: Compile>(ir: IR, output: &str) -> Result<()> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
crate::ir::Step::Enum(definition) => {
|
crate::ir::Step::Enum(definition) => {
|
||||||
|
debug!("Generating enum {}", definition.name);
|
||||||
match compiler.generate_enum(&mut ctx, &definition) {
|
match compiler.generate_enum(&mut ctx, &definition) {
|
||||||
Ok(_) => (),
|
Ok(_) => (),
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
@ -67,6 +70,7 @@ pub fn compile<T: Compile>(ir: IR, output: &str) -> Result<()> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
crate::ir::Step::Service(definition) => {
|
crate::ir::Step::Service(definition) => {
|
||||||
|
debug!("Generating service {}", definition.name);
|
||||||
match compiler.generate_service(&mut ctx, &definition) {
|
match compiler.generate_service(&mut ctx, &definition) {
|
||||||
Ok(_) => (),
|
Ok(_) => (),
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
|
|||||||
@ -463,7 +463,17 @@ impl Compile for RustCompiler {
|
|||||||
|
|
||||||
self.add_dependencies(&mut f, &definition.depends)?;
|
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));
|
f.a0(format!("pub struct {} {{", definition.name));
|
||||||
for field in definition.fields.iter() {
|
for field in definition.fields.iter() {
|
||||||
f.a(1, "#[allow(non_snake_case)]");
|
f.a(1, "#[allow(non_snake_case)]");
|
||||||
|
|||||||
@ -435,12 +435,8 @@ import {{ VerificationError }} from \"./ts_base{esm}\";
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<F: Flavour> Compile for TypeScriptCompiler<F> {
|
impl<F: Flavour> Compile for TypeScriptCompiler<F> {
|
||||||
fn new(options: &BTreeMap<String, String>) -> Result<Self> {
|
fn new(_options: &BTreeMap<String, String>) -> Result<Self> {
|
||||||
let flavour = options
|
info!("TypeScript target initialized with flavour: {}", F::name());
|
||||||
.get("flavour")
|
|
||||||
.cloned()
|
|
||||||
.unwrap_or_else(|| "node".to_string());
|
|
||||||
info!("TypeScript target initialized with flavour: {}", flavour);
|
|
||||||
Ok(TypeScriptCompiler {
|
Ok(TypeScriptCompiler {
|
||||||
flavour: std::marker::PhantomData,
|
flavour: std::marker::PhantomData,
|
||||||
})
|
})
|
||||||
|
|||||||
@ -4,7 +4,7 @@ use libjrpc::{
|
|||||||
targets::{
|
targets::{
|
||||||
csharp::CSharpCompiler,
|
csharp::CSharpCompiler,
|
||||||
rust::RustCompiler,
|
rust::RustCompiler,
|
||||||
typescript::{Node, TypeScriptCompiler},
|
typescript::{Node, TypeScriptCompiler, ESM},
|
||||||
},
|
},
|
||||||
FileProcessor,
|
FileProcessor,
|
||||||
};
|
};
|
||||||
@ -66,7 +66,7 @@ pub fn main() -> Result<()> {
|
|||||||
libjrpc::targets::compile::<TypeScriptCompiler<Node>>(ir, output_dir)?
|
libjrpc::targets::compile::<TypeScriptCompiler<Node>>(ir, output_dir)?
|
||||||
}
|
}
|
||||||
"ts-esm" => {
|
"ts-esm" => {
|
||||||
libjrpc::targets::compile::<TypeScriptCompiler<Node>>(ir, output_dir)?
|
libjrpc::targets::compile::<TypeScriptCompiler<ESM>>(ir, output_dir)?
|
||||||
}
|
}
|
||||||
"csharp" => libjrpc::targets::compile::<CSharpCompiler>(ir, output_dir)?,
|
"csharp" => libjrpc::targets::compile::<CSharpCompiler>(ir, output_dir)?,
|
||||||
_ => {
|
_ => {
|
||||||
|
|||||||
14
src/test.rs
14
src/test.rs
@ -6,12 +6,12 @@ use std::{
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn compare_tools() {
|
fn compare_tools() {
|
||||||
let targets = vec!["rust"];
|
let targets = vec!["js-esm"];
|
||||||
for target in targets {
|
for target in targets {
|
||||||
std::fs::remove_dir_all("./tests").unwrap();
|
std::fs::remove_dir_all("./tests").unwrap();
|
||||||
std::fs::create_dir_all("./tests").unwrap();
|
std::fs::create_dir_all("./tests").unwrap();
|
||||||
|
|
||||||
Command::new("cargo")
|
let result1 = Command::new("cargo")
|
||||||
.arg("run")
|
.arg("run")
|
||||||
.arg("--")
|
.arg("--")
|
||||||
.arg("compile")
|
.arg("compile")
|
||||||
@ -26,7 +26,11 @@ fn compare_tools() {
|
|||||||
.wait()
|
.wait()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
Command::new("node")
|
if !result1.success() {
|
||||||
|
panic!("Failed to generate Rust code");
|
||||||
|
}
|
||||||
|
|
||||||
|
let result2 = Command::new("node")
|
||||||
.arg("JsonRPC/lib/jrpc.js")
|
.arg("JsonRPC/lib/jrpc.js")
|
||||||
.arg("compile")
|
.arg("compile")
|
||||||
.arg("--verbose")
|
.arg("--verbose")
|
||||||
@ -40,6 +44,10 @@ fn compare_tools() {
|
|||||||
.wait()
|
.wait()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
if !result2.success() {
|
||||||
|
panic!("Failed to generate JavaScript code");
|
||||||
|
}
|
||||||
|
|
||||||
let rust_files = walkdir::WalkDir::new("tests/rust")
|
let rust_files = walkdir::WalkDir::new("tests/rust")
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|e| e.unwrap())
|
.map(|e| e.unwrap())
|
||||||
|
|||||||
Reference in New Issue
Block a user