-
Notifications
You must be signed in to change notification settings - Fork 30
/
rust-gen.js
67 lines (58 loc) · 1.93 KB
/
rust-gen.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const {
quicktype,
InputData,
jsonInputForTargetLanguage,
JSONSchemaInput,
JSONSchemaStore,
} = require("quicktype-core");
const $RefParser = require("@apidevtools/json-schema-ref-parser");
const BREAKLINE = "\n";
const SCHEMA_ENTRYPOINT = "config-schema.json";
const BASE_STRUCT_RUNTIME_SPEC = "Spec";
const BASE_STRUCT_IMAGE_SPEC = "ImageSpec";
var fs = require('fs');
function writeFile(fileName, data){
fs.writeFile(fileName, data, function (err) {
if (err) throw err;
console.log('Successfully generated '+fileName);
});
}
async function quicktypeJSONSchema(targetLanguage, typeName, jsonSchemaString) {
const schemaInput = new JSONSchemaInput(new JSONSchemaStore());
await schemaInput.addSource({ name: typeName, schema: jsonSchemaString });
const inputData = new InputData();
inputData.addInput(schemaInput);
return await quicktype({
inputData,
rendererOptions: { visibility: "public" },
lang: targetLanguage,
});
}
function dereference(parentStructName, mySchema, fileName){
$RefParser.dereference(mySchema, (err, schema) => {
if (err) {
console.error(err);
}
else {
schemaGen = JSON.stringify(schema);
generateSchema(parentStructName, JSON.stringify(schema), fileName);
}
})
}
async function generateSchema(parentStructName ,schema, fileName) {
const { lines: rustSpec } = await quicktypeJSONSchema(
"rust",
parentStructName,
schema
);
writeFile(fileName, rustSpec.join(BREAKLINE));
}
function generator(parentStructName ,base, entryPoint, outputPath){
var rootPath = process.cwd();
process.chdir(base);
const myschema = require(base+"/"+entryPoint);
dereference(parentStructName ,myschema, rootPath+"/"+outputPath);
process.chdir(rootPath);
}
generator(BASE_STRUCT_RUNTIME_SPEC,'./runtime-spec/schema', SCHEMA_ENTRYPOINT, 'src/runtime/mod.rs');
generator(BASE_STRUCT_IMAGE_SPEC,'./image-spec/schema', SCHEMA_ENTRYPOINT, 'src/image/mod.rs');