Skip to content

Commit

Permalink
Rust example
Browse files Browse the repository at this point in the history
  • Loading branch information
YushiOMOTE committed Sep 11, 2022
1 parent b29ae95 commit 6f47a2e
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 19 deletions.
30 changes: 30 additions & 0 deletions examples/generate-javascript-models/api_example.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
openapi: 3.0.1
info:
title: Test
paths:
/example/api:
post:
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/Request'
required: true
responses:
200:
description: success
components:
schemas:
Request:
type: object
required: ['inputVariable']
properties:
inputVariable:
type: array
items:
$ref: '#/components/schemas/Variable'
Variable:
type: object
properties:
v:
type: string
64 changes: 45 additions & 19 deletions examples/generate-javascript-models/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,48 @@
import { JavaScriptGenerator } from '../../src';
import { parse } from 'yaml'
import { RustFileGenerator, RustRenderCompleteModelOptions, RUST_COMMON_PRESET, defaultRustRenderCompleteModelOptions, RustPackageFeatures } from '../../src/generators';

const generator = new JavaScriptGenerator();
const jsonSchemaDraft7 = {
$schema: 'http://json-schema.org/draft-07/schema#',
type: 'object',
additionalProperties: false,
properties: {
email: {
type: 'string',
format: 'email'
}
}
};
import * as fs from 'node:fs/promises';
import * as path from 'node:path';

function replaceExt(file: string, ext: string) : string {
const basename = path.basename(file, path.extname(file))
return path.join(path.dirname(file), basename + ext)
}

export async function generate(file: string): Promise<void> {
const spec = parse(await fs.readFile(file, 'utf8'));

export async function generate() : Promise<void> {
const models = await generator.generate(jsonSchemaDraft7);
for (const model of models) {
console.log(model.result);
}
const generator = new RustFileGenerator({
presets: [
{
preset: RUST_COMMON_PRESET, options: {
implementNew: true,
implementDefault: true,
}
}
]
});

const outDir = path.join(__dirname, 'rust/' + replaceExt(file, ''));

const models = await generator.generateToPackage(spec, 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);
}
generate();

generate('api_example.yml');

0 comments on commit 6f47a2e

Please sign in to comment.