Compare commits
	
		
			7 Commits
		
	
	
		
			58e00a9ca3
			...
			rust-targe
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| a2224bbf95 | |||
| 42ef89f32b | |||
| f4a761bf55 | |||
| b980af4b17 | |||
| 7b6ef3231f | |||
| 339d3006d6 | |||
| 97ce0ea9b5 | 
							
								
								
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							@ -6,5 +6,6 @@ examples/CSharp/Generated
 | 
			
		||||
examples/CSharp/Example/bin
 | 
			
		||||
examples/CSharp/Example/obj
 | 
			
		||||
examples/definition.json
 | 
			
		||||
examples/Rust/Generated
 | 
			
		||||
templates/CSharp/bin
 | 
			
		||||
templates/CSharp/obj
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										42
									
								
								.vscode/tasks.json
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								.vscode/tasks.json
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1,42 @@
 | 
			
		||||
{
 | 
			
		||||
    "version": "2.0.0",
 | 
			
		||||
    "tasks": [
 | 
			
		||||
        {
 | 
			
		||||
            "label": "build",
 | 
			
		||||
            "command": "dotnet",
 | 
			
		||||
            "type": "process",
 | 
			
		||||
            "args": [
 | 
			
		||||
                "build",
 | 
			
		||||
                "${workspaceFolder}/examples/CSharp/Example/CSharp_Example.csproj",
 | 
			
		||||
                "/property:GenerateFullPaths=true",
 | 
			
		||||
                "/consoleloggerparameters:NoSummary"
 | 
			
		||||
            ],
 | 
			
		||||
            "problemMatcher": "$msCompile"
 | 
			
		||||
        },
 | 
			
		||||
        {
 | 
			
		||||
            "label": "publish",
 | 
			
		||||
            "command": "dotnet",
 | 
			
		||||
            "type": "process",
 | 
			
		||||
            "args": [
 | 
			
		||||
                "publish",
 | 
			
		||||
                "${workspaceFolder}/examples/CSharp/Example/CSharp_Example.csproj",
 | 
			
		||||
                "/property:GenerateFullPaths=true",
 | 
			
		||||
                "/consoleloggerparameters:NoSummary"
 | 
			
		||||
            ],
 | 
			
		||||
            "problemMatcher": "$msCompile"
 | 
			
		||||
        },
 | 
			
		||||
        {
 | 
			
		||||
            "label": "watch",
 | 
			
		||||
            "command": "dotnet",
 | 
			
		||||
            "type": "process",
 | 
			
		||||
            "args": [
 | 
			
		||||
                "watch",
 | 
			
		||||
                "run",
 | 
			
		||||
                "${workspaceFolder}/examples/CSharp/Example/CSharp_Example.csproj",
 | 
			
		||||
                "/property:GenerateFullPaths=true",
 | 
			
		||||
                "/consoleloggerparameters:NoSummary"
 | 
			
		||||
            ],
 | 
			
		||||
            "problemMatcher": "$msCompile"
 | 
			
		||||
        }
 | 
			
		||||
    ]
 | 
			
		||||
}
 | 
			
		||||
@ -1,6 +1,7 @@
 | 
			
		||||
import "./import";
 | 
			
		||||
 | 
			
		||||
define csharp_namespace Example;
 | 
			
		||||
define rust_crate example;
 | 
			
		||||
 | 
			
		||||
enum TestEnum {
 | 
			
		||||
   VAL1,
 | 
			
		||||
@ -14,17 +15,17 @@ type Test {
 | 
			
		||||
   atom: TestAtom;
 | 
			
		||||
   array: TestAtom[];
 | 
			
		||||
   enumValue: TestEnum;
 | 
			
		||||
   map: {number, TestAtom};
 | 
			
		||||
   map: {int, TestAtom};
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
type AddValueRequest {
 | 
			
		||||
   value1: number;
 | 
			
		||||
   value2: number;
 | 
			
		||||
   value1: float;
 | 
			
		||||
   value2: float;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type AddValueResponse {
 | 
			
		||||
   value: number;
 | 
			
		||||
   value: float;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
service TestService {
 | 
			
		||||
@ -36,11 +37,11 @@ service TestService {
 | 
			
		||||
   @Description("Add two numbers")
 | 
			
		||||
   @Param("value1", "The first value")
 | 
			
		||||
   @Param("value2", "The second value")
 | 
			
		||||
   AddValuesMultipleParams(value1: number, value2: number): number;
 | 
			
		||||
   AddValuesMultipleParams(value1: float, value2: float): float;
 | 
			
		||||
 | 
			
		||||
   @Description("Does literaly nothing")
 | 
			
		||||
   @Param("param1", "Some number")
 | 
			
		||||
   ReturningVoid(param1: number): void;
 | 
			
		||||
   ReturningVoid(param1: float): void;
 | 
			
		||||
 | 
			
		||||
   @Description("Just sends an Event with a String")
 | 
			
		||||
   @Param("param1", "Parameter with some string for event")
 | 
			
		||||
@ -49,5 +50,5 @@ service TestService {
 | 
			
		||||
   ThrowingError(): void;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
   FunctionWithArrayAsParamAndReturn(values1: number[], values2: number[]): number[];
 | 
			
		||||
   FunctionWithArrayAsParamAndReturn(values1: float[], values2: float[]): float[];
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -1,5 +1,5 @@
 | 
			
		||||
type TestAtom {
 | 
			
		||||
   val_number: number;
 | 
			
		||||
   val_number: float;
 | 
			
		||||
   val_boolean: boolean;
 | 
			
		||||
   val_string: string;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										142
									
								
								lib/jrpc.js
									
									
									
									
									
								
							
							
						
						
									
										142
									
								
								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 {
 | 
			
		||||
@ -10046,7 +10046,8 @@ function get_ir(parsed) {
 | 
			
		||||
      ]);
 | 
			
		||||
    } else if (statement.type == "define") {
 | 
			
		||||
      options[statement.key] = statement.value;
 | 
			
		||||
      if (statement.key == "use_messagepack" && statement.value == "true") {
 | 
			
		||||
      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) {
 | 
			
		||||
@ -10260,7 +10266,6 @@ var TypescriptTarget = class extends CompileTarget {
 | 
			
		||||
      const params = fnc.inputs.map((e) => `${e.name}: ${toJSType(e.type) + (e.array ? "[]" : "")}`).join(", ");
 | 
			
		||||
      if (!fnc.return) {
 | 
			
		||||
        a(1, `${fnc.name}(${params}): void {`);
 | 
			
		||||
        1;
 | 
			
		||||
        a(2, `this._provider.sendMessage({`);
 | 
			
		||||
        a(3, `jsonrpc: "2.0",`);
 | 
			
		||||
        a(3, `method: "${def.name}.${fnc.name}",`);
 | 
			
		||||
@ -10334,6 +10339,8 @@ var TypescriptTarget = class extends CompileTarget {
 | 
			
		||||
      a(2, `let p: any[] = [];`);
 | 
			
		||||
      a(2, `if(Array.isArray(params)){`);
 | 
			
		||||
      a(3, `p = params;`);
 | 
			
		||||
      a(3, `while(p.length < ${fnc.inputs.length})`);
 | 
			
		||||
      a(4, `p.push(undefined)`);
 | 
			
		||||
      a(2, `} else {`);
 | 
			
		||||
      for (const param of fnc.inputs) {
 | 
			
		||||
        a(3, `p.push(params["${param.name}"])`);
 | 
			
		||||
@ -10439,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: ""
 | 
			
		||||
@ -10453,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);
 | 
			
		||||
@ -10669,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 {
 | 
			
		||||
};
 | 
			
		||||
@ -10677,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;
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										12
									
								
								meta.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								meta.json
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,12 @@
 | 
			
		||||
{
 | 
			
		||||
   "name": "jsonrpc",
 | 
			
		||||
   "version": "0.0.1",
 | 
			
		||||
   "description": "",
 | 
			
		||||
   "author": "Fabian Stamm <dev@fabianstamm.de>",
 | 
			
		||||
   "contributors": [],
 | 
			
		||||
   "files": [
 | 
			
		||||
      "**/*.ts",
 | 
			
		||||
      "**/*.js",
 | 
			
		||||
      "README.md"
 | 
			
		||||
   ]
 | 
			
		||||
}
 | 
			
		||||
@ -1,13 +1,14 @@
 | 
			
		||||
{
 | 
			
		||||
   "name": "@hibas123/jrpcgen",
 | 
			
		||||
   "version": "1.0.29",
 | 
			
		||||
   "version": "1.1.1",
 | 
			
		||||
   "main": "lib/index.js",
 | 
			
		||||
   "license": "MIT",
 | 
			
		||||
   "packageManager": "yarn@3.1.1",
 | 
			
		||||
   "scripts": {
 | 
			
		||||
      "start": "ts-node src/index.ts",
 | 
			
		||||
      "test-start": "npm run start -- compile examples/example.jrpc --definition=examples/definition.json -o=ts-node:examples/Typescript/out -o=c#:examples/CSharp/Generated",
 | 
			
		||||
      "test-start": "npm run start -- compile examples/example.jrpc --definition=examples/definition.json -o=ts-node:examples/Typescript/out -o=c#:examples/CSharp/Generated -o=rust:examples/Rust/Generated",
 | 
			
		||||
      "test-csharp": "cd examples/CSharp/Example/ && dotnet run",
 | 
			
		||||
      "test-rust": "cd examples/Rust/Generated/ && cargo build",
 | 
			
		||||
      "test-typescript": "ts-node examples/test.ts",
 | 
			
		||||
      "test": "npm run test-start && npm run test-csharp && npm run test-typescript",
 | 
			
		||||
      "build": "esbuild src/index.ts --bundle --platform=node --target=node14 --outfile=lib/jrpc.js",
 | 
			
		||||
@ -19,7 +20,7 @@
 | 
			
		||||
   "files": [
 | 
			
		||||
      "lib/jrpc.js",
 | 
			
		||||
      "templates/**",
 | 
			
		||||
      "examples/**",
 | 
			
		||||
      "examples/*.jrpc",
 | 
			
		||||
      "src/**",
 | 
			
		||||
      "tsconfig.json"
 | 
			
		||||
   ],
 | 
			
		||||
 | 
			
		||||
@ -1,5 +1,5 @@
 | 
			
		||||
import * as FS from "fs";
 | 
			
		||||
import * as FSE from "fs-extra"
 | 
			
		||||
import * as FSE from "fs-extra";
 | 
			
		||||
import * as Path from "path";
 | 
			
		||||
import {
 | 
			
		||||
   EnumDefinition,
 | 
			
		||||
@ -11,7 +11,10 @@ import {
 | 
			
		||||
 | 
			
		||||
export abstract class CompileTarget<T = any> {
 | 
			
		||||
   abstract name: string;
 | 
			
		||||
   constructor(private outputFolder: string, protected options: T & { use_messagepack: boolean }) {
 | 
			
		||||
   constructor(
 | 
			
		||||
      private outputFolder: string,
 | 
			
		||||
      protected options: T & { allow_bytes: boolean }
 | 
			
		||||
   ) {
 | 
			
		||||
      if (!FS.existsSync(outputFolder)) {
 | 
			
		||||
         FS.mkdirSync(outputFolder, {
 | 
			
		||||
            recursive: true,
 | 
			
		||||
@ -30,12 +33,13 @@ export abstract class CompileTarget<T = any> {
 | 
			
		||||
   abstract finalize(steps: Step[]): void;
 | 
			
		||||
 | 
			
		||||
   protected writeFile(name: string, content: string | Promise<string>) {
 | 
			
		||||
      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);
 | 
			
		||||
      }
 | 
			
		||||
   }
 | 
			
		||||
 | 
			
		||||
@ -59,12 +63,10 @@ export abstract class CompileTarget<T = any> {
 | 
			
		||||
      return res.join("\n");
 | 
			
		||||
   }
 | 
			
		||||
 | 
			
		||||
   protected loadTemplateFolder(name:string) {
 | 
			
		||||
   protected loadTemplateFolder(name: string) {
 | 
			
		||||
      let root = Path.join(__dirname, "../templates/", name);
 | 
			
		||||
      
 | 
			
		||||
      FSE.copySync(root, this.outputFolder, {
 | 
			
		||||
 | 
			
		||||
      });
 | 
			
		||||
      FSE.copySync(root, this.outputFolder, {});
 | 
			
		||||
   }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										117
									
								
								src/ir.ts
									
									
									
									
									
								
							
							
						
						
									
										117
									
								
								src/ir.ts
									
									
									
									
									
								
							@ -2,7 +2,7 @@ import type { Parsed, StatementNode } from "./parser";
 | 
			
		||||
import dbg from "debug";
 | 
			
		||||
const log = dbg("app");
 | 
			
		||||
 | 
			
		||||
const BUILTIN = ["number", "string", "boolean"];
 | 
			
		||||
const BUILTIN = ["float", "int", "string", "boolean"];
 | 
			
		||||
 | 
			
		||||
export class IRError extends Error {
 | 
			
		||||
   constructor(public statement: StatementNode, message: string) {
 | 
			
		||||
@ -36,17 +36,17 @@ export interface EnumDefinition {
 | 
			
		||||
export type ServiceFunctionDecorators = {
 | 
			
		||||
   description: string;
 | 
			
		||||
   parameters: {
 | 
			
		||||
      name:string;
 | 
			
		||||
      name: string;
 | 
			
		||||
      description: string;
 | 
			
		||||
   }[];
 | 
			
		||||
   returns: string;
 | 
			
		||||
}
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
export interface ServiceFunctionParamsDefinition {
 | 
			
		||||
   name: string;
 | 
			
		||||
   inputs: { type: string; name: string, array: boolean }[];
 | 
			
		||||
   return: { type: string, array: boolean } | undefined;
 | 
			
		||||
   decorators: ServiceFunctionDecorators
 | 
			
		||||
   inputs: { type: string; name: string; array: boolean }[];
 | 
			
		||||
   return: { type: string; array: boolean } | undefined;
 | 
			
		||||
   decorators: ServiceFunctionDecorators;
 | 
			
		||||
}
 | 
			
		||||
export type ServiceFunctionDefinition = ServiceFunctionParamsDefinition;
 | 
			
		||||
 | 
			
		||||
@ -62,8 +62,8 @@ export type Step = [
 | 
			
		||||
];
 | 
			
		||||
 | 
			
		||||
export type IR = {
 | 
			
		||||
   options: { [key:string]: string},
 | 
			
		||||
   steps: Step[]
 | 
			
		||||
   options: { [key: string]: string };
 | 
			
		||||
   steps: Step[];
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
export default function get_ir(parsed: Parsed): IR {
 | 
			
		||||
@ -101,7 +101,10 @@ export default function get_ir(parsed: Parsed): IR {
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (defined.indexOf(field.fieldtype) < 0) {
 | 
			
		||||
               if (builtin.indexOf(field.fieldtype) < 0 && field.fieldtype !== statement.name) {
 | 
			
		||||
               if (
 | 
			
		||||
                  builtin.indexOf(field.fieldtype) < 0 &&
 | 
			
		||||
                  field.fieldtype !== statement.name
 | 
			
		||||
               ) {
 | 
			
		||||
                  throw new IRError(
 | 
			
		||||
                     field,
 | 
			
		||||
                     `Type ${field.fieldtype} is not defined!`
 | 
			
		||||
@ -112,11 +115,7 @@ export default function get_ir(parsed: Parsed): IR {
 | 
			
		||||
                  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!`
 | 
			
		||||
@ -200,7 +199,10 @@ export default function get_ir(parsed: Parsed): IR {
 | 
			
		||||
                  if (!depends.some((a) => a === fnc.return_type.type))
 | 
			
		||||
                     depends.push(fnc.return_type.type);
 | 
			
		||||
               } else {
 | 
			
		||||
                  if (fnc.return_type.type !== "void" && builtin.indexOf(fnc.return_type.type) < 0) {
 | 
			
		||||
                  if (
 | 
			
		||||
                     fnc.return_type.type !== "void" &&
 | 
			
		||||
                     builtin.indexOf(fnc.return_type.type) < 0
 | 
			
		||||
                  ) {
 | 
			
		||||
                     throw new IRError(
 | 
			
		||||
                        fnc,
 | 
			
		||||
                        `Type ${fnc.return_type.type} is not defined`
 | 
			
		||||
@ -225,51 +227,73 @@ export default function get_ir(parsed: Parsed): IR {
 | 
			
		||||
 | 
			
		||||
            let decorators = {} as ServiceFunctionDecorators;
 | 
			
		||||
 | 
			
		||||
            fnc.decorators.forEach((values, key)=>{
 | 
			
		||||
               for(const val of values) {
 | 
			
		||||
                  switch(key) {
 | 
			
		||||
            fnc.decorators.forEach((values, key) => {
 | 
			
		||||
               for (const val of values) {
 | 
			
		||||
                  switch (key) {
 | 
			
		||||
                     case "Description":
 | 
			
		||||
                        if(decorators.description)
 | 
			
		||||
                           throw new IRError(fnc, `Decorator 'Description' can only be used once!`);
 | 
			
		||||
                        if(val.length != 1)
 | 
			
		||||
                           throw new IRError(fnc, `Decorator 'Description' requires exactly one parameter!`);
 | 
			
		||||
                        if (decorators.description)
 | 
			
		||||
                           throw new IRError(
 | 
			
		||||
                              fnc,
 | 
			
		||||
                              `Decorator 'Description' can only be used once!`
 | 
			
		||||
                           );
 | 
			
		||||
                        if (val.length != 1)
 | 
			
		||||
                           throw new IRError(
 | 
			
		||||
                              fnc,
 | 
			
		||||
                              `Decorator 'Description' requires exactly one parameter!`
 | 
			
		||||
                           );
 | 
			
		||||
                        decorators.description = val[0];
 | 
			
		||||
                        break;
 | 
			
		||||
                     case "Returns":
 | 
			
		||||
                           if(decorators.returns)
 | 
			
		||||
                              throw new IRError(fnc, `Decorator 'Returns' can only be used once!`);
 | 
			
		||||
                           if(val.length != 1)
 | 
			
		||||
                              throw new IRError(fnc, `Decorator 'Returns' requires exactly one parameter!`);
 | 
			
		||||
                           decorators.returns = val[0];
 | 
			
		||||
                           break;
 | 
			
		||||
                        if (decorators.returns)
 | 
			
		||||
                           throw new IRError(
 | 
			
		||||
                              fnc,
 | 
			
		||||
                              `Decorator 'Returns' can only be used once!`
 | 
			
		||||
                           );
 | 
			
		||||
                        if (val.length != 1)
 | 
			
		||||
                           throw new IRError(
 | 
			
		||||
                              fnc,
 | 
			
		||||
                              `Decorator 'Returns' requires exactly one parameter!`
 | 
			
		||||
                           );
 | 
			
		||||
                        decorators.returns = val[0];
 | 
			
		||||
                        break;
 | 
			
		||||
                     case "Param":
 | 
			
		||||
                        if(!decorators.parameters)
 | 
			
		||||
                           decorators.parameters = [];
 | 
			
		||||
                        if(val.length != 2)
 | 
			
		||||
                           throw new IRError(fnc, `Decorator 'Param' requires exactly two parameters!`);
 | 
			
		||||
                        if (!decorators.parameters) decorators.parameters = [];
 | 
			
		||||
                        if (val.length != 2)
 | 
			
		||||
                           throw new IRError(
 | 
			
		||||
                              fnc,
 | 
			
		||||
                              `Decorator 'Param' requires exactly two parameters!`
 | 
			
		||||
                           );
 | 
			
		||||
                        const [name, description] = val;
 | 
			
		||||
                        if(!fnc.inputs.find(e=>e.name == name))
 | 
			
		||||
                           throw new IRError(fnc, `Decorator 'Param' requires the first param to equal the name of a function parameter!`);
 | 
			
		||||
                        if(decorators.parameters.find(e=>e.name == name))
 | 
			
		||||
                           throw new IRError(fnc, `Decorator 'Param' has already been set for the parameter ${name}!`);
 | 
			
		||||
                        if (!fnc.inputs.find((e) => e.name == name))
 | 
			
		||||
                           throw new IRError(
 | 
			
		||||
                              fnc,
 | 
			
		||||
                              `Decorator 'Param' requires the first param to equal the name of a function parameter!`
 | 
			
		||||
                           );
 | 
			
		||||
                        if (decorators.parameters.find((e) => e.name == name))
 | 
			
		||||
                           throw new IRError(
 | 
			
		||||
                              fnc,
 | 
			
		||||
                              `Decorator 'Param' has already been set for the parameter ${name}!`
 | 
			
		||||
                           );
 | 
			
		||||
 | 
			
		||||
                        decorators.parameters.push({
 | 
			
		||||
                           name,
 | 
			
		||||
                           description,
 | 
			
		||||
                        })
 | 
			
		||||
                        });
 | 
			
		||||
                        break;
 | 
			
		||||
                     default:
 | 
			
		||||
                        throw new IRError(fnc, `Decorator ${key} is not a valid decorator!`);
 | 
			
		||||
                        throw new IRError(
 | 
			
		||||
                           fnc,
 | 
			
		||||
                           `Decorator ${key} is not a valid decorator!`
 | 
			
		||||
                        );
 | 
			
		||||
                  }
 | 
			
		||||
               }
 | 
			
		||||
            })
 | 
			
		||||
 | 
			
		||||
            });
 | 
			
		||||
 | 
			
		||||
            return {
 | 
			
		||||
               name: fnc.name,
 | 
			
		||||
               inputs: fnc.inputs,
 | 
			
		||||
               return: fnc.return_type,
 | 
			
		||||
               decorators
 | 
			
		||||
               decorators,
 | 
			
		||||
            } as ServiceFunctionDefinition;
 | 
			
		||||
         });
 | 
			
		||||
 | 
			
		||||
@ -281,9 +305,14 @@ export default function get_ir(parsed: Parsed): IR {
 | 
			
		||||
               functions,
 | 
			
		||||
            } as ServiceDefinition,
 | 
			
		||||
         ]);
 | 
			
		||||
      } else if(statement.type == "define") {
 | 
			
		||||
      } else if (statement.type == "define") {
 | 
			
		||||
         options[statement.key] = statement.value;
 | 
			
		||||
         if(statement.key == "use_messagepack" && statement.value == "true") {
 | 
			
		||||
         if (
 | 
			
		||||
            (statement.key == "use_messagepack" ||
 | 
			
		||||
               statement.key == "allow_bytes") &&
 | 
			
		||||
            statement.value == "true"
 | 
			
		||||
         ) {
 | 
			
		||||
            options["allow_bytes"] = true;
 | 
			
		||||
            builtin.push("bytes");
 | 
			
		||||
         }
 | 
			
		||||
      } else {
 | 
			
		||||
@ -293,6 +322,6 @@ export default function get_ir(parsed: Parsed): IR {
 | 
			
		||||
 | 
			
		||||
   return {
 | 
			
		||||
      options,
 | 
			
		||||
      steps
 | 
			
		||||
      steps,
 | 
			
		||||
   };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -11,6 +11,7 @@ import {
 | 
			
		||||
   NodeJSTypescriptTarget,
 | 
			
		||||
} from "./targets/typescript";
 | 
			
		||||
import { CSharpTarget } from "./targets/csharp";
 | 
			
		||||
import { RustTarget } from "./targets/rust";
 | 
			
		||||
 | 
			
		||||
class CatchedError extends Error {}
 | 
			
		||||
 | 
			
		||||
@ -21,7 +22,7 @@ export const Targets = new Map<string, typeof CompileTarget>();
 | 
			
		||||
Targets.set("ts-esm", ESMTypescriptTarget);
 | 
			
		||||
Targets.set("ts-node", NodeJSTypescriptTarget);
 | 
			
		||||
Targets.set("c#", CSharpTarget as typeof CompileTarget);
 | 
			
		||||
 | 
			
		||||
Targets.set("rust", RustTarget as typeof CompileTarget);
 | 
			
		||||
 | 
			
		||||
function indexToLineAndCol(src: string, index: number) {
 | 
			
		||||
   let line = 1;
 | 
			
		||||
 | 
			
		||||
@ -12,10 +12,11 @@ type lineAppender = (ind: number, line: string | string[]) => void;
 | 
			
		||||
 | 
			
		||||
const conversion = {
 | 
			
		||||
   boolean: "bool",
 | 
			
		||||
   number: "double",
 | 
			
		||||
   int: "long",
 | 
			
		||||
   float: "double",
 | 
			
		||||
   string: "string",
 | 
			
		||||
   void: "void",
 | 
			
		||||
   bytes: ""
 | 
			
		||||
   bytes: "",
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
function toCSharpType(type: string): string {
 | 
			
		||||
@ -30,8 +31,8 @@ export class CSharpTarget extends CompileTarget<{ csharp_namespace: string }> {
 | 
			
		||||
   }
 | 
			
		||||
 | 
			
		||||
   start(): void {
 | 
			
		||||
      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",
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										241
									
								
								src/targets/rust.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										241
									
								
								src/targets/rust.ts
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,241 @@
 | 
			
		||||
import { CompileTarget } from "../compile";
 | 
			
		||||
import { TypeDefinition, EnumDefinition, ServiceDefinition, Step } from "../ir";
 | 
			
		||||
 | 
			
		||||
type lineAppender = (ind: number, line: string | string[]) => void;
 | 
			
		||||
 | 
			
		||||
const conversion = {
 | 
			
		||||
   boolean: "bool",
 | 
			
		||||
   float: "f64",
 | 
			
		||||
   int: "i64",
 | 
			
		||||
   string: "String",
 | 
			
		||||
   void: "void",
 | 
			
		||||
   bytes: "Vec<u8>",
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
function toRustType(type: string): string {
 | 
			
		||||
   return (conversion as any)[type] || type;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function toSnake(input: string) {
 | 
			
		||||
   return (
 | 
			
		||||
      input[0].toLowerCase() +
 | 
			
		||||
      input.slice(1).replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`)
 | 
			
		||||
   );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export class RustTarget extends CompileTarget<{ rust_crate: string }> {
 | 
			
		||||
   name: string = "rust";
 | 
			
		||||
 | 
			
		||||
   get crate() {
 | 
			
		||||
      return this.options.rust_crate;
 | 
			
		||||
   }
 | 
			
		||||
 | 
			
		||||
   start(): void {
 | 
			
		||||
      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)
 | 
			
		||||
      );
 | 
			
		||||
   }
 | 
			
		||||
 | 
			
		||||
   private addDependencies(
 | 
			
		||||
      a: lineAppender,
 | 
			
		||||
      def: TypeDefinition | ServiceDefinition
 | 
			
		||||
   ) {
 | 
			
		||||
      for (const dep of def.depends) {
 | 
			
		||||
         a(0, `use crate::${dep};`);
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      a(0, ``);
 | 
			
		||||
      a(0, ``);
 | 
			
		||||
   }
 | 
			
		||||
 | 
			
		||||
   generateType(definition: TypeDefinition): void {
 | 
			
		||||
      let lines: string[] = [];
 | 
			
		||||
      const a: lineAppender = (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: EnumDefinition): void {
 | 
			
		||||
      let lines: string[] = [];
 | 
			
		||||
      const a: lineAppender = (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"));
 | 
			
		||||
   }
 | 
			
		||||
 | 
			
		||||
   private generateServiceReqResTypes(definition: ServiceDefinition) {
 | 
			
		||||
      let lines: string[] = [];
 | 
			
		||||
      const a: lineAppender = (i, t) => {
 | 
			
		||||
         if (!Array.isArray(t)) {
 | 
			
		||||
            t = [t];
 | 
			
		||||
         }
 | 
			
		||||
         t.forEach((l) => lines.push("   ".repeat(i) + l.trim()));
 | 
			
		||||
      };
 | 
			
		||||
 | 
			
		||||
      const paramToRust = (type: string, array: boolean) => {
 | 
			
		||||
         let rt = toRustType(type);
 | 
			
		||||
         return array ? `Vec<${rt}>` : rt;
 | 
			
		||||
      };
 | 
			
		||||
 | 
			
		||||
      this.addDependencies(a, definition);
 | 
			
		||||
      let baseName = definition.name;
 | 
			
		||||
      for (const fnc of definition.functions) {
 | 
			
		||||
         let name = definition.name + "_" + toSnake(fnc.name);
 | 
			
		||||
         a(
 | 
			
		||||
            0,
 | 
			
		||||
            `struct ${name}_Params(${fnc.inputs
 | 
			
		||||
               .map((i) => paramToRust(i.type, i.array))
 | 
			
		||||
               .join(",")});`
 | 
			
		||||
         );
 | 
			
		||||
         a(0, ``);
 | 
			
		||||
         a(0, ``);
 | 
			
		||||
         a(0, ``);
 | 
			
		||||
         a(0, ``);
 | 
			
		||||
         a(0, ``);
 | 
			
		||||
      }
 | 
			
		||||
      this.writeFile(
 | 
			
		||||
         `src/${toSnake(definition.name)}_types.rs`,
 | 
			
		||||
         lines.join("\n")
 | 
			
		||||
      );
 | 
			
		||||
   }
 | 
			
		||||
 | 
			
		||||
   private generateServiceClient(definition: ServiceDefinition): void {}
 | 
			
		||||
 | 
			
		||||
   private generateServiceServer(definition: ServiceDefinition): void {
 | 
			
		||||
      let lines: string[] = [];
 | 
			
		||||
      const a: lineAppender = (i, t) => {
 | 
			
		||||
         if (!Array.isArray(t)) {
 | 
			
		||||
            t = [t];
 | 
			
		||||
         }
 | 
			
		||||
         t.forEach((l) => lines.push("   ".repeat(i) + l.trim()));
 | 
			
		||||
      };
 | 
			
		||||
 | 
			
		||||
      this.addDependencies(a, definition);
 | 
			
		||||
 | 
			
		||||
      const retToRust = (type: string, array: boolean) => {
 | 
			
		||||
         let rt = toRustType(type);
 | 
			
		||||
         return array ? `Vec<${rt}>` : rt;
 | 
			
		||||
      };
 | 
			
		||||
 | 
			
		||||
      a(0, ``);
 | 
			
		||||
      a(0, `struct ${definition.name}ServiceHandler {`);
 | 
			
		||||
      a(1, `service: impl ${definition.name}`);
 | 
			
		||||
      a(1, ``);
 | 
			
		||||
      a(1, ``);
 | 
			
		||||
      a(0, `}`);
 | 
			
		||||
      a(0, ``);
 | 
			
		||||
      a(0, `impl ${definition.name}ServiceHandler {`);
 | 
			
		||||
      a(
 | 
			
		||||
         1,
 | 
			
		||||
         `fn new(service: impl ${definition.name}) -> ${definition.name}ServiceHandler {`
 | 
			
		||||
      );
 | 
			
		||||
      a(2, ``);
 | 
			
		||||
      a(2, ``);
 | 
			
		||||
      a(2, ``);
 | 
			
		||||
      a(1, `}`);
 | 
			
		||||
      a(0, `}`);
 | 
			
		||||
      a(0, ``);
 | 
			
		||||
      a(0, `trait ${definition.name} {`);
 | 
			
		||||
 | 
			
		||||
      for (const fnc of definition.functions) {
 | 
			
		||||
         let returnType = !fnc.return
 | 
			
		||||
            ? "()"
 | 
			
		||||
            : retToRust(fnc.return.type, fnc.return.array);
 | 
			
		||||
         a(1, `pub async fn ${fnc.name}(&self) -> Result<${returnType}>;`);
 | 
			
		||||
         // a(0, ``);
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      a(0, `}`);
 | 
			
		||||
      this.writeFile(
 | 
			
		||||
         `src/${toSnake(definition.name)}_types.rs`,
 | 
			
		||||
         lines.join("\n")
 | 
			
		||||
      );
 | 
			
		||||
   }
 | 
			
		||||
 | 
			
		||||
   generateService(definition: ServiceDefinition): void {
 | 
			
		||||
      this.generateServiceServer(definition);
 | 
			
		||||
      // throw new Error("Service not implemented.");
 | 
			
		||||
   }
 | 
			
		||||
 | 
			
		||||
   private generateLib(steps: Step[]) {
 | 
			
		||||
      let lines: string[] = [];
 | 
			
		||||
      const a: lineAppender = (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: Step[]): void {
 | 
			
		||||
      this.generateLib(steps);
 | 
			
		||||
      // throw new Error("Method not implemented.");
 | 
			
		||||
   }
 | 
			
		||||
}
 | 
			
		||||
@ -13,10 +13,11 @@ type lineAppender = (ind: number, line: string | string[]) => void;
 | 
			
		||||
 | 
			
		||||
const conversion = {
 | 
			
		||||
   boolean: "boolean",
 | 
			
		||||
   number: "number",
 | 
			
		||||
   int: "number",
 | 
			
		||||
   float: "number",
 | 
			
		||||
   string: "string",
 | 
			
		||||
   void: "void",
 | 
			
		||||
   bytes: "Uint8Array"
 | 
			
		||||
   bytes: "Uint8Array",
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
function toJSType(type: string): string {
 | 
			
		||||
@ -45,17 +46,14 @@ export class TypescriptTarget extends CompileTarget {
 | 
			
		||||
      a(
 | 
			
		||||
         0,
 | 
			
		||||
         this.generateImport(
 | 
			
		||||
            `{ VerificationError, apply_number, apply_string, apply_boolean, apply_void }`,
 | 
			
		||||
            `{ 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
 | 
			
		||||
            )
 | 
			
		||||
            this.generateImport(`${dep}, { apply_${dep} }`, "./" + dep)
 | 
			
		||||
         )
 | 
			
		||||
      );
 | 
			
		||||
   }
 | 
			
		||||
@ -128,17 +126,29 @@ export class TypescriptTarget extends CompileTarget {
 | 
			
		||||
         `export function apply_${def.name}(data: ${def.name}): ${def.name} {`
 | 
			
		||||
      );
 | 
			
		||||
      {
 | 
			
		||||
         a(1, `if(typeof data !== "object") throw new VerificationError("${def.name}", undefined, data);`)
 | 
			
		||||
         a(
 | 
			
		||||
            1,
 | 
			
		||||
            `if(typeof data !== "object") throw new VerificationError("${def.name}", undefined, data);`
 | 
			
		||||
         );
 | 
			
		||||
         a(1, `let res = new ${def.name}() as any;`);
 | 
			
		||||
         def.fields.forEach((field) => {
 | 
			
		||||
            a(1, `if(data["${field.name}"] !== null && data["${field.name}"] !== undefined) {`)
 | 
			
		||||
            a(
 | 
			
		||||
               1,
 | 
			
		||||
               `if(data["${field.name}"] !== null && data["${field.name}"] !== undefined) {`
 | 
			
		||||
            );
 | 
			
		||||
            if (field.array) {
 | 
			
		||||
               a(2, `if(!Array.isArray(data["${field.name}"])) throw new VerificationError("array", "${field.name}", data["${field.name}"]);`)
 | 
			
		||||
               a(
 | 
			
		||||
                  2,
 | 
			
		||||
                  `if(!Array.isArray(data["${field.name}"])) throw new VerificationError("array", "${field.name}", data["${field.name}"]);`
 | 
			
		||||
               );
 | 
			
		||||
               a(2, `res["${field.name}"] = data["${field.name}"].map(elm=>`);
 | 
			
		||||
               a(3, `apply_${field.type}(elm)`);
 | 
			
		||||
               a(2, `)`);
 | 
			
		||||
            } else if (field.map) {
 | 
			
		||||
               a(2, `if(typeof data["${field.name}"] !== "object") throw new VerificationError("map", "${field.name}", data["${field.name}"]);`)
 | 
			
		||||
               a(
 | 
			
		||||
                  2,
 | 
			
		||||
                  `if(typeof data["${field.name}"] !== "object") throw new VerificationError("map", "${field.name}", data["${field.name}"]);`
 | 
			
		||||
               );
 | 
			
		||||
               a(2, `res["${field.name}"] = {}`);
 | 
			
		||||
               a(
 | 
			
		||||
                  2,
 | 
			
		||||
@ -255,7 +265,7 @@ export class TypescriptTarget extends CompileTarget {
 | 
			
		||||
         // }
 | 
			
		||||
 | 
			
		||||
         if (!fnc.return) {
 | 
			
		||||
            a(1, `${fnc.name}(${params}): void {`);1
 | 
			
		||||
            a(1, `${fnc.name}(${params}): void {`);
 | 
			
		||||
            a(2, `this._provider.sendMessage({`);
 | 
			
		||||
            a(3, `jsonrpc: "2.0",`);
 | 
			
		||||
            a(3, `method: "${def.name}.${fnc.name}",`);
 | 
			
		||||
@ -361,6 +371,8 @@ export class TypescriptTarget extends CompileTarget {
 | 
			
		||||
         a(2, `let p: any[] = [];`);
 | 
			
		||||
         a(2, `if(Array.isArray(params)){`);
 | 
			
		||||
         a(3, `p = params;`);
 | 
			
		||||
         a(3, `while(p.length < ${fnc.inputs.length})`);
 | 
			
		||||
         a(4, `p.push(undefined)`);
 | 
			
		||||
         a(2, `} else {`);
 | 
			
		||||
         for (const param of fnc.inputs) {
 | 
			
		||||
            a(3, `p.push(params["${param.name}"])`);
 | 
			
		||||
@ -383,7 +395,10 @@ export class TypescriptTarget extends CompileTarget {
 | 
			
		||||
         a(2, ``);
 | 
			
		||||
         a(2, `p.push(ctx);`);
 | 
			
		||||
 | 
			
		||||
         a(2, `//@ts-ignore This will cause a typescript error when strict checking, since p is not a tuple`)
 | 
			
		||||
         a(
 | 
			
		||||
            2,
 | 
			
		||||
            `//@ts-ignore This will cause a typescript error when strict checking, since p is not a tuple`
 | 
			
		||||
         );
 | 
			
		||||
         a(
 | 
			
		||||
            2,
 | 
			
		||||
            `return this.${fnc.name}.call(this, ...p)` + //TODO: Refactor. This line is way to compicated for anyone to understand, including me
 | 
			
		||||
@ -466,10 +481,7 @@ export class TypescriptTarget extends CompileTarget {
 | 
			
		||||
                     "./" + def.name
 | 
			
		||||
                  )
 | 
			
		||||
               );
 | 
			
		||||
               a(
 | 
			
		||||
                  0,
 | 
			
		||||
                  `export { ${def.name}, apply_${def.name} }`
 | 
			
		||||
               );
 | 
			
		||||
               a(0, `export { ${def.name}, apply_${def.name} }`);
 | 
			
		||||
               a(0, ``);
 | 
			
		||||
               break;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										2
									
								
								templates/Rust/.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								templates/Rust/.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1,2 @@
 | 
			
		||||
target
 | 
			
		||||
Cargo.lock
 | 
			
		||||
							
								
								
									
										13
									
								
								templates/Rust/Cargo.toml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								templates/Rust/Cargo.toml
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,13 @@
 | 
			
		||||
[package]
 | 
			
		||||
name = "__name__"
 | 
			
		||||
version = "0.1.0"
 | 
			
		||||
edition = "2021"
 | 
			
		||||
 | 
			
		||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
 | 
			
		||||
 | 
			
		||||
[dependencies]
 | 
			
		||||
int-enum = "0.4.0"
 | 
			
		||||
tokio = { version = "1.17.0", features = ["full"] }
 | 
			
		||||
serde = { version = "1.0.136", features = ["derive"] }
 | 
			
		||||
serde_json = "1.0.79"
 | 
			
		||||
async-trait = "0.1.7"
 | 
			
		||||
							
								
								
									
										22
									
								
								templates/Rust/src/client.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								templates/Rust/src/client.rs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,22 @@
 | 
			
		||||
use std::future::Future;
 | 
			
		||||
use std::collections::HashMap;
 | 
			
		||||
use serde_json::Value;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
pub struct ServiceClientRequest {
 | 
			
		||||
   
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Future for ServiceClientRequest {
 | 
			
		||||
   type Output = ()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub struct ServiceClient {
 | 
			
		||||
   channel: Box<dny ComChannel>,
 | 
			
		||||
   requests: HashMap<String, Box<dyn >>
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub trait ServiceClient {
 | 
			
		||||
   
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										249
									
								
								templates/Rust/src/lib.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										249
									
								
								templates/Rust/src/lib.rs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,249 @@
 | 
			
		||||
mod service;
 | 
			
		||||
pub use service::{ComChannel, JRPCError, JRPCRequest, JRPCResult, Result};
 | 
			
		||||
 | 
			
		||||
use async_trait::async_trait;
 | 
			
		||||
use serde::{Deserialize, Serialize};
 | 
			
		||||
use serde_json::Value;
 | 
			
		||||
use std::boxed::Box;
 | 
			
		||||
use std::collections::HashMap;
 | 
			
		||||
use std::error::Error;
 | 
			
		||||
use std::marker::Send;
 | 
			
		||||
use std::sync::{Arc, Mutex};
 | 
			
		||||
 | 
			
		||||
// REGION: FIXED/STATIC Code
 | 
			
		||||
 | 
			
		||||
#[async_trait]
 | 
			
		||||
pub trait JRPCServiceHandler<C: Sync>: Send {
 | 
			
		||||
   fn get_name(&self) -> String;
 | 
			
		||||
   async fn on_message(&self, msg: JRPCRequest, function: String, ctx: &C)
 | 
			
		||||
      -> Result<(bool, Value)>;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type Shared<T> = Arc<Mutex<T>>;
 | 
			
		||||
type SharedHM<K, V> = Shared<HashMap<K, V>>;
 | 
			
		||||
type ServiceSharedHM<C: 'static + Sync + Send> = SharedHM<String, Box<dyn JRPCServiceHandler<C>>>;
 | 
			
		||||
 | 
			
		||||
pub struct JRPCServer<C: 'static + Sync + Send> {
 | 
			
		||||
   services: ServiceSharedHM<C>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl<C: 'static + Sync + Send> JRPCServer<C> {
 | 
			
		||||
   fn new() -> Self {
 | 
			
		||||
      return Self {
 | 
			
		||||
         services: Arc::new(Mutex::new(HashMap::new())),
 | 
			
		||||
      };
 | 
			
		||||
   }
 | 
			
		||||
 | 
			
		||||
   fn add_service(&mut self, service: Box<dyn JRPCServiceHandler<C>>) {
 | 
			
		||||
      let mut services = self.services.lock().unwrap();
 | 
			
		||||
      services.insert(service.get_name(), service);
 | 
			
		||||
   }
 | 
			
		||||
 | 
			
		||||
   fn start_session(&mut self, channel: Box<dyn ComChannel>, context: C) {
 | 
			
		||||
      JRPCSession::start(channel, context, self.services.clone());
 | 
			
		||||
   }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub struct JRPCSession<C: 'static + Sync + Send> {
 | 
			
		||||
   context: C,
 | 
			
		||||
   channel: Box<dyn ComChannel>,
 | 
			
		||||
   services: ServiceSharedHM<C>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
unsafe impl<C: 'static + Sync + Send> Sync for JRPCSession<C> {}
 | 
			
		||||
 | 
			
		||||
impl<C: 'static + Sync + Send> JRPCSession<C> {
 | 
			
		||||
   fn start(channel: Box<dyn ComChannel>, context: C, services: ServiceSharedHM<C>) {
 | 
			
		||||
      tokio::spawn(async move {
 | 
			
		||||
         // let res = Self {
 | 
			
		||||
         //    channel,
 | 
			
		||||
         //    context,
 | 
			
		||||
         //    services,
 | 
			
		||||
         // };
 | 
			
		||||
 | 
			
		||||
         // let ch = &channel;
 | 
			
		||||
 | 
			
		||||
         loop {
 | 
			
		||||
            let pkg = channel.read_packet().await;
 | 
			
		||||
 | 
			
		||||
            let data = match pkg {
 | 
			
		||||
               Err(_) => return,
 | 
			
		||||
               Ok(res) => res,
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            let req: Result<JRPCRequest> =
 | 
			
		||||
               serde_json::from_str(data.as_str()).map_err(|err| Box::from(err));
 | 
			
		||||
 | 
			
		||||
            let req = match req {
 | 
			
		||||
               Err(err) => {
 | 
			
		||||
                  continue;
 | 
			
		||||
               }
 | 
			
		||||
               Ok(parsed) => parsed,
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            let construct_err = |err: Box<dyn Error>, code: i64| {
 | 
			
		||||
               if let Some(id) = &req.id {
 | 
			
		||||
                  let error = JRPCError {
 | 
			
		||||
                     code,
 | 
			
		||||
                     message: err.to_string(),
 | 
			
		||||
                     data: Value::Null,
 | 
			
		||||
                  };
 | 
			
		||||
                  let r = JRPCResult {
 | 
			
		||||
                     jsonrpc: "2.0".to_owned(),
 | 
			
		||||
                     id: id.clone(),
 | 
			
		||||
                     result: Value::Null,
 | 
			
		||||
                     error: Option::from(error),
 | 
			
		||||
                  };
 | 
			
		||||
                  let s = serde_json::to_string(&r);
 | 
			
		||||
                  return Some(s.unwrap());
 | 
			
		||||
               }
 | 
			
		||||
               return None;
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            let parts: Vec<String> = req.method.split('.').map(|e| e.to_owned()).collect();
 | 
			
		||||
            if parts.len() != 2 {
 | 
			
		||||
               Self::send_err_res(req, &channel, &Box::from("Error".to_owned())).await;
 | 
			
		||||
               // let res = construct_err(Box::from("Method not found!".to_owned()), -32602);
 | 
			
		||||
               // if let Some(err) = res {
 | 
			
		||||
               //    channel.send_packet(err).await;
 | 
			
		||||
               // }
 | 
			
		||||
               continue;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            let service = parts[0];
 | 
			
		||||
            let name = parts[1];
 | 
			
		||||
 | 
			
		||||
            // JRPCSession::send_err_res(&res, &channel, "Error").await;
 | 
			
		||||
 | 
			
		||||
            // let pkg = self.channel.lock().unwrap().read_packet();
 | 
			
		||||
            // if !pkg {
 | 
			
		||||
            //    return Ok(());
 | 
			
		||||
            // }
 | 
			
		||||
            // // pkg behandelm
 | 
			
		||||
            // self.channel.send_packet(vec![0, 1, 2]);
 | 
			
		||||
         }
 | 
			
		||||
      });
 | 
			
		||||
   }
 | 
			
		||||
 | 
			
		||||
   async fn handle_packet(channel: &Box<dyn ComChannel>, context: C, services: ServiceSharedHM<C>) {
 | 
			
		||||
   }
 | 
			
		||||
   async fn send_err_res(req: JRPCRequest, channel: &Box<dyn ComChannel>, err: &Box<dyn Error>) {
 | 
			
		||||
      if let Some(id) = &req.id {
 | 
			
		||||
         let error = JRPCError {
 | 
			
		||||
            code: 0, //TODO: Make this better?
 | 
			
		||||
            message: err.to_string(),
 | 
			
		||||
            data: Value::Null,
 | 
			
		||||
         };
 | 
			
		||||
 | 
			
		||||
         let r = JRPCResult {
 | 
			
		||||
            jsonrpc: "2.0".to_owned(),
 | 
			
		||||
            id: id.clone(),
 | 
			
		||||
            result: Value::Null,
 | 
			
		||||
            error: Option::from(error),
 | 
			
		||||
         };
 | 
			
		||||
 | 
			
		||||
         let s = serde_json::to_string(&r);
 | 
			
		||||
 | 
			
		||||
         if s.is_ok() {
 | 
			
		||||
            channel.send_packet(s.unwrap()).await;
 | 
			
		||||
         }
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      return ();
 | 
			
		||||
   }
 | 
			
		||||
}
 | 
			
		||||
// REGION: Generated Code
 | 
			
		||||
 | 
			
		||||
#[derive(Deserialize, Serialize, Debug, Clone)]
 | 
			
		||||
struct Test {
 | 
			
		||||
   name: String,
 | 
			
		||||
   age: i64,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[async_trait]
 | 
			
		||||
trait TestService<C: 'static + Sync + Send> {
 | 
			
		||||
   async fn GetTest(&self, name: String, age: i64, context: &C) -> Result<Test>;
 | 
			
		||||
   async fn TestNot(&self, ctx: &C) -> Result<()>;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
struct TestServiceHandler<C: 'static + Sync + Send> {
 | 
			
		||||
   name: String,
 | 
			
		||||
   implementation: Box<dyn TestService<C> + Sync + Send>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl<C: 'static + Sync + Send> TestServiceHandler<C> {
 | 
			
		||||
   fn new(implementation: Box<dyn TestService<C> + Sync + Send>) -> Self {
 | 
			
		||||
      return Self {
 | 
			
		||||
         name: "TestService".to_owned(),
 | 
			
		||||
         implementation,
 | 
			
		||||
      };
 | 
			
		||||
   }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[async_trait]
 | 
			
		||||
impl<C: 'static + Sync + Send> JRPCServiceHandler<C> for TestServiceHandler<C> {
 | 
			
		||||
   fn get_name(&self) -> String {
 | 
			
		||||
      return "".to_owned();
 | 
			
		||||
   }
 | 
			
		||||
 | 
			
		||||
   async fn on_message(
 | 
			
		||||
      &self,
 | 
			
		||||
      msg: JRPCRequest,
 | 
			
		||||
      function: String,
 | 
			
		||||
      ctx: &C,
 | 
			
		||||
   ) -> Result<(bool, Value)> {
 | 
			
		||||
      if function == "GetTest" {
 | 
			
		||||
         if msg.params.is_array() {
 | 
			
		||||
            let arr = msg.params.as_array().unwrap(); //TODO: Check this, but it should never ever happen
 | 
			
		||||
 | 
			
		||||
            let res = self
 | 
			
		||||
               .implementation
 | 
			
		||||
               .GetTest(
 | 
			
		||||
                  serde_json::from_value(arr[0].clone())
 | 
			
		||||
                     .map_err(|_| "Parameter for field 'name' should be a string!")?,
 | 
			
		||||
                  serde_json::from_value(arr[1].clone())
 | 
			
		||||
                     .map_err(|_| "Parameter for field 'age' should be a int!")?,
 | 
			
		||||
                  ctx,
 | 
			
		||||
               )
 | 
			
		||||
               .await?;
 | 
			
		||||
            return Ok((true, serde_json::to_value(res)?));
 | 
			
		||||
         } else if msg.params.is_object() {
 | 
			
		||||
            return Err(Box::from("Not implemented yet".to_owned()));
 | 
			
		||||
         } else {
 | 
			
		||||
            return Err(Box::from("Invalid parameters??".to_owned()));
 | 
			
		||||
         }
 | 
			
		||||
      } else if function == "TestNot" {
 | 
			
		||||
         if msg.params.is_array() {
 | 
			
		||||
            let arr = msg.params.as_array().unwrap(); //TODO: Check this, but it should never ever happen
 | 
			
		||||
 | 
			
		||||
            self.implementation.TestNot(ctx).await?;
 | 
			
		||||
            return Ok((false, Value::Null));
 | 
			
		||||
         } else if msg.params.is_object() {
 | 
			
		||||
            return Err(Box::from("Not implemented yet".to_owned()));
 | 
			
		||||
         } else {
 | 
			
		||||
            return Err(Box::from("Invalid parameters??".to_owned()));
 | 
			
		||||
         }
 | 
			
		||||
      }
 | 
			
		||||
      return Err(Box::from(
 | 
			
		||||
         format!("Invalid function {}", function).to_owned(),
 | 
			
		||||
      ));
 | 
			
		||||
   }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// REGION: Not Generated. User implemented
 | 
			
		||||
 | 
			
		||||
struct MyCtx {
 | 
			
		||||
   isAuthenticated: bool,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
struct TestServiceImplementation {}
 | 
			
		||||
#[async_trait]
 | 
			
		||||
impl TestService<MyCtx> for TestServiceImplementation {
 | 
			
		||||
   async fn GetTest(&self, name: String, age: i64, context: &MyCtx) -> Result<Test> {
 | 
			
		||||
      return Ok(Test { name, age });
 | 
			
		||||
   }
 | 
			
		||||
 | 
			
		||||
   async fn TestNot(&self, _: &MyCtx) -> Result<()> {
 | 
			
		||||
      return Ok(());
 | 
			
		||||
   }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										27
									
								
								templates/Rust/src/server.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								templates/Rust/src/server.rs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,27 @@
 | 
			
		||||
use crate::service::{ComChannel, JRPCRequest, JRPCResult, Result};
 | 
			
		||||
use async_trait::async_trait;
 | 
			
		||||
use std::collections::HashMap;
 | 
			
		||||
use std::error::Error;
 | 
			
		||||
 | 
			
		||||
#[#[derive(Send)]]
 | 
			
		||||
pub struct JRPCServer {
 | 
			
		||||
   channel: Box<dyn ComChannel>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl JRPCServer {
 | 
			
		||||
   fn new(channel: Box<dyn ComChannel>) -> JRPCServer {
 | 
			
		||||
      return JRPCServer {
 | 
			
		||||
         channel: channel,
 | 
			
		||||
         services: HashMap::new(),
 | 
			
		||||
      };
 | 
			
		||||
   }
 | 
			
		||||
   async fn run(&self) -> Result<()> {
 | 
			
		||||
      loop {
 | 
			
		||||
         let p = self.channel.readPacket().await?;
 | 
			
		||||
      }
 | 
			
		||||
   }
 | 
			
		||||
 | 
			
		||||
   fn addService(serv: impl JRPCService) {
 | 
			
		||||
 | 
			
		||||
   }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										52
									
								
								templates/Rust/src/service.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								templates/Rust/src/service.rs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,52 @@
 | 
			
		||||
use async_trait::async_trait;
 | 
			
		||||
use serde_json::Value;
 | 
			
		||||
use std::error::Error;
 | 
			
		||||
 | 
			
		||||
// use serde_json::from_value()
 | 
			
		||||
// use int_enum::IntEnum;
 | 
			
		||||
use serde::{Deserialize, Serialize};
 | 
			
		||||
// use std::collections::HashMap;
 | 
			
		||||
// use std::future::Future;
 | 
			
		||||
 | 
			
		||||
pub type Result<T> = std::result::Result<T, Box<dyn Error>>;
 | 
			
		||||
 | 
			
		||||
//TODO: Vec<u8> or String.
 | 
			
		||||
 | 
			
		||||
pub trait ComChannel: Send + Sync {
 | 
			
		||||
   fn send_packet(&self, packet: String) -> Result<()>;
 | 
			
		||||
   fn read_packet(&self) -> Result<String>;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// TODO: Check what happens when error code is not included
 | 
			
		||||
// #[repr(i64)]
 | 
			
		||||
// #[derive(Clone, Copy, Debug, Eq, PartialEq, IntEnum, Deserialize, Serialize)]
 | 
			
		||||
// pub enum ErrorCodes {
 | 
			
		||||
//    ParseError = -32700,
 | 
			
		||||
//    InvalidRequest = -32600,
 | 
			
		||||
//    MethodNotFound = -32601,
 | 
			
		||||
//    InvalidParams = -32602,
 | 
			
		||||
//    InternalError = -32603,
 | 
			
		||||
// }
 | 
			
		||||
 | 
			
		||||
#[derive(Serialize, Deserialize)]
 | 
			
		||||
pub struct JRPCRequest {
 | 
			
		||||
   pub jsonrpc: String,
 | 
			
		||||
   pub id: Option<String>,
 | 
			
		||||
   pub method: String,
 | 
			
		||||
   pub params: Value,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Serialize, Deserialize)]
 | 
			
		||||
pub struct JRPCError {
 | 
			
		||||
   pub code: i64,
 | 
			
		||||
   pub message: String,
 | 
			
		||||
   pub data: Value,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Serialize, Deserialize)]
 | 
			
		||||
pub struct JRPCResult {
 | 
			
		||||
   pub jsonrpc: String,
 | 
			
		||||
   pub id: String,
 | 
			
		||||
   pub result: Value,
 | 
			
		||||
   pub error: Option<JRPCError>,
 | 
			
		||||
}
 | 
			
		||||
@ -4,13 +4,24 @@ export class VerificationError extends Error {
 | 
			
		||||
      public readonly field?: string,
 | 
			
		||||
      public readonly value?: any
 | 
			
		||||
   ) {
 | 
			
		||||
      super("Parameter verification failed! " +(type ? "Expected " + type + "! " :"") + (field ? "At: " + field + "! " : ""));
 | 
			
		||||
      super(
 | 
			
		||||
         "Parameter verification failed! " +
 | 
			
		||||
            (type ? "Expected " + type + "! " : "") +
 | 
			
		||||
            (field ? "At: " + field + "! " : "")
 | 
			
		||||
      );
 | 
			
		||||
   }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export function apply_number(data: any) {
 | 
			
		||||
export function apply_int(data: any) {
 | 
			
		||||
   data = Math.floor(Number(data));
 | 
			
		||||
   if (Number.isNaN(data)) throw new VerificationError("int", undefined, data);
 | 
			
		||||
   return data;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export function apply_float(data: any) {
 | 
			
		||||
   data = Number(data);
 | 
			
		||||
   if(Number.isNaN(data)) throw new VerificationError("number", undefined, data);
 | 
			
		||||
   if (Number.isNaN(data))
 | 
			
		||||
      throw new VerificationError("float", undefined, data);
 | 
			
		||||
   return data;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user