-
-
Notifications
You must be signed in to change notification settings - Fork 193
/
Copy pathindex.ts
87 lines (84 loc) · 2.42 KB
/
index.ts
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import {
RustFileGenerator,
RustRenderCompleteModelOptions,
RUST_COMMON_PRESET,
defaultRustRenderCompleteModelOptions,
RustPackageFeatures
} from '../../src/generators';
import * as path from 'path';
const doc = {
$id: '_address',
type: 'object',
properties: {
street_name: { type: 'string' },
city: { type: 'string', description: 'City description' },
state: { type: 'string' },
house_number: { type: 'number' },
marriage: {
type: 'boolean',
description: 'Status if marriage live in given house'
},
members: {
oneOf: [{ type: 'string' }, { type: 'number' }, { type: 'boolean' }]
},
tuple_type: {
type: 'array',
items: [{ type: 'string' }, { type: 'number' }],
additionalItems: false
},
array_type: {
type: 'array',
items: { type: 'string' },
additionalItems: false
},
enum_type: {
enum: ['Texas', 'Alabama', 'California'],
default: 'California'
}
},
required: ['street_name', 'city', 'state', 'house_number', 'array_type'],
additionalProperties: {
type: 'string'
}
};
export async function generate(): Promise<void> {
// initialize the generator from a preset
const generator = new RustFileGenerator({
presets: [
{
preset: RUST_COMMON_PRESET,
options: {
implementNew: true,
implementDefault: true
}
}
]
});
// Generated files will be written to output/ directory
const outDir = path.join(__dirname, 'output');
// Run the file generator with options
const models = await generator.generateToPackage(doc, outDir, {
...defaultRustRenderCompleteModelOptions,
supportFiles: true, // generate Cargo.toml and lib.rs
package: {
packageName: 'asyncapi-rs-example',
packageVersion: '1.0.0',
// set authors, homepage, repository, and license
authors: ['AsyncAPI Rust Champions'],
homepage: 'https://www.asyncapi.com/tools/modelina',
repository: 'https://github.com/asyncapi/modelina',
license: 'Apache-2.0',
description: 'Rust models generated by AsyncAPI Modelina',
// support 2018 editions and up
edition: '2018',
// enable serde_json
packageFeatures: [RustPackageFeatures.json] as RustPackageFeatures[]
}
} as RustRenderCompleteModelOptions);
for (const model of models) {
console.log(model.result);
}
}
if (require.main === module) {
generate();
}