Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0a81e90e1b | ||
|
|
c29dafb042 | ||
|
|
4c7084563f | ||
|
|
ef8e97b15a | ||
|
|
b069237b91 |
1683
Cargo.lock
generated
1683
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -1,17 +1,18 @@
|
||||
[package]
|
||||
edition = "2021"
|
||||
name = "jrpc-cli"
|
||||
version = "0.1.1"
|
||||
version = "0.2.0"
|
||||
|
||||
[workspace]
|
||||
resolver = "2"
|
||||
members = [".", "zed", "libjrpc"]
|
||||
members = [".", "zed", "libjrpc", "libjrpc/templates/Rust"]
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1"
|
||||
clap = { version = "4", features = ["derive"] }
|
||||
libjrpc = { path = "libjrpc" }
|
||||
log = "0.4"
|
||||
simple_logger = { version = "5.0.0", features = ["threads"] }
|
||||
simple_logger = { version = "5", features = ["threads"] }
|
||||
|
||||
[dev-dependencies]
|
||||
walkdir = "2"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "libjrpc"
|
||||
version = "0.1.0"
|
||||
version = "0.1.5"
|
||||
edition = "2021"
|
||||
|
||||
[features]
|
||||
@@ -8,9 +8,9 @@ default = ["http"]
|
||||
http = ["dep:reqwest", "dep:url"]
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0.89"
|
||||
lazy_static = "1.5.0"
|
||||
log = "0.4.22"
|
||||
regex = "1.10.6"
|
||||
reqwest = { version = "0.12.7", optional = true, features = ["blocking"] }
|
||||
url = { version = "2.5.2", optional = true }
|
||||
anyhow = "1"
|
||||
lazy_static = "1"
|
||||
log = "0.4"
|
||||
regex = "1"
|
||||
reqwest = { version = "0.13", optional = true, features = ["blocking"] }
|
||||
url = { version = "2", optional = true }
|
||||
|
||||
@@ -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(())
|
||||
|
||||
404
libjrpc/src/targets/csharp.rs
Normal file
404
libjrpc/src/targets/csharp.rs
Normal file
@@ -0,0 +1,404 @@
|
||||
use anyhow::Result;
|
||||
use log::warn;
|
||||
use std::collections::{BTreeMap, BTreeSet};
|
||||
use std::fmt::format;
|
||||
|
||||
use crate::compile::{Compile, CompileContext, FileGenerator};
|
||||
use crate::ir::{BaseType, EnumDefinition, ServiceDefinition, Step, Type, TypeDefinition};
|
||||
use crate::shared::Keywords;
|
||||
use crate::IR;
|
||||
|
||||
pub struct CSharpCompiler {
|
||||
namespace: String,
|
||||
}
|
||||
|
||||
static CSHARP_KEYWORDS: [&'static str; 5] = ["event", "internal", "public", "private", "static"];
|
||||
|
||||
impl CSharpCompiler {
|
||||
fn type_to_csharp(typ: &BaseType) -> String {
|
||||
match typ {
|
||||
BaseType::String => "string".to_string(),
|
||||
BaseType::Int => "long".to_string(),
|
||||
BaseType::Float => "double".to_string(),
|
||||
BaseType::Bool => "bool".to_string(),
|
||||
BaseType::Bytes => todo!("Bytes are not implemented for C#"),
|
||||
BaseType::Void => "void".to_string(),
|
||||
BaseType::Custom(name) => name.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
fn type_to_csharp_ext(typ: &Type) -> String {
|
||||
let mut result = Self::type_to_csharp(&typ.0);
|
||||
|
||||
let (optional, array, map) = typ.1.get_flags();
|
||||
|
||||
if array {
|
||||
result = format!("IList<{}>", result);
|
||||
}
|
||||
if let Some(map) = &map {
|
||||
result = format!("Dictionary<{}, {}>", Self::type_to_csharp(&map), result);
|
||||
}
|
||||
if optional {
|
||||
result = format!("{}?", result);
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
fn fix_keyword_name(name: &str) -> String {
|
||||
if CSHARP_KEYWORDS.contains(&name) {
|
||||
format!("{}_", name)
|
||||
} else {
|
||||
name.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
fn generate_service_server(
|
||||
&mut self,
|
||||
ctx: &mut CompileContext,
|
||||
definition: &ServiceDefinition,
|
||||
) -> anyhow::Result<()> {
|
||||
let mut f = FileGenerator::new();
|
||||
|
||||
f.a0("using System;");
|
||||
f.a0("using System.Text.Json;");
|
||||
f.a0("using System.Text.Json.Serialization;");
|
||||
f.a0("using System.Text.Json.Nodes;");
|
||||
f.a0("using System.Threading.Tasks;");
|
||||
f.a0("using System.Collections.Generic;");
|
||||
|
||||
f.a0("");
|
||||
f.a0(format!("namespace {};", self.namespace));
|
||||
f.a0("");
|
||||
|
||||
f.a0(format!(
|
||||
"public abstract class {}Server<TContext> : JRpcService<TContext> {{",
|
||||
definition.name
|
||||
));
|
||||
f.a1("public override string Name {");
|
||||
f.a2("get {");
|
||||
f.a3(format!("return \"{}\";", definition.name));
|
||||
f.a2("}");
|
||||
f.a1("}");
|
||||
|
||||
f.a1(format!("public {}Server() {{", definition.name));
|
||||
for method in &definition.methods {
|
||||
f.a2(format!("this.RegisterFunction(\"{}\");", method.name));
|
||||
}
|
||||
f.a1("}");
|
||||
f.a0("");
|
||||
|
||||
for fnc in &definition.methods {
|
||||
let mut args = fnc
|
||||
.inputs
|
||||
.iter()
|
||||
.map(|inp| {
|
||||
format!(
|
||||
"{} {}",
|
||||
Self::type_to_csharp_ext(&inp.typ),
|
||||
Self::fix_keyword_name(&inp.name)
|
||||
)
|
||||
})
|
||||
.collect::<Vec<String>>();
|
||||
args.push("TContext ctx".to_string());
|
||||
let args = args.join(",");
|
||||
|
||||
if let Some(output) = &fnc.output {
|
||||
if output.typ.0 == BaseType::Void {
|
||||
f.a1(format!("public abstract Task {}({});", fnc.name, args));
|
||||
} else {
|
||||
f.a1(format!(
|
||||
"public abstract Task<{}> {}({});",
|
||||
Self::type_to_csharp_ext(&output.typ),
|
||||
fnc.name,
|
||||
args
|
||||
));
|
||||
}
|
||||
} else {
|
||||
f.a1(format!("public abstract void {}({});", fnc.name, args));
|
||||
}
|
||||
}
|
||||
|
||||
f.a0("");
|
||||
f.a1("public async override Task<JsonNode?> HandleRequest(string func, JsonNode param, TContext context) {");
|
||||
f.a2("switch(func) {");
|
||||
for fnc in &definition.methods {
|
||||
f.a3(format!("case \"{}\": {{", fnc.name));
|
||||
f.a4("if(param is JsonObject) {");
|
||||
let args_array = fnc
|
||||
.inputs
|
||||
.iter()
|
||||
.map(|inp| format!("param[\"{}\"]", inp.name))
|
||||
.collect::<Vec<String>>();
|
||||
f.a5(format!(
|
||||
"var ja = new JsonArray({});",
|
||||
args_array.join(", ")
|
||||
));
|
||||
f.a5("param = ja;");
|
||||
f.a4("}");
|
||||
|
||||
let pref = if let Some(output) = &fnc.output {
|
||||
if output.typ.0 == BaseType::Void {
|
||||
"await"
|
||||
} else {
|
||||
"var result = await"
|
||||
}
|
||||
} else {
|
||||
""
|
||||
};
|
||||
|
||||
let mut args_array = fnc
|
||||
.inputs
|
||||
.iter()
|
||||
.map(|inp| {
|
||||
format!(
|
||||
"param[\"{}\"]!.Deserialize<{}>()",
|
||||
inp.name,
|
||||
Self::type_to_csharp_ext(&inp.typ)
|
||||
)
|
||||
})
|
||||
.collect::<Vec<String>>();
|
||||
args_array.push("context".to_string());
|
||||
let args_array = args_array.join(", ");
|
||||
f.a4(format!("{} this.{}({});", pref, fnc.name, args_array));
|
||||
if let Some(output) = &fnc.output {
|
||||
if output.typ.0 == BaseType::Void {
|
||||
f.a4("return null;");
|
||||
} else {
|
||||
f.a4("return JsonSerializer.SerializeToNode(result);");
|
||||
}
|
||||
} else {
|
||||
f.a4("return null;");
|
||||
};
|
||||
f.a3("}");
|
||||
}
|
||||
f.a3("default: {");
|
||||
f.a4("throw new Exception(\"Invalid Method!\");");
|
||||
f.a3("}");
|
||||
f.a2("}");
|
||||
f.a1("}");
|
||||
|
||||
f.a0("}");
|
||||
|
||||
ctx.write_file(&format!("{}Server.cs", &definition.name), &f.into_content())?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn generate_service_client(
|
||||
&mut self,
|
||||
ctx: &mut CompileContext,
|
||||
definition: &ServiceDefinition,
|
||||
) -> anyhow::Result<()> {
|
||||
let mut f = FileGenerator::new();
|
||||
|
||||
f.a0("using System;");
|
||||
f.a0("using System.Text.Json;");
|
||||
f.a0("using System.Text.Json.Serialization;");
|
||||
f.a0("using System.Text.Json.Nodes;");
|
||||
f.a0("using System.Threading.Tasks;");
|
||||
f.a0("using System.Collections.Generic;");
|
||||
|
||||
f.a0("");
|
||||
f.a0(format!("namespace {};", self.namespace));
|
||||
f.a0("");
|
||||
|
||||
f.a0(format!("public class {}Client {{", definition.name));
|
||||
f.a1("private JRpcClient Client;");
|
||||
f.a1(format!(
|
||||
"public {}Client(JRpcClient client) {{",
|
||||
definition.name
|
||||
));
|
||||
f.a2("this.Client = client;");
|
||||
f.a1("}");
|
||||
f.a0("");
|
||||
for fnc in &definition.methods {
|
||||
let args = fnc
|
||||
.inputs
|
||||
.iter()
|
||||
.map(|inp| {
|
||||
format!(
|
||||
"{} {}",
|
||||
Self::type_to_csharp_ext(&inp.typ),
|
||||
Self::fix_keyword_name(&inp.name)
|
||||
)
|
||||
})
|
||||
.collect::<Vec<String>>()
|
||||
.join(", ");
|
||||
|
||||
let args_code = format!(
|
||||
"var param = new JsonArray({});",
|
||||
fnc.inputs
|
||||
.iter()
|
||||
.map(|inp| {
|
||||
format!(
|
||||
"JsonSerializer.SerializeToNode({})",
|
||||
Self::fix_keyword_name(&inp.name)
|
||||
)
|
||||
})
|
||||
.collect::<Vec<String>>()
|
||||
.join(", ")
|
||||
);
|
||||
|
||||
if let Some(output) = &fnc.output {
|
||||
if output.typ.0 == BaseType::Void {
|
||||
f.a1(format!("public async Task {}({}) {{", fnc.name, args));
|
||||
f.a2(args_code);
|
||||
f.a2(format!(
|
||||
"await Client.SendRequestRaw(\"{}.{}\", param);",
|
||||
definition.name, fnc.name,
|
||||
));
|
||||
f.a1("}");
|
||||
} else {
|
||||
f.a1(format!(
|
||||
"public async Task<{}> {}({}) {{",
|
||||
Self::type_to_csharp_ext(&output.typ),
|
||||
fnc.name,
|
||||
args
|
||||
));
|
||||
f.a2(args_code);
|
||||
f.a2(format!(
|
||||
"return await Client.SendRequest<{}>(\"{}.{}\", param);",
|
||||
Self::type_to_csharp_ext(&output.typ),
|
||||
definition.name,
|
||||
fnc.name,
|
||||
));
|
||||
f.a1("}");
|
||||
}
|
||||
} else {
|
||||
f.a1(format!("public void {}({}) {{", fnc.name, args));
|
||||
f.a2(args_code);
|
||||
f.a2(format!(
|
||||
"Client.SendNotification(\"{}.{}\", param);",
|
||||
definition.name, fnc.name,
|
||||
));
|
||||
f.a1("}");
|
||||
}
|
||||
}
|
||||
|
||||
f.a0("}");
|
||||
|
||||
ctx.write_file(&format!("{}Client.cs", &definition.name), &f.into_content())?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Compile for CSharpCompiler {
|
||||
fn new(options: &BTreeMap<String, String>) -> anyhow::Result<Self> {
|
||||
let namespace = if let Some(namespace) = options.get("csharp_namespace") {
|
||||
namespace.to_string()
|
||||
} else {
|
||||
"JRPC".to_string()
|
||||
};
|
||||
|
||||
if let Some(allow_bytes) = options.get("allow_bytes") {
|
||||
if allow_bytes == "true" {
|
||||
anyhow::bail!("allow_bytes option is not supported for csharp compiler");
|
||||
}
|
||||
}
|
||||
|
||||
Ok(CSharpCompiler { namespace })
|
||||
}
|
||||
|
||||
fn name(&self) -> String {
|
||||
"csharp".to_string()
|
||||
}
|
||||
|
||||
fn start(&mut self, ctx: &mut CompileContext) -> anyhow::Result<()> {
|
||||
let fix_ns = |input: &str| input.replace("__NAMESPACE__", &self.namespace);
|
||||
|
||||
ctx.write_file(
|
||||
&format!("{}.csproj", self.namespace),
|
||||
&fix_ns(include_str!("../../templates/CSharp/CSharp.csproj")),
|
||||
)?;
|
||||
|
||||
ctx.write_file(
|
||||
"JRpcClient.cs",
|
||||
&fix_ns(include_str!("../../templates/CSharp/JRpcClient.cs")),
|
||||
)?;
|
||||
|
||||
ctx.write_file(
|
||||
"JRpcServer.cs",
|
||||
&fix_ns(include_str!("../../templates/CSharp/JRpcServer.cs")),
|
||||
)?;
|
||||
|
||||
ctx.write_file(
|
||||
"JRpcTransport.cs",
|
||||
&fix_ns(include_str!("../../templates/CSharp/JRpcTransport.cs")),
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn generate_type(
|
||||
&mut self,
|
||||
ctx: &mut CompileContext,
|
||||
definition: &TypeDefinition,
|
||||
) -> anyhow::Result<()> {
|
||||
let mut f = FileGenerator::new();
|
||||
|
||||
f.a0("using System.Text.Json;");
|
||||
f.a0("using System.Text.Json.Serialization;");
|
||||
f.a0("using System.Collections.Generic;");
|
||||
f.a0("");
|
||||
f.a0(format!("namespace {};", self.namespace));
|
||||
f.a0("");
|
||||
f.a0(format!("public class {} {{", definition.name));
|
||||
|
||||
for field in &definition.fields {
|
||||
f.a1(format!("[JsonPropertyName(\"{}\")]", field.name));
|
||||
|
||||
f.a1(format!(
|
||||
"public {} {} {{ get; set; }}",
|
||||
Self::type_to_csharp_ext(&field.typ),
|
||||
Self::fix_keyword_name(&field.name)
|
||||
));
|
||||
}
|
||||
|
||||
f.a0("}");
|
||||
|
||||
ctx.write_file(&format!("{}.cs", &definition.name), &f.into_content())?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn generate_enum(
|
||||
&mut self,
|
||||
ctx: &mut CompileContext,
|
||||
definition: &EnumDefinition,
|
||||
) -> anyhow::Result<()> {
|
||||
let mut f = FileGenerator::new();
|
||||
|
||||
f.a0("using System.Text.Json;");
|
||||
f.a0("using System.Text.Json.Serialization;");
|
||||
f.a0("");
|
||||
f.a0(format!("namespace {};", self.namespace));
|
||||
f.a0("");
|
||||
|
||||
f.a0(format!("public enum {} {{", definition.name));
|
||||
|
||||
for variant in &definition.values {
|
||||
f.a1(format!("{} = {},", variant.name, variant.value));
|
||||
}
|
||||
|
||||
f.a0("}");
|
||||
|
||||
ctx.write_file(&format!("{}.cs", &definition.name), &f.into_content())?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn generate_service(
|
||||
&mut self,
|
||||
ctx: &mut CompileContext,
|
||||
definition: &ServiceDefinition,
|
||||
) -> anyhow::Result<()> {
|
||||
self.generate_service_client(ctx, definition)?;
|
||||
self.generate_service_server(ctx, definition)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn finalize(&mut self, ctx: &mut CompileContext, ir: &IR) -> anyhow::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ use std::{
|
||||
};
|
||||
|
||||
use anyhow::Result;
|
||||
use log::debug;
|
||||
|
||||
use crate::{
|
||||
compile::{Compile, CompileContext},
|
||||
@@ -11,6 +12,7 @@ use crate::{
|
||||
IR,
|
||||
};
|
||||
|
||||
pub mod csharp;
|
||||
pub mod rust;
|
||||
pub mod typescript;
|
||||
|
||||
@@ -50,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) => {
|
||||
@@ -58,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) => {
|
||||
@@ -66,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) => {
|
||||
|
||||
@@ -63,7 +63,7 @@ impl RustCompiler {
|
||||
|
||||
fn fix_keyword_name(name: &str) -> String {
|
||||
if RUST_KEYWORDS.contains(&name) {
|
||||
format!("{}_", name)
|
||||
format!("r#{}", name)
|
||||
} else {
|
||||
name.to_string()
|
||||
}
|
||||
@@ -146,7 +146,7 @@ impl RustCompiler {
|
||||
|
||||
f.a0("#[async_trait]");
|
||||
f.a0(format!("pub trait {} {{", definition.name));
|
||||
f.a1("type Context: Clone + Sync + Send + 'static;");
|
||||
f.a1("type Context: Sync + Send;");
|
||||
for method in definition.methods.iter() {
|
||||
let mut params = method
|
||||
.inputs
|
||||
@@ -159,7 +159,7 @@ impl RustCompiler {
|
||||
)
|
||||
})
|
||||
.collect::<Vec<String>>();
|
||||
params.push("ctx: Self::Context".to_string());
|
||||
params.push("ctx: &Self::Context".to_string());
|
||||
let params = params.join(", ");
|
||||
|
||||
let ret = method
|
||||
@@ -190,7 +190,7 @@ impl RustCompiler {
|
||||
f.a0("");
|
||||
|
||||
f.a0(format!(
|
||||
"impl<Context: Clone + Sync + Send + 'static> {}Handler<Context> {{",
|
||||
"impl<Context: Sync + Send> {}Handler<Context> {{",
|
||||
definition.name
|
||||
));
|
||||
f.a1(format!(
|
||||
@@ -205,7 +205,7 @@ impl RustCompiler {
|
||||
|
||||
f.a0("#[async_trait]");
|
||||
f.a0(format!(
|
||||
"impl<Context: Clone + Sync + Send + 'static> JRPCServerService for {}Handler<Context> {{",
|
||||
"impl<Context: Sync + Send> JRPCServerService for {}Handler<Context> {{",
|
||||
definition.name
|
||||
));
|
||||
f.a1("type Context = Context;");
|
||||
@@ -218,7 +218,7 @@ impl RustCompiler {
|
||||
|
||||
f.a1("#[allow(non_snake_case)]");
|
||||
f.a1(
|
||||
"async fn handle(&self, msg: &JRPCRequest, function: &str, ctx: Self::Context) -> Result<(bool, Value)> {",
|
||||
"async fn handle(&self, msg: &JRPCRequest, function: &str, ctx: &Self::Context) -> Result<(bool, Value)> {",
|
||||
);
|
||||
f.a2("match function {");
|
||||
|
||||
@@ -241,6 +241,9 @@ impl RustCompiler {
|
||||
f.a5(
|
||||
"let arr = msg.params.as_array().unwrap(); //TODO: Check if this can fail.",
|
||||
);
|
||||
f.a5(format!("if arr.len() != {} {{", method.inputs.len()));
|
||||
f.a6("return Err(\"Invalid number of arguments!\".into())");
|
||||
f.a5("}");
|
||||
}
|
||||
f.a5(format!("let res = self.implementation.{}(", method.name));
|
||||
for (i, arg) in method.inputs.iter().enumerate() {
|
||||
@@ -463,16 +466,26 @@ 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)]");
|
||||
|
||||
if Keywords::is_keyword(&field.name) {
|
||||
warn!(
|
||||
"[RUST] Warning: Field name '{}' is not allowed in Rust. Renaming to '{}_'",
|
||||
field.name, field.name
|
||||
);
|
||||
// warn!(
|
||||
// "[RUST] Warning: Field name '{}' is not allowed in Rust. Renaming to '{}_'",
|
||||
// field.name, field.name
|
||||
// );
|
||||
|
||||
f.a(1, format!("#[serde(rename = \"{}\")]", field.name));
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
})
|
||||
|
||||
@@ -6,7 +6,7 @@ edition = "2021"
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
int-enum = { version = "0.5", features = ["serde", "convert"] }
|
||||
int-enum = { version = "1.2.0" }
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
nanoid = "0.4"
|
||||
|
||||
@@ -112,19 +112,27 @@ pub trait JRPCServerService: Send + Sync {
|
||||
&self,
|
||||
request: &JRPCRequest,
|
||||
function: &str,
|
||||
ctx: Self::Context,
|
||||
ctx: &Self::Context,
|
||||
) -> Result<(bool, Value)>;
|
||||
}
|
||||
|
||||
pub type JRPCServiceHandle<Context> = Arc<dyn JRPCServerService<Context = Context>>;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct JRPCSession<Context> {
|
||||
server: JRPCServer<Context>,
|
||||
message_sender: Sender<JRPCResult>,
|
||||
}
|
||||
|
||||
impl<Context: Clone + Send + Sync + 'static> JRPCSession<Context> {
|
||||
impl<Context> Clone for JRPCSession<Context> {
|
||||
fn clone(&self) -> Self {
|
||||
JRPCSession {
|
||||
server: self.server.clone(),
|
||||
message_sender: self.message_sender.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<Context: Send + Sync + 'static> JRPCSession<Context> {
|
||||
pub fn new(server: JRPCServer<Context>, sender: Sender<JRPCResult>) -> Self {
|
||||
JRPCSession {
|
||||
server,
|
||||
@@ -157,6 +165,12 @@ impl<Context: Clone + Send + Sync + 'static> JRPCSession<Context> {
|
||||
pub fn handle_request(&self, request: JRPCRequest, ctx: Context) -> () {
|
||||
let session = self.clone();
|
||||
tokio::task::spawn(async move {
|
||||
let context = ctx;
|
||||
session.handle_request_awaiting(request, &context).await;
|
||||
});
|
||||
}
|
||||
|
||||
pub async fn handle_request_awaiting(&self, request: JRPCRequest, ctx: &Context) -> () {
|
||||
info!("Received request: {}", request.method);
|
||||
trace!("Request data: {:?}", request);
|
||||
let method: Vec<&str> = request.method.split('.').collect();
|
||||
@@ -167,13 +181,13 @@ impl<Context: Clone + Send + Sync + 'static> JRPCSession<Context> {
|
||||
let service = method[0];
|
||||
let function = method[1];
|
||||
|
||||
let service = session.server.services.get(service);
|
||||
let service = self.server.services.get(service);
|
||||
if let Some(service) = service {
|
||||
let result = service.handle(&request, function, ctx).await;
|
||||
match result {
|
||||
Ok((is_send, result)) => {
|
||||
if is_send && request.id.is_some() {
|
||||
let result = session
|
||||
let result = self
|
||||
.message_sender
|
||||
.send(JRPCResult {
|
||||
jsonrpc: "2.0".to_string(),
|
||||
@@ -189,32 +203,31 @@ impl<Context: Clone + Send + Sync + 'static> JRPCSession<Context> {
|
||||
}
|
||||
Err(err) => {
|
||||
warn!("Error while handling request: {}", err);
|
||||
session
|
||||
.send_error(
|
||||
request,
|
||||
format!("Error while handling request: {}", err),
|
||||
1,
|
||||
)
|
||||
self.send_error(request, format!("Error while handling request: {}", err), 1)
|
||||
.await;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
warn!("Service not found: {}", method[0]);
|
||||
session
|
||||
.send_error(request, "Service not found".to_string(), 1)
|
||||
self.send_error(request, "Service not found".to_string(), 1)
|
||||
.await;
|
||||
return;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct JRPCServer<Context> {
|
||||
services: HashMap<String, JRPCServiceHandle<Context>>,
|
||||
}
|
||||
|
||||
impl<Context: Clone + Send + Sync + 'static> JRPCServer<Context> {
|
||||
impl<Context> Clone for JRPCServer<Context> {
|
||||
fn clone(&self) -> Self {
|
||||
JRPCServer {
|
||||
services: self.services.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<Context: Send + Sync + 'static> JRPCServer<Context> {
|
||||
pub fn new() -> Self {
|
||||
JRPCServer {
|
||||
services: HashMap::new(),
|
||||
|
||||
@@ -63,7 +63,10 @@ export class ServiceProvider {
|
||||
let resListener = this.requests.get(msg.id);
|
||||
if (!resListener) return; // Ignore wrong responses
|
||||
if (msg.error) {
|
||||
if (msg.error.data && msg.error.data.$ == "verification_error") {
|
||||
if (
|
||||
msg.error.data &&
|
||||
msg.error.data.$ == "verification_error"
|
||||
) {
|
||||
resListener.err(
|
||||
new VerificationError(
|
||||
msg.error.data.type,
|
||||
@@ -112,14 +115,26 @@ export const getRandomBytes = (
|
||||
return function (n: number) {
|
||||
var a = new Uint8Array(n);
|
||||
for (var i = 0; i < n; i += QUOTA) {
|
||||
crypto.getRandomValues(a.subarray(i, i + Math.min(n - i, QUOTA)));
|
||||
crypto.getRandomValues(
|
||||
a.subarray(i, i + Math.min(n - i, QUOTA)),
|
||||
);
|
||||
}
|
||||
return a;
|
||||
};
|
||||
}
|
||||
: function () {
|
||||
// Node
|
||||
if (typeof require !== "undefined") {
|
||||
return require("crypto").randomBytes;
|
||||
} else {
|
||||
return (n: number) => {
|
||||
let a = new Uint8Array(n);
|
||||
for (let i = 0; i < n; i++) {
|
||||
a[i] = Math.floor(Math.random() * 256);
|
||||
}
|
||||
return a;
|
||||
};
|
||||
}
|
||||
}
|
||||
)() as (cnt: number) => Uint8Array;
|
||||
|
||||
|
||||
@@ -2,8 +2,9 @@ use anyhow::Result;
|
||||
use clap::{Parser, Subcommand};
|
||||
use libjrpc::{
|
||||
targets::{
|
||||
csharp::CSharpCompiler,
|
||||
rust::RustCompiler,
|
||||
typescript::{Node, TypeScriptCompiler},
|
||||
typescript::{Node, TypeScriptCompiler, ESM},
|
||||
},
|
||||
FileProcessor,
|
||||
};
|
||||
@@ -65,8 +66,9 @@ pub fn main() -> Result<()> {
|
||||
libjrpc::targets::compile::<TypeScriptCompiler<Node>>(ir, output_dir)?
|
||||
}
|
||||
"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)?,
|
||||
_ => {
|
||||
println!("Unsupported target: {}", output_target);
|
||||
}
|
||||
@@ -83,6 +85,7 @@ pub fn main() -> Result<()> {
|
||||
println!("rust");
|
||||
println!("ts-node");
|
||||
println!("ts-esm");
|
||||
println!("csharp")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
100
src/test.rs
100
src/test.rs
@@ -6,12 +6,12 @@ use std::{
|
||||
|
||||
#[test]
|
||||
fn compare_tools() {
|
||||
let targets = vec!["rust"];
|
||||
let targets = vec!["js-esm", "rust"];
|
||||
for target in targets {
|
||||
std::fs::remove_dir_all("./tests").unwrap();
|
||||
std::fs::create_dir_all("./tests").unwrap();
|
||||
|
||||
Command::new("cargo")
|
||||
let result1 = Command::new("cargo")
|
||||
.arg("run")
|
||||
.arg("--")
|
||||
.arg("compile")
|
||||
@@ -26,52 +26,60 @@ fn compare_tools() {
|
||||
.wait()
|
||||
.unwrap();
|
||||
|
||||
Command::new("node")
|
||||
.arg("JsonRPC/lib/jrpc.js")
|
||||
.arg("compile")
|
||||
.arg("--verbose")
|
||||
.arg("examples/test.jrpc")
|
||||
.arg("-o")
|
||||
.arg(target.to_string() + ":tests/js")
|
||||
.stdout(Stdio::null())
|
||||
.stderr(Stdio::null())
|
||||
.spawn()
|
||||
.expect("Failed to spawn process")
|
||||
.wait()
|
||||
.unwrap();
|
||||
|
||||
let rust_files = walkdir::WalkDir::new("tests/rust")
|
||||
.into_iter()
|
||||
.map(|e| e.unwrap())
|
||||
.filter(|e| e.file_type().is_file())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let js_files = walkdir::WalkDir::new("tests/js")
|
||||
.into_iter()
|
||||
.map(|e| e.unwrap())
|
||||
.filter(|e| e.file_type().is_file())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
if rust_files.len() != js_files.len() {
|
||||
panic!("Number of files mismatch");
|
||||
if !result1.success() {
|
||||
panic!("Failed to generate Rust code");
|
||||
}
|
||||
|
||||
for (rust_file, js_file) in rust_files.iter().zip(js_files.iter()) {
|
||||
println!("Testing files {:?} {:?}", rust_file.path(), js_file.path());
|
||||
let mut rust_str = String::new();
|
||||
File::open(rust_file.path())
|
||||
.unwrap()
|
||||
.read_to_string(&mut rust_str)
|
||||
.unwrap();
|
||||
let mut js_str = String::new();
|
||||
File::open(js_file.path())
|
||||
.unwrap()
|
||||
.read_to_string(&mut js_str)
|
||||
.unwrap();
|
||||
// let result2 = Command::new("node")
|
||||
// .arg("JsonRPC/lib/jrpc.js")
|
||||
// .arg("compile")
|
||||
// .arg("--verbose")
|
||||
// .arg("examples/test.jrpc")
|
||||
// .arg("-o")
|
||||
// .arg(target.to_string() + ":tests/js")
|
||||
// .stdout(Stdio::null())
|
||||
// .stderr(Stdio::null())
|
||||
// .spawn()
|
||||
// .expect("Failed to spawn process")
|
||||
// .wait()
|
||||
// .unwrap();
|
||||
|
||||
if rust_str != js_str {
|
||||
panic!("Files are different!")
|
||||
}
|
||||
}
|
||||
// if !result2.success() {
|
||||
// panic!("Failed to generate JavaScript code");
|
||||
// }
|
||||
|
||||
// let rust_files = walkdir::WalkDir::new("tests/rust")
|
||||
// .into_iter()
|
||||
// .map(|e| e.unwrap())
|
||||
// .filter(|e| e.file_type().is_file())
|
||||
// .collect::<Vec<_>>();
|
||||
|
||||
// let js_files = walkdir::WalkDir::new("tests/js")
|
||||
// .into_iter()
|
||||
// .map(|e| e.unwrap())
|
||||
// .filter(|e| e.file_type().is_file())
|
||||
// .collect::<Vec<_>>();
|
||||
|
||||
// if rust_files.len() != js_files.len() {
|
||||
// panic!("Number of files mismatch");
|
||||
// }
|
||||
|
||||
// for (rust_file, js_file) in rust_files.iter().zip(js_files.iter()) {
|
||||
// println!("Testing files {:?} {:?}", rust_file.path(), js_file.path());
|
||||
// let mut rust_str = String::new();
|
||||
// File::open(rust_file.path())
|
||||
// .unwrap()
|
||||
// .read_to_string(&mut rust_str)
|
||||
// .unwrap();
|
||||
// let mut js_str = String::new();
|
||||
// File::open(js_file.path())
|
||||
// .unwrap()
|
||||
// .read_to_string(&mut js_str)
|
||||
// .unwrap();
|
||||
|
||||
// if rust_str != js_str {
|
||||
// panic!("Files are different!")
|
||||
// }
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user