rust2.0 #1

Merged
hibas123 merged 13 commits from rust2.0 into main 2024-08-13 10:00:18 +00:00
3 changed files with 18 additions and 6 deletions
Showing only changes of commit 6e947bde57 - Show all commits

View File

@ -1,6 +1,6 @@
{
"name": "@hibas123/jrpcgen",
"version": "1.2.2",
"version": "1.2.3",
"main": "lib/index.js",
"license": "MIT",
"packageManager": "yarn@3.1.1",

View File

@ -1,6 +1,6 @@
import chalk from "chalk";
import { CompileTarget } from "../compile";
import { TypeDefinition, EnumDefinition, ServiceDefinition, Step } from "../ir";
import { TypeDefinition, EnumDefinition, ServiceDefinition, Step, IR } from "../ir";
import { lineAppender, LineAppender } from "../utils";
const conversion = {
@ -72,17 +72,28 @@ export class RustTarget extends CompileTarget<{ rust_crate: string }> {
a(0, `pub struct ${definition.name} {`);
for (const field of definition.fields) {
a(1, `#[allow(non_snake_case)]`);
let fn = `pub ${field.name}:`;
if (field.name == "type") {
// TODO: Add other keywords as well!
console.log(
chalk.yellow("[RUST] WARNING:"),
"Field name 'type' is not allowed in Rust. Renaming to 'type_'"
);
fn = `pub type_:`;
a(1, `#[serde(rename = "type")]`);
}
if (field.array) {
a(1, `pub ${field.name}: Vec<${toRustType(field.type)}>,`);
a(1, `${fn} Vec<${toRustType(field.type)}>,`);
} else if (field.map) {
a(
1,
`pub ${field.name}: HashMap<${toRustType(
`${fn} HashMap<${toRustType(
field.map
)}, ${toRustType(field.type)}>,`
);
} else {
a(1, `pub ${field.name}: ${toRustType(field.type)},`);
a(1, `${fn} ${toRustType(field.type)},`);
}
}
a(0, `}`);
@ -219,6 +230,7 @@ export class RustTarget extends CompileTarget<{ rust_crate: string }> {
a(0, `}`);
a(0, ``);
a(0, `impl ${definition.name}Handler {`);
//TODO: Maybe add a new definition like, pub fn new2<T>(implementation: T) where T: ${definition.name} + Sync + Send + 'static {}
a(
1,
`pub fn new(implementation: Box<dyn ${definition.name} + Sync + Send + 'static>) -> Arc<Self> {`