fix rust invalid field names

This commit is contained in:
Fabian Stamm 2023-01-02 14:40:45 +01:00
parent a291851b5a
commit 6e947bde57
3 changed files with 18 additions and 6 deletions

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

@ -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> {`