Start implementing Rust
This commit is contained in:
137
lib/jrpc.js
137
lib/jrpc.js
@ -1656,7 +1656,7 @@ var require_route = __commonJS({
|
||||
}
|
||||
module2.exports = function(fromModel) {
|
||||
const graph = deriveBFS(fromModel);
|
||||
const conversion3 = {};
|
||||
const conversion4 = {};
|
||||
const models = Object.keys(graph);
|
||||
for (let len = models.length, i = 0; i < len; i++) {
|
||||
const toModel = models[i];
|
||||
@ -1664,9 +1664,9 @@ var require_route = __commonJS({
|
||||
if (node.parent === null) {
|
||||
continue;
|
||||
}
|
||||
conversion3[toModel] = wrapConversion(toModel, graph);
|
||||
conversion4[toModel] = wrapConversion(toModel, graph);
|
||||
}
|
||||
return conversion3;
|
||||
return conversion4;
|
||||
};
|
||||
}
|
||||
});
|
||||
@ -9876,7 +9876,7 @@ function parse(tokens, file) {
|
||||
// src/ir.ts
|
||||
var import_debug = __toESM(require_src());
|
||||
var log = (0, import_debug.default)("app");
|
||||
var BUILTIN = ["number", "string", "boolean"];
|
||||
var BUILTIN = ["float", "int", "string", "boolean"];
|
||||
var IRError = class extends Error {
|
||||
constructor(statement, message) {
|
||||
super("Error building IR: " + message);
|
||||
@ -9912,7 +9912,7 @@ function get_ir(parsed) {
|
||||
if (depends.indexOf(field.fieldtype) < 0)
|
||||
depends.push(field.fieldtype);
|
||||
}
|
||||
if (field.map && field.map !== "number" && field.map !== "string") {
|
||||
if (field.map && field.map !== "int" && field.map !== "string") {
|
||||
throw new IRError(field, `Type ${field.map} is not valid as map key!`);
|
||||
}
|
||||
return {
|
||||
@ -10047,6 +10047,7 @@ function get_ir(parsed) {
|
||||
} else if (statement.type == "define") {
|
||||
options[statement.key] = statement.value;
|
||||
if ((statement.key == "use_messagepack" || statement.key == "allow_bytes") && statement.value == "true") {
|
||||
options["allow_bytes"] = true;
|
||||
builtin.push("bytes");
|
||||
}
|
||||
} else {
|
||||
@ -10074,10 +10075,14 @@ var CompileTarget = class {
|
||||
}
|
||||
}
|
||||
writeFile(name, content) {
|
||||
let resPath = Path.join(this.outputFolder, name);
|
||||
let resDir = Path.dirname(resPath);
|
||||
if (!FS.existsSync(resDir))
|
||||
FS.mkdirSync(resDir, { recursive: true });
|
||||
if (content instanceof Promise) {
|
||||
content.then((res) => FS.writeFileSync(Path.join(this.outputFolder, name), res));
|
||||
content.then((res) => FS.writeFileSync(resPath, res));
|
||||
} else {
|
||||
FS.writeFileSync(Path.join(this.outputFolder, name), content);
|
||||
FS.writeFileSync(resPath, content);
|
||||
}
|
||||
}
|
||||
getTemplate(name) {
|
||||
@ -10120,7 +10125,8 @@ function compile(ir, target) {
|
||||
// src/targets/typescript.ts
|
||||
var conversion = {
|
||||
boolean: "boolean",
|
||||
number: "number",
|
||||
int: "number",
|
||||
float: "number",
|
||||
string: "string",
|
||||
void: "void",
|
||||
bytes: "Uint8Array"
|
||||
@ -10139,7 +10145,7 @@ var TypescriptTarget = class extends CompileTarget {
|
||||
`;
|
||||
}
|
||||
generateImports(a, def) {
|
||||
a(0, this.generateImport(`{ VerificationError, apply_number, apply_string, apply_boolean, apply_void }`, `./ts_base`));
|
||||
a(0, this.generateImport(`{ VerificationError, apply_int, apply_float, apply_string, apply_boolean, apply_void }`, `./ts_base`));
|
||||
a(0, def.depends.map((dep) => this.generateImport(`${dep}, { apply_${dep} }`, "./" + dep)));
|
||||
}
|
||||
getFileName(typename) {
|
||||
@ -10440,7 +10446,8 @@ var NodeJSTypescriptTarget = class extends TypescriptTarget {
|
||||
// src/targets/csharp.ts
|
||||
var conversion2 = {
|
||||
boolean: "bool",
|
||||
number: "double",
|
||||
int: "long",
|
||||
float: "double",
|
||||
string: "string",
|
||||
void: "void",
|
||||
bytes: ""
|
||||
@ -10454,8 +10461,8 @@ var CSharpTarget = class extends CompileTarget {
|
||||
return this.options.csharp_namespace || "JRPC";
|
||||
}
|
||||
start() {
|
||||
if (this.options.use_messagepack == true) {
|
||||
throw new Error("C# has no support for MessagePack yet!");
|
||||
if (this.options.allow_bytes == true) {
|
||||
throw new Error("C# has no support for 'bytes' yet!");
|
||||
}
|
||||
this.writeFile(this.namespace + ".csproj", this.getTemplate("CSharp/CSharp.csproj"));
|
||||
const fixNS = (input) => input.replace("__NAMESPACE__", this.namespace);
|
||||
@ -10670,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
|
||||
var CatchedError = class extends Error {
|
||||
};
|
||||
@ -10678,6 +10790,7 @@ var Targets = /* @__PURE__ */ new Map();
|
||||
Targets.set("ts-esm", ESMTypescriptTarget);
|
||||
Targets.set("ts-node", NodeJSTypescriptTarget);
|
||||
Targets.set("c#", CSharpTarget);
|
||||
Targets.set("rust", RustTarget);
|
||||
function indexToLineAndCol(src, index) {
|
||||
let line = 1;
|
||||
let col = 1;
|
||||
|
Reference in New Issue
Block a user