Add C# implementation

This commit is contained in:
Fabian Stamm
2025-07-31 23:24:31 +02:00
parent 369ccbe84e
commit b069237b91
6 changed files with 418 additions and 9 deletions

View 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(())
}
}

View File

@ -11,6 +11,7 @@ use crate::{
IR,
};
pub mod csharp;
pub mod rust;
pub mod typescript;