Change the context API to make Context-Ownership more flexible

This commit is contained in:
Fabian Stamm
2026-01-09 15:58:32 +01:00
parent 4c7084563f
commit c29dafb042
7 changed files with 1294 additions and 617 deletions

View File

@@ -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() {
@@ -479,10 +482,10 @@ impl Compile for RustCompiler {
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));
}