Merge branch 'rust-target' of https://gitlab.hibas.dev/hibas123/JsonRPC into rust-target

This commit is contained in:
Fabian Stamm 2022-04-13 19:02:30 +00:00
commit 42ef89f32b

View File

@ -1656,7 +1656,7 @@ var require_route = __commonJS({
} }
module2.exports = function(fromModel) { module2.exports = function(fromModel) {
const graph = deriveBFS(fromModel); const graph = deriveBFS(fromModel);
const conversion3 = {}; const conversion4 = {};
const models = Object.keys(graph); const models = Object.keys(graph);
for (let len = models.length, i = 0; i < len; i++) { for (let len = models.length, i = 0; i < len; i++) {
const toModel = models[i]; const toModel = models[i];
@ -1664,9 +1664,9 @@ var require_route = __commonJS({
if (node.parent === null) { if (node.parent === null) {
continue; continue;
} }
conversion3[toModel] = wrapConversion(toModel, graph); conversion4[toModel] = wrapConversion(toModel, graph);
} }
return conversion3; return conversion4;
}; };
} }
}); });
@ -10677,6 +10677,111 @@ var CSharpTarget = class extends CompileTarget {
} }
}; };
// src/targets/rust.ts
var conversion3 = {
boolean: "bool",
float: "f64",
int: "i64",
string: "String",
void: "void",
bytes: "Vec<u8>"
};
function toRustType(type) {
return conversion3[type] || type;
}
function toSnake(input) {
return input[0].toLowerCase() + input.slice(1).replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`);
}
var RustTarget = class extends CompileTarget {
name = "rust";
get crate() {
return this.options.rust_crate;
}
start() {
if (!this.crate)
throw new Error("Setting a crate name is required. Add the following to your jrpc file: 'define rust_crate <name>'");
if (this.options.allow_bytes == true) {
throw new Error("Rust has no support for 'bytes' yet!");
}
this.writeFile("Cargo.toml", this.getTemplate("Rust/Cargo.toml").replace("${NAME}", this.crate));
}
addDependencies(a, def) {
for (const dep of def.depends) {
a(0, `use crate::${dep};`);
}
a(0, ``);
a(0, ``);
}
generateType(definition) {
let lines = [];
const a = (i, t) => {
if (!Array.isArray(t)) {
t = [t];
}
t.forEach((l) => lines.push(" ".repeat(i) + l.trim()));
};
if (definition.fields.find((e) => e.map))
a(0, `use std::collections::hash_map::HashMap;`);
a(0, `use serde::{Deserialize, Serialize};`);
this.addDependencies(a, definition);
a(0, `#[derive(Clone, Debug, Serialize, Deserialize)]`);
a(0, `pub struct ${definition.name} {`);
for (const field of definition.fields) {
if (field.array) {
a(1, `pub ${field.name}: Vec<${toRustType(field.type)}>,`);
} else if (field.map) {
a(1, `pub ${field.name}: HashMap<${toRustType(field.map)}, ${toRustType(field.type)}>,`);
} else {
a(1, `pub ${field.name}: ${toRustType(field.type)},`);
}
}
a(0, `}`);
this.writeFile(`src/${toSnake(definition.name)}.rs`, lines.join("\n"));
}
generateEnum(definition) {
let lines = [];
const a = (i, t) => {
if (!Array.isArray(t)) {
t = [t];
}
t.forEach((l) => lines.push(" ".repeat(i) + l.trim()));
};
a(0, `use int_enum::IntEnum;`);
a(0, `use serde::{Deserialize, Serialize};`);
a(0, ``);
a(0, ``);
a(0, `#[repr(i64)]`);
a(0, "#[derive(Clone, Copy, Debug, Eq, PartialEq, IntEnum, Deserialize, Serialize)]");
a(0, `pub enum ${definition.name} {`);
for (const field of definition.values) {
a(1, `${field.name} = ${field.value},`);
}
a(0, `}`);
this.writeFile(`src/${toSnake(definition.name)}.rs`, lines.join("\n"));
}
generateService(definition) {
}
generateLib(steps) {
let lines = [];
const a = (i, t) => {
if (!Array.isArray(t)) {
t = [t];
}
t.forEach((l) => lines.push(" ".repeat(i) + l.trim()));
};
for (const [typ, def] of steps) {
if (typ == "type" || typ == "enum") {
a(0, `mod ${toSnake(def.name)};`);
a(0, `pub use ${toSnake(def.name)}::${def.name};`);
}
}
this.writeFile(`src/lib.rs`, lines.join("\n"));
}
finalize(steps) {
this.generateLib(steps);
}
};
// src/process.ts // src/process.ts
var CatchedError = class extends Error { var CatchedError = class extends Error {
}; };
@ -10685,6 +10790,7 @@ var Targets = /* @__PURE__ */ new Map();
Targets.set("ts-esm", ESMTypescriptTarget); Targets.set("ts-esm", ESMTypescriptTarget);
Targets.set("ts-node", NodeJSTypescriptTarget); Targets.set("ts-node", NodeJSTypescriptTarget);
Targets.set("c#", CSharpTarget); Targets.set("c#", CSharpTarget);
Targets.set("rust", RustTarget);
function indexToLineAndCol(src, index) { function indexToLineAndCol(src, index) {
let line = 1; let line = 1;
let col = 1; let col = 1;