vovk-rust 0.0.1-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,81 @@
1
+ ---
2
+ imports: ['vovk-rust']
3
+ ---
4
+ <% const vars = {
5
+ convertJSONSchemasToRustTypes: t.imports['vovk-rust'].convertJSONSchemasToRustTypes,
6
+ }; %>
7
+ <%- t.getFirstLineBanner() %>
8
+ mod http_request;
9
+ mod read_full_schema;
10
+
11
+ pub use crate::http_request::HttpException;
12
+
13
+ <% Object.entries(t.schema.segments).forEach(([segmentName, segment]) => {
14
+ Object.values(segment.controllers).forEach((controllerSchema) => { %>
15
+ pub mod <%= t._.snakeCase(controllerSchema.rpcModuleName) %> {
16
+ #[allow(unused_imports)]
17
+ use crate::http_request::{HttpException, http_request, http_request_stream};
18
+ use std::collections::HashMap;
19
+ <% Object.entries(controllerSchema.handlers).forEach(([handlerNameOriginal, handlerSchema]) => {
20
+ const { validation, openapi, path, httpMethod } = handlerSchema;
21
+ const handlerName = t._.snakeCase(handlerNameOriginal);
22
+ %>
23
+ // <%= controllerSchema.rpcModuleName %>.<%= handlerName %> <%= httpMethod %> `<%= [t.apiRoot, segmentName, controllerSchema.prefix, handlerSchema.path].filter(Boolean).join('/') %>`
24
+ <%-
25
+ vars.convertJSONSchemasToRustTypes({
26
+ schemas: {
27
+ body: validation?.body,
28
+ query: validation?.query,
29
+ params: validation?.params,
30
+ output: validation?.output,
31
+ iteration: validation?.iteration
32
+ },
33
+ rootName: handlerName,
34
+ pad: 4
35
+ })
36
+ %>
37
+ <%= ([
38
+ openapi?.summary ? `Summary: ${openapi.summary}` : '',
39
+ openapi?.description ? `Description: ${openapi.description}` : '',
40
+ validation?.params?.description ? `Params: ${validation?.params?.description}` : '',
41
+ validation?.body?.description ? `Body: ${validation?.body?.description}`: '',
42
+ validation?.query?.description ? `Query: ${validation?.query?.description}`: '',
43
+ validation?.output?.description ? `Returns: ${validation?.output?.description}`: ''
44
+ ]).filter(Boolean).map((s) => s.split('\n')).flat().map((s) => ' '.repeat(4) + '/// ' + s).join('\n') %>
45
+ pub fn <%= handlerName %>(
46
+ body: <%- validation?.body ? (validation?.body?.['x-isForm'] ?'reqwest::blocking::multipart::Form' : `${handlerName}_::body`): '()' %>,
47
+ query: <%- validation?.query ? `${handlerName}_::query` : '()' %>,
48
+ params: <%- validation?.params ? `${handlerName}_::params` : '()' %>,
49
+ headers: Option<&HashMap<String, String>>,
50
+ api_root: Option<&str>,
51
+ disable_client_validation: bool,
52
+ ) -> <%- validation?.output ? `Result<${handlerName}_::output, HttpException>` : validation?.iteration ? `Result<Box<dyn Iterator<Item = ${handlerName}_::iteration>>, HttpException>` : 'Result<serde_json::Value, HttpException>' %>{
53
+ let result = <%= validation?.iteration ? 'http_request_stream' : 'http_request' %>::<
54
+ <%- [
55
+ validation?.output ? `${handlerName}_::output` : validation?.iteration ? `${handlerName}_::iteration` : 'serde_json::Value',
56
+ validation?.body && !validation?.body?.['x-isForm'] ? `${handlerName}_::body` : '()',
57
+ validation?.query ? `${handlerName}_::query` : '()',
58
+ validation?.params ? `${handlerName}_::params` : '()'
59
+ ].filter(Boolean).map((s) => ' '.repeat(12) + s).join(',\n') %>
60
+ >(
61
+ "<%= t.apiRoot %>",
62
+ "<%= segmentName %>",
63
+ "<%= controllerSchema.rpcModuleName %>",
64
+ "<%= handlerNameOriginal %>",
65
+ <%- !validation?.body || !validation?.body?.['x-isForm'] ? `Some(&body)` : 'None' %>,
66
+ <%- validation?.body && validation?.body?.['x-isForm'] ? `Some(body)` : 'None' %>,
67
+ Some(&query),
68
+ Some(&params),
69
+ headers,
70
+ api_root,
71
+ disable_client_validation,
72
+ );
73
+
74
+ result
75
+ }
76
+ <% }) %>
77
+ }
78
+ <% }) %>
79
+
80
+ <% }) %>
81
+
@@ -0,0 +1,88 @@
1
+ use std::fs::File;
2
+ use std::io::BufReader;
3
+ use std::path::Path;
4
+ use std::collections::HashMap;
5
+ use serde::{Deserialize, Serialize};
6
+ use serde_json::Value;
7
+
8
+ /// Validation schema structure
9
+ #[derive(Debug, Deserialize, Serialize)]
10
+ pub struct ValidationSchema {
11
+ #[serde(skip_serializing_if = "Option::is_none")]
12
+ pub body: Option<Value>,
13
+ #[serde(skip_serializing_if = "Option::is_none")]
14
+ pub query: Option<Value>,
15
+ #[serde(skip_serializing_if = "Option::is_none")]
16
+ pub params: Option<Value>,
17
+ #[serde(skip_serializing_if = "Option::is_none")]
18
+ pub output: Option<Value>,
19
+ #[serde(skip_serializing_if = "Option::is_none")]
20
+ pub iteration: Option<Value>,
21
+ }
22
+
23
+ /// OpenAPI documentation
24
+ #[derive(Debug, Deserialize, Serialize)]
25
+ pub struct OpenApiDocs {
26
+ pub summary: Option<String>,
27
+ pub description: Option<String>,
28
+ #[serde(flatten)]
29
+ pub additional_fields: HashMap<String, Value>,
30
+ }
31
+
32
+ /// Handler schema
33
+ #[derive(Debug, Deserialize, Serialize)]
34
+ #[allow(non_snake_case)]
35
+ pub struct HandlerSchema {
36
+ pub path: String,
37
+ pub httpMethod: String,
38
+ #[serde(skip_serializing_if = "Option::is_none")]
39
+ pub validation: Option<ValidationSchema>,
40
+ #[serde(skip_serializing_if = "Option::is_none")]
41
+ pub openapi: Option<OpenApiDocs>,
42
+ #[serde(skip_serializing_if = "Option::is_none")]
43
+ pub misc: Option<HashMap<String, Value>>,
44
+ }
45
+
46
+ /// Controller schema
47
+ #[derive(Debug, Deserialize, Serialize)]
48
+ #[allow(non_snake_case)]
49
+ pub struct ControllerSchema {
50
+ pub rpcModuleName: String,
51
+ pub originalControllerName: String,
52
+ pub prefix: String,
53
+ pub handlers: HashMap<String, HandlerSchema>,
54
+ }
55
+
56
+ /// Schema for individual segment
57
+ #[derive(Debug, Deserialize, Serialize)]
58
+ #[allow(non_snake_case)]
59
+ pub struct VovkSegmentSchema {
60
+ pub emitSchema: bool,
61
+ pub segmentName: String,
62
+ pub controllers: HashMap<String, ControllerSchema>,
63
+ }
64
+
65
+ /// Complete Vovk schema with meta and multiple segments
66
+ #[derive(Debug, Deserialize, Serialize)]
67
+ pub struct VovkSchema {
68
+ pub meta: HashMap<String, Value>,
69
+ pub segments: HashMap<String, VovkSegmentSchema>,
70
+ }
71
+
72
+ /// Read the complete Vovk schema from a JSON file
73
+ pub fn read_full_schema() -> Result<VovkSchema, Box<dyn std::error::Error>> {
74
+ // Get the path to the project root (where Cargo.toml is)
75
+ let manifest_dir = env!("CARGO_MANIFEST_DIR");
76
+
77
+ // Build the full path to the data file
78
+ let json_path = Path::new(manifest_dir).join("src/schema.json");
79
+
80
+ // Open the file
81
+ let file = File::open(&json_path)?;
82
+ let reader = BufReader::new(file);
83
+
84
+ // Parse the JSON
85
+ let schema: VovkSchema = serde_json::from_reader(reader)?;
86
+
87
+ Ok(schema)
88
+ }
package/index.d.ts ADDED
@@ -0,0 +1,16 @@
1
+ import type { KnownAny } from 'vovk';
2
+ export declare function indent(level: number, pad?: number): string;
3
+ export declare function generateDocComment(schema: KnownAny, level: number, pad?: number): string;
4
+ export declare function resolveRef(ref: string, rootSchema: KnownAny): KnownAny | undefined;
5
+ export declare function getModulePath(path: string[]): string;
6
+ export declare function toRustType(schema: KnownAny, path: string[], rootSchema?: KnownAny): string;
7
+ export declare function generateEnum(schema: KnownAny, name: string, level: number, pad?: number): string;
8
+ export declare function generateVariantEnum(schema: KnownAny, name: string, path: string[], level: number, rootSchema: KnownAny, pad?: number): string;
9
+ export declare function generateAllOfType(schemas: KnownAny[], name: string, path: string[], level: number, rootSchema: KnownAny, pad?: number): string;
10
+ export declare function processObject(schema: KnownAny, path: string[], level: number, rootSchema?: KnownAny, pad?: number): string;
11
+ export declare function processPrimitive(schema: KnownAny, name: string, level: number, pad?: number): string;
12
+ export declare function convertJSONSchemasToRustTypes({ schemas, pad, rootName, }: {
13
+ schemas: Record<string, KnownAny | undefined>;
14
+ pad?: number;
15
+ rootName: string;
16
+ }): string;