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", "name": "@hibas123/jrpcgen",
"version": "1.2.2", "version": "1.2.3",
"main": "lib/index.js", "main": "lib/index.js",
"license": "MIT", "license": "MIT",
"packageManager": "yarn@3.1.1", "packageManager": "yarn@3.1.1",

View File

@ -18,7 +18,7 @@ import { ZIGTarget } from "./targets/zig";
import { DartTarget } from "./targets/dart"; import { DartTarget } from "./targets/dart";
import { URL } from "url"; import { URL } from "url";
class CatchedError extends Error {} class CatchedError extends Error { }
const log = dbg("app"); const log = dbg("app");

View File

@ -1,6 +1,6 @@
import chalk from "chalk"; import chalk from "chalk";
import { CompileTarget } from "../compile"; import { CompileTarget } from "../compile";
import { TypeDefinition, EnumDefinition, ServiceDefinition, Step } from "../ir"; import { TypeDefinition, EnumDefinition, ServiceDefinition, Step, IR } from "../ir";
import { lineAppender, LineAppender } from "../utils"; import { lineAppender, LineAppender } from "../utils";
const conversion = { const conversion = {
@ -72,17 +72,28 @@ export class RustTarget extends CompileTarget<{ rust_crate: string }> {
a(0, `pub struct ${definition.name} {`); a(0, `pub struct ${definition.name} {`);
for (const field of definition.fields) { for (const field of definition.fields) {
a(1, `#[allow(non_snake_case)]`); 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) { if (field.array) {
a(1, `pub ${field.name}: Vec<${toRustType(field.type)}>,`); a(1, `${fn} Vec<${toRustType(field.type)}>,`);
} else if (field.map) { } else if (field.map) {
a( a(
1, 1,
`pub ${field.name}: HashMap<${toRustType( `${fn} HashMap<${toRustType(
field.map field.map
)}, ${toRustType(field.type)}>,` )}, ${toRustType(field.type)}>,`
); );
} else { } else {
a(1, `pub ${field.name}: ${toRustType(field.type)},`); a(1, `${fn} ${toRustType(field.type)},`);
} }
} }
a(0, `}`); a(0, `}`);
@ -219,6 +230,7 @@ export class RustTarget extends CompileTarget<{ rust_crate: string }> {
a(0, `}`); a(0, `}`);
a(0, ``); a(0, ``);
a(0, `impl ${definition.name}Handler {`); 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( a(
1, 1,
`pub fn new(implementation: Box<dyn ${definition.name} + Sync + Send + 'static>) -> Arc<Self> {` `pub fn new(implementation: Box<dyn ${definition.name} + Sync + Send + 'static>) -> Arc<Self> {`