From 6e947bde579189207eba85f9a632a40d1257f7d6 Mon Sep 17 00:00:00 2001 From: Fabian Stamm Date: Mon, 2 Jan 2023 14:40:45 +0100 Subject: [PATCH] fix rust invalid field names --- package.json | 2 +- src/process.ts | 2 +- src/targets/rust.ts | 20 ++++++++++++++++---- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 1017fe8..64fc477 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/process.ts b/src/process.ts index e5f1949..7d22a5b 100644 --- a/src/process.ts +++ b/src/process.ts @@ -18,7 +18,7 @@ import { ZIGTarget } from "./targets/zig"; import { DartTarget } from "./targets/dart"; import { URL } from "url"; -class CatchedError extends Error {} +class CatchedError extends Error { } const log = dbg("app"); diff --git a/src/targets/rust.ts b/src/targets/rust.ts index 29f3bc1..e8348af 100644 --- a/src/targets/rust.ts +++ b/src/targets/rust.ts @@ -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(implementation: T) where T: ${definition.name} + Sync + Send + 'static {} a( 1, `pub fn new(implementation: Box) -> Arc {`