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.
- package/README.md +17 -0
- package/client-templates/rsPkg/Cargo.toml.ejs +84 -0
- package/client-templates/rsReadme/README.md.ejs +37 -0
- package/client-templates/rsSrc/http_request.rs +488 -0
- package/client-templates/rsSrc/lib.rs.ejs +81 -0
- package/client-templates/rsSrc/read_full_schema.rs +88 -0
- package/index.d.ts +16 -0
- package/index.js +534 -0
- package/package.json +33 -0
package/index.js
ADDED
|
@@ -0,0 +1,534 @@
|
|
|
1
|
+
// Helper function for indentation
|
|
2
|
+
export function indent(level, pad = 0) {
|
|
3
|
+
return ' '.repeat(pad + level * 2);
|
|
4
|
+
}
|
|
5
|
+
// Generate documentation comments from title and description
|
|
6
|
+
export function generateDocComment(schema, level, pad = 0) {
|
|
7
|
+
if (!schema?.title && !schema?.description)
|
|
8
|
+
return '';
|
|
9
|
+
let comment = '';
|
|
10
|
+
if (schema.title) {
|
|
11
|
+
comment += `${indent(level, pad)}/// ${schema.title}\n`;
|
|
12
|
+
if (schema.description) {
|
|
13
|
+
comment += `${indent(level, pad)}///\n`;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
if (schema.description) {
|
|
17
|
+
// Split description into lines and add /// to each line
|
|
18
|
+
const lines = schema.description.split('\n');
|
|
19
|
+
for (const line of lines) {
|
|
20
|
+
comment += `${indent(level, pad)}/// ${line}\n`;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return comment;
|
|
24
|
+
}
|
|
25
|
+
// Track processed $refs to avoid circular references
|
|
26
|
+
const processedRefs = new Set();
|
|
27
|
+
// Resolve $ref paths in the schema
|
|
28
|
+
export function resolveRef(ref, rootSchema) {
|
|
29
|
+
if (processedRefs.has(ref)) {
|
|
30
|
+
// Circular reference detected, return a placeholder
|
|
31
|
+
return { type: 'string' };
|
|
32
|
+
}
|
|
33
|
+
processedRefs.add(ref);
|
|
34
|
+
// Format: #/$defs/TypeName or #/components/schemas/TypeName or #/definitions/TypeName etc.
|
|
35
|
+
const parts = ref.split('/').filter((part) => part && part !== '#');
|
|
36
|
+
// Standard path traversal
|
|
37
|
+
let current = rootSchema;
|
|
38
|
+
for (const part of parts) {
|
|
39
|
+
if (!current || typeof current !== 'object') {
|
|
40
|
+
// If standard traversal fails and the path might be a definition reference
|
|
41
|
+
if (parts.includes('definitions') && rootSchema.definitions) {
|
|
42
|
+
// Try to access the definition directly using the last part as the key
|
|
43
|
+
const definitionKey = parts[parts.length - 1];
|
|
44
|
+
return rootSchema.definitions[definitionKey];
|
|
45
|
+
}
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
48
|
+
current = current[part];
|
|
49
|
+
}
|
|
50
|
+
// If the resolved schema also has a $ref, resolve it recursively
|
|
51
|
+
if (current && current.$ref) {
|
|
52
|
+
return resolveRef(current.$ref, rootSchema);
|
|
53
|
+
}
|
|
54
|
+
processedRefs.delete(ref);
|
|
55
|
+
return current;
|
|
56
|
+
}
|
|
57
|
+
// Generate module path for nested types
|
|
58
|
+
export function getModulePath(path) {
|
|
59
|
+
if (path.length <= 1)
|
|
60
|
+
return path[0];
|
|
61
|
+
let result = '';
|
|
62
|
+
for (let i = 0; i < path.length - 1; i++) {
|
|
63
|
+
result += path[i] + '_::';
|
|
64
|
+
}
|
|
65
|
+
result += path[path.length - 1];
|
|
66
|
+
return result;
|
|
67
|
+
}
|
|
68
|
+
// Convert JSON Schema type to Rust type
|
|
69
|
+
export function toRustType(schema, path, rootSchema = schema) {
|
|
70
|
+
if (!schema)
|
|
71
|
+
return 'String';
|
|
72
|
+
// Handle $ref first
|
|
73
|
+
if (schema.$ref) {
|
|
74
|
+
const resolvedSchema = resolveRef(schema.$ref, rootSchema);
|
|
75
|
+
if (resolvedSchema) {
|
|
76
|
+
// Extract type name from ref path for better type naming
|
|
77
|
+
const refName = schema.$ref.split('/').pop();
|
|
78
|
+
// Use the ref name directly if it's a definition reference
|
|
79
|
+
if (schema.$ref.includes('definitions/') || schema.$ref.includes('$defs/')) {
|
|
80
|
+
return refName || 'String';
|
|
81
|
+
}
|
|
82
|
+
return toRustType(resolvedSchema, refName ? [...path.slice(0, -1), refName] : path, rootSchema);
|
|
83
|
+
}
|
|
84
|
+
return 'serde_json::Value'; // Fallback for unresolved $ref
|
|
85
|
+
}
|
|
86
|
+
// Check for enum without type (assume string)
|
|
87
|
+
if (schema.enum) {
|
|
88
|
+
if (schema.type === 'string' || !schema.type) {
|
|
89
|
+
return `${getModulePath(path)}`;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
if (schema.type === 'string') {
|
|
93
|
+
return 'String';
|
|
94
|
+
}
|
|
95
|
+
else if (schema.type === 'number' || schema.type === 'integer') {
|
|
96
|
+
// Handle numeric types with constraints
|
|
97
|
+
if (schema.type === 'integer') {
|
|
98
|
+
// Determine integer type based on min/max constraints
|
|
99
|
+
const min = typeof schema.minimum === 'number'
|
|
100
|
+
? schema.minimum
|
|
101
|
+
: typeof schema.exclusiveMinimum === 'number'
|
|
102
|
+
? schema.exclusiveMinimum + 1
|
|
103
|
+
: undefined;
|
|
104
|
+
const max = typeof schema.maximum === 'number'
|
|
105
|
+
? schema.maximum
|
|
106
|
+
: typeof schema.exclusiveMaximum === 'number'
|
|
107
|
+
? schema.exclusiveMaximum - 1
|
|
108
|
+
: undefined;
|
|
109
|
+
// Check if we need unsigned (no negative values)
|
|
110
|
+
if (min !== undefined && min >= 0) {
|
|
111
|
+
// Choose appropriate unsigned int size
|
|
112
|
+
if (max !== undefined) {
|
|
113
|
+
if (max <= 255)
|
|
114
|
+
return 'u8';
|
|
115
|
+
if (max <= 65535)
|
|
116
|
+
return 'u16';
|
|
117
|
+
if (max <= 4294967295)
|
|
118
|
+
return 'u32';
|
|
119
|
+
}
|
|
120
|
+
return 'u64'; // Default unsigned
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
// Choose appropriate signed int size
|
|
124
|
+
if (min !== undefined && max !== undefined) {
|
|
125
|
+
const absMin = Math.abs(min);
|
|
126
|
+
const absMax = Math.abs(max);
|
|
127
|
+
const maxVal = Math.max(absMin - 1, absMax);
|
|
128
|
+
if (maxVal <= 127)
|
|
129
|
+
return 'i8';
|
|
130
|
+
if (maxVal <= 32767)
|
|
131
|
+
return 'i16';
|
|
132
|
+
if (maxVal <= 2147483647)
|
|
133
|
+
return 'i32';
|
|
134
|
+
}
|
|
135
|
+
return 'i64'; // Default signed
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
// Floating point
|
|
140
|
+
const hasLowRange = (schema.minimum !== undefined &&
|
|
141
|
+
schema.maximum !== undefined &&
|
|
142
|
+
Math.abs(schema.maximum - schema.minimum) <= 3.4e38) ||
|
|
143
|
+
(schema.exclusiveMinimum !== undefined &&
|
|
144
|
+
schema.exclusiveMaximum !== undefined &&
|
|
145
|
+
Math.abs(schema.exclusiveMaximum - schema.exclusiveMinimum) <= 3.4e38);
|
|
146
|
+
return hasLowRange ? 'f32' : 'f64';
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
else if (schema.type === 'boolean') {
|
|
150
|
+
return 'bool';
|
|
151
|
+
}
|
|
152
|
+
else if (schema.type === 'null') {
|
|
153
|
+
return '()';
|
|
154
|
+
}
|
|
155
|
+
else if (schema.type === 'array') {
|
|
156
|
+
if (schema.items) {
|
|
157
|
+
// Check if array items are objects that need special handling
|
|
158
|
+
if (schema.items.type === 'object' || schema.items.properties || schema.items.$ref) {
|
|
159
|
+
// For array of objects, reference the item type with proper module path
|
|
160
|
+
// Find the parent module name to reference the item
|
|
161
|
+
const parentName = path[path.length - 2] || path[0];
|
|
162
|
+
return `Vec<${parentName}_::${path[path.length - 1]}Item>`;
|
|
163
|
+
}
|
|
164
|
+
const itemType = toRustType(schema.items, [...path, '_item'], rootSchema);
|
|
165
|
+
return `Vec<${itemType}>`;
|
|
166
|
+
}
|
|
167
|
+
return 'Vec<String>';
|
|
168
|
+
}
|
|
169
|
+
else if (schema.type === 'object' || schema.properties) {
|
|
170
|
+
// Handle empty objects
|
|
171
|
+
if (schema.type === 'object' && (!schema.properties || Object.keys(schema.properties).length === 0)) {
|
|
172
|
+
return 'serde_json::Value';
|
|
173
|
+
}
|
|
174
|
+
return path[path.length - 1];
|
|
175
|
+
}
|
|
176
|
+
else if (schema.anyOf || schema.oneOf || schema.allOf) {
|
|
177
|
+
return `${getModulePath(path)}`;
|
|
178
|
+
}
|
|
179
|
+
return 'String'; // Default fallback
|
|
180
|
+
}
|
|
181
|
+
// Generate enum for string with enum values
|
|
182
|
+
export function generateEnum(schema, name, level, pad = 0) {
|
|
183
|
+
const indentFn = (level) => ' '.repeat(pad + level * 2);
|
|
184
|
+
let code = '';
|
|
185
|
+
// Add documentation comments for the enum
|
|
186
|
+
code += generateDocComment(schema, level, pad);
|
|
187
|
+
code += `${indentFn(level)}#[derive(Debug, Serialize, Deserialize, Clone)]\n`;
|
|
188
|
+
code += `${indentFn(level)}#[allow(non_camel_case_types)]\n`;
|
|
189
|
+
code += `${indentFn(level)}pub enum ${name} {\n`;
|
|
190
|
+
schema.enum.forEach((value, index) => {
|
|
191
|
+
// Create valid Rust enum variant
|
|
192
|
+
const variant = value?.replace(/[^a-zA-Z0-9_]/g, '_');
|
|
193
|
+
// Add documentation if available in enumDescriptions
|
|
194
|
+
if (schema.enumDescriptions && schema.enumDescriptions[index]) {
|
|
195
|
+
const description = schema.enumDescriptions[index];
|
|
196
|
+
code += `${indentFn(level + 1)}/// ${description}\n`;
|
|
197
|
+
}
|
|
198
|
+
code += `${indentFn(level + 1)}#[serde(rename = "${value}")]\n`;
|
|
199
|
+
code += `${indentFn(level + 1)}${variant},\n`;
|
|
200
|
+
});
|
|
201
|
+
code += `${indentFn(level)}}\n\n`;
|
|
202
|
+
return code;
|
|
203
|
+
}
|
|
204
|
+
// Generate enum for anyOf/oneOf/allOf schemas
|
|
205
|
+
export function generateVariantEnum(schema, name, path, level, rootSchema, pad = 0) {
|
|
206
|
+
const indentFn = (level) => ' '.repeat(pad + level * 2);
|
|
207
|
+
// Handle allOf separately - it should combine schemas rather than create variants
|
|
208
|
+
if (schema.allOf) {
|
|
209
|
+
return generateAllOfType(schema.allOf, name, path, level, rootSchema, pad);
|
|
210
|
+
}
|
|
211
|
+
const variants = schema.anyOf || schema.oneOf || [];
|
|
212
|
+
let code = '';
|
|
213
|
+
let nestedTypes = '';
|
|
214
|
+
// Add documentation comments for the enum
|
|
215
|
+
code += generateDocComment(schema, level, pad);
|
|
216
|
+
code += `${indentFn(level)}#[derive(Debug, Serialize, Deserialize, Clone)]\n`;
|
|
217
|
+
code += `${indentFn(level)}#[allow(non_camel_case_types)]\n`;
|
|
218
|
+
code += `${indentFn(level)}#[serde(untagged)]\n`;
|
|
219
|
+
code += `${indentFn(level)}pub enum ${name} {\n`;
|
|
220
|
+
variants.forEach((variant, index) => {
|
|
221
|
+
// Resolve $ref if present
|
|
222
|
+
if (variant.$ref) {
|
|
223
|
+
const resolved = resolveRef(variant.$ref, rootSchema);
|
|
224
|
+
if (resolved) {
|
|
225
|
+
variant = resolved;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
const variantName = `Variant${index}`;
|
|
229
|
+
const variantPath = [...path, name, variantName];
|
|
230
|
+
// If it's an object type, we need to create a separate struct
|
|
231
|
+
if (variant.type === 'object' || variant.properties) {
|
|
232
|
+
code += `${indentFn(level + 1)}${variantName}(${name}_::${variantName}),\n`;
|
|
233
|
+
// Create a nested type definition to be added outside the enum
|
|
234
|
+
nestedTypes += processObject(variant, variantPath, level, rootSchema, pad);
|
|
235
|
+
}
|
|
236
|
+
else {
|
|
237
|
+
// For simple types, we can include them directly in the enum
|
|
238
|
+
const variantType = toRustType(variant, variantPath, rootSchema);
|
|
239
|
+
code += `${indentFn(level + 1)}${variantName}(${variantType}),\n`;
|
|
240
|
+
}
|
|
241
|
+
});
|
|
242
|
+
code += `${indentFn(level)}}\n\n`;
|
|
243
|
+
// Add nested type definitions if needed
|
|
244
|
+
if (nestedTypes) {
|
|
245
|
+
code += nestedTypes;
|
|
246
|
+
}
|
|
247
|
+
return code;
|
|
248
|
+
}
|
|
249
|
+
// Handle allOf schema by merging properties
|
|
250
|
+
export function generateAllOfType(schemas, name, path, level, rootSchema, pad = 0) {
|
|
251
|
+
const mergedSchema = {
|
|
252
|
+
type: 'object',
|
|
253
|
+
properties: {},
|
|
254
|
+
required: [],
|
|
255
|
+
};
|
|
256
|
+
// Merge all schemas in allOf
|
|
257
|
+
schemas.forEach((schema) => {
|
|
258
|
+
// Resolve $ref if present
|
|
259
|
+
if (schema.$ref) {
|
|
260
|
+
const resolved = resolveRef(schema.$ref, rootSchema);
|
|
261
|
+
if (resolved) {
|
|
262
|
+
schema = resolved;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
if (schema.properties) {
|
|
266
|
+
mergedSchema.properties = {
|
|
267
|
+
...mergedSchema.properties,
|
|
268
|
+
...schema.properties,
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
if (schema.required) {
|
|
272
|
+
mergedSchema.required = [...mergedSchema.required, ...schema.required];
|
|
273
|
+
}
|
|
274
|
+
});
|
|
275
|
+
// Process the merged schema as a regular object
|
|
276
|
+
return processObject(mergedSchema, [...path, name], level, rootSchema, pad);
|
|
277
|
+
}
|
|
278
|
+
// Process schema objects and generate Rust code
|
|
279
|
+
export function processObject(schema, path, level, rootSchema = schema, pad = 0) {
|
|
280
|
+
const indentFn = (level) => ' '.repeat(pad + level * 2);
|
|
281
|
+
if (!schema) {
|
|
282
|
+
return '';
|
|
283
|
+
}
|
|
284
|
+
// Handle empty objects
|
|
285
|
+
if (schema.type === 'object' && (!schema.properties || Object.keys(schema.properties).length === 0)) {
|
|
286
|
+
// Empty object is handled as serde_json::Value at the field level
|
|
287
|
+
return '';
|
|
288
|
+
}
|
|
289
|
+
if (!schema.properties) {
|
|
290
|
+
return '';
|
|
291
|
+
}
|
|
292
|
+
const currentName = path[path.length - 1];
|
|
293
|
+
let code = '';
|
|
294
|
+
// Add documentation comments for the struct
|
|
295
|
+
code += generateDocComment(schema, level, pad);
|
|
296
|
+
if (schema.type === 'object' && schema['x-isForm'] === true) {
|
|
297
|
+
code += `${indentFn(level)}pub use reqwest::multipart::Form as ${currentName};\n`;
|
|
298
|
+
return code;
|
|
299
|
+
}
|
|
300
|
+
// Generate struct
|
|
301
|
+
code += `${indentFn(level)}#[derive(Debug, Serialize, Deserialize, Clone)]\n`;
|
|
302
|
+
code += `${indentFn(level)}#[allow(non_snake_case, non_camel_case_types)]\n`;
|
|
303
|
+
code += `${indentFn(level)}pub struct ${currentName} {\n`;
|
|
304
|
+
// Generate struct fields
|
|
305
|
+
Object.entries(schema.properties).forEach(([propName, propSchema]) => {
|
|
306
|
+
const isRequired = schema.required && schema.required.includes(propName);
|
|
307
|
+
// Handle $ref in property
|
|
308
|
+
if (propSchema.$ref) {
|
|
309
|
+
const resolvedSchema = resolveRef(propSchema.$ref, rootSchema);
|
|
310
|
+
if (resolvedSchema) {
|
|
311
|
+
propSchema = resolvedSchema;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
// Add documentation comments for the field
|
|
315
|
+
code += generateDocComment(propSchema, level + 1, pad);
|
|
316
|
+
// Determine if this property is a nested type that should be accessed via module path
|
|
317
|
+
const isNestedObject = propSchema.type === 'object' || propSchema.properties;
|
|
318
|
+
const isGenericObject = propSchema.type === 'object' && !propSchema.properties;
|
|
319
|
+
// Define nested enum types
|
|
320
|
+
const isNestedEnum = ((propSchema.type === 'string' || !propSchema.type) && propSchema.enum) ||
|
|
321
|
+
propSchema.anyOf ||
|
|
322
|
+
propSchema.oneOf ||
|
|
323
|
+
propSchema.allOf;
|
|
324
|
+
const propPath = [...path, propName];
|
|
325
|
+
let propType;
|
|
326
|
+
if (isGenericObject) {
|
|
327
|
+
// For generic objects, we can use serde_json::Value
|
|
328
|
+
propType = 'serde_json::Value';
|
|
329
|
+
}
|
|
330
|
+
else if (isNestedObject || isNestedEnum) {
|
|
331
|
+
// For nested objects and enums, we need to reference them via their module path
|
|
332
|
+
propType = `${currentName}_::${propName}`;
|
|
333
|
+
// Special case for enums which have a different naming convention
|
|
334
|
+
if (isNestedEnum) {
|
|
335
|
+
propType += ''; // 'Enum';
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
else {
|
|
339
|
+
// For other types, use the standard type resolution
|
|
340
|
+
propType = toRustType(propSchema, propPath, rootSchema);
|
|
341
|
+
}
|
|
342
|
+
if (!isRequired) {
|
|
343
|
+
propType = `Option<${propType}>`;
|
|
344
|
+
}
|
|
345
|
+
code += `${indentFn(level + 1)}pub ${propName}: ${propType},\n`;
|
|
346
|
+
});
|
|
347
|
+
code += `${indentFn(level)}}\n\n`;
|
|
348
|
+
// Check if any properties require nested types before generating the sub-module
|
|
349
|
+
const hasNestedTypes = Object.entries(schema.properties).some(([, propSchema]) => {
|
|
350
|
+
// Resolve $ref if present
|
|
351
|
+
if (propSchema.$ref) {
|
|
352
|
+
const resolved = resolveRef(propSchema.$ref, rootSchema);
|
|
353
|
+
propSchema = resolved || propSchema;
|
|
354
|
+
}
|
|
355
|
+
return (propSchema.type === 'object' ||
|
|
356
|
+
propSchema.properties ||
|
|
357
|
+
((propSchema.type === 'string' || !propSchema.type) && propSchema.enum) ||
|
|
358
|
+
(propSchema.type === 'array' &&
|
|
359
|
+
propSchema.items &&
|
|
360
|
+
(propSchema.items.type === 'object' || propSchema.items.properties || propSchema.items.$ref)) ||
|
|
361
|
+
propSchema.anyOf ||
|
|
362
|
+
propSchema.oneOf ||
|
|
363
|
+
propSchema.allOf);
|
|
364
|
+
});
|
|
365
|
+
// Only generate sub-modules if there are nested types
|
|
366
|
+
if (hasNestedTypes && Object.keys(schema.properties).length > 0) {
|
|
367
|
+
code += `${indentFn(level)}#[allow(non_snake_case)]\n`;
|
|
368
|
+
code += `${indentFn(level)}pub mod ${currentName}_ {\n`;
|
|
369
|
+
code += `${indentFn(level + 1)}use serde::{Serialize, Deserialize};\n\n`;
|
|
370
|
+
Object.entries(schema.properties).forEach(([propName, propSchema]) => {
|
|
371
|
+
// Resolve $ref if present
|
|
372
|
+
if (propSchema.$ref) {
|
|
373
|
+
const resolved = resolveRef(propSchema.$ref, rootSchema);
|
|
374
|
+
if (resolved) {
|
|
375
|
+
propSchema = resolved;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
// Generate nested object types
|
|
379
|
+
if (propSchema.type === 'object' || propSchema.properties) {
|
|
380
|
+
code += processObject(propSchema, [...path, propName], level + 1, rootSchema, pad);
|
|
381
|
+
}
|
|
382
|
+
// Generate enum types for string enums (also when type is missing but enum exists)
|
|
383
|
+
else if ((propSchema.type === 'string' || !propSchema.type) && propSchema.enum) {
|
|
384
|
+
code += generateEnum(propSchema, propName, level + 1, pad);
|
|
385
|
+
}
|
|
386
|
+
// Generate types for array items if they're objects
|
|
387
|
+
else if (propSchema.type === 'array' && propSchema.items) {
|
|
388
|
+
// Check if items has a $ref
|
|
389
|
+
if (propSchema.items.$ref) {
|
|
390
|
+
const resolved = resolveRef(propSchema.items.$ref, rootSchema);
|
|
391
|
+
if (resolved && (resolved.type === 'object' || resolved.properties)) {
|
|
392
|
+
code += processObject(resolved, [...path, propName + 'Item'], level + 1, rootSchema, pad);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
else if (propSchema.items.type === 'object' || propSchema.items.properties) {
|
|
396
|
+
code += processObject(propSchema.items, [...path, propName + 'Item'], level + 1, rootSchema, pad);
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
// Handle anyOf/oneOf/allOf schemas
|
|
400
|
+
else if (propSchema.anyOf || propSchema.oneOf || propSchema.allOf) {
|
|
401
|
+
code += generateVariantEnum(propSchema, propName, path, level + 1, rootSchema, pad);
|
|
402
|
+
}
|
|
403
|
+
});
|
|
404
|
+
code += `${indentFn(level)}}\n`;
|
|
405
|
+
}
|
|
406
|
+
return code;
|
|
407
|
+
}
|
|
408
|
+
// Generate code for primitive types
|
|
409
|
+
export function processPrimitive(schema, name, level, pad = 0) {
|
|
410
|
+
const indentFn = (level) => ' '.repeat(pad + level * 2);
|
|
411
|
+
let code = '';
|
|
412
|
+
// Add documentation comments
|
|
413
|
+
code += generateDocComment(schema, level, pad);
|
|
414
|
+
// For primitive types, create a type alias
|
|
415
|
+
code += `${indentFn(level)}pub type ${name} = `;
|
|
416
|
+
if (schema.type === 'string') {
|
|
417
|
+
code += 'String';
|
|
418
|
+
}
|
|
419
|
+
else if (schema.type === 'number') {
|
|
420
|
+
code += 'f64';
|
|
421
|
+
}
|
|
422
|
+
else if (schema.type === 'integer') {
|
|
423
|
+
code += 'i64';
|
|
424
|
+
}
|
|
425
|
+
else if (schema.type === 'boolean') {
|
|
426
|
+
code += 'bool';
|
|
427
|
+
}
|
|
428
|
+
else if (schema.type === 'null') {
|
|
429
|
+
code += '()';
|
|
430
|
+
}
|
|
431
|
+
else if (schema.enum) {
|
|
432
|
+
// If it has enum values, we'll generate an actual enum instead of a type alias
|
|
433
|
+
return generateEnum(schema, name, level, pad);
|
|
434
|
+
}
|
|
435
|
+
else {
|
|
436
|
+
code += 'String'; // Default fallback
|
|
437
|
+
}
|
|
438
|
+
code += ';\n\n';
|
|
439
|
+
return code;
|
|
440
|
+
}
|
|
441
|
+
export function convertJSONSchemasToRustTypes({ schemas, pad = 0, rootName, }) {
|
|
442
|
+
// Check if all schemas are undefined
|
|
443
|
+
const hasDefinedSchemas = Object.values(schemas).some((schema) => schema !== undefined);
|
|
444
|
+
if (!hasDefinedSchemas) {
|
|
445
|
+
return '';
|
|
446
|
+
}
|
|
447
|
+
const indentFn = (level) => ' '.repeat(pad + level * 2);
|
|
448
|
+
// Start code generation
|
|
449
|
+
let result = `${indentFn(0)}pub mod ${rootName}_ {\n`;
|
|
450
|
+
result += `${indentFn(1)}use serde::{Serialize, Deserialize};\n`;
|
|
451
|
+
// Process each schema in the schemas object
|
|
452
|
+
Object.entries(schemas).forEach(([schemaName, schemaObj]) => {
|
|
453
|
+
// Skip undefined schemas
|
|
454
|
+
if (!schemaObj)
|
|
455
|
+
return;
|
|
456
|
+
// Extract and process types from $defs if present
|
|
457
|
+
if (schemaObj.$defs) {
|
|
458
|
+
Object.entries(schemaObj.$defs).forEach(([defName, defSchema]) => {
|
|
459
|
+
// Create a root object for each definition
|
|
460
|
+
if (defSchema) {
|
|
461
|
+
if (defSchema.type === 'object' || defSchema.properties) {
|
|
462
|
+
const rootDefObject = {
|
|
463
|
+
type: 'object',
|
|
464
|
+
properties: defSchema.properties || {},
|
|
465
|
+
required: defSchema.required || [],
|
|
466
|
+
title: defSchema.title,
|
|
467
|
+
description: defSchema.description,
|
|
468
|
+
'x-isForm': defSchema['x-isForm'],
|
|
469
|
+
};
|
|
470
|
+
result += processObject(rootDefObject, [defName], 1, schemaObj, pad);
|
|
471
|
+
}
|
|
472
|
+
else if (defSchema.type === 'string' && defSchema.enum) {
|
|
473
|
+
result += generateEnum(defSchema, defName, 1, pad);
|
|
474
|
+
}
|
|
475
|
+
else if (defSchema.anyOf || defSchema.oneOf || defSchema.allOf) {
|
|
476
|
+
result += generateVariantEnum(defSchema, defName, [defName], 1, schemaObj, pad);
|
|
477
|
+
}
|
|
478
|
+
else if (defSchema.type && ['string', 'number', 'integer', 'boolean', 'null'].includes(defSchema.type)) {
|
|
479
|
+
// Handle primitive types in $defs
|
|
480
|
+
result += processPrimitive(defSchema, defName, 1, pad);
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
});
|
|
484
|
+
}
|
|
485
|
+
// Handle the schema based on its type
|
|
486
|
+
if (schemaObj.type === 'object' || schemaObj.properties) {
|
|
487
|
+
// Create a root object for object schema
|
|
488
|
+
const rootObject = {
|
|
489
|
+
type: 'object',
|
|
490
|
+
properties: schemaObj.properties || {},
|
|
491
|
+
required: schemaObj.required || [],
|
|
492
|
+
title: schemaObj.title,
|
|
493
|
+
description: schemaObj.description,
|
|
494
|
+
'x-isForm': schemaObj['x-isForm'],
|
|
495
|
+
};
|
|
496
|
+
result += processObject(rootObject, [schemaName], 1, schemaObj, pad);
|
|
497
|
+
}
|
|
498
|
+
else if (['string', 'number', 'integer', 'boolean', 'null'].includes(schemaObj.type)) {
|
|
499
|
+
// Handle primitive schema
|
|
500
|
+
result += processPrimitive(schemaObj, schemaName, 1, pad);
|
|
501
|
+
}
|
|
502
|
+
else if (schemaObj.enum) {
|
|
503
|
+
// Handle enum schema
|
|
504
|
+
result += generateEnum(schemaObj, schemaName, 1, pad);
|
|
505
|
+
}
|
|
506
|
+
else if (schemaObj.anyOf || schemaObj.oneOf || schemaObj.allOf) {
|
|
507
|
+
// Handle variant schema
|
|
508
|
+
result += generateVariantEnum(schemaObj, schemaName, [schemaName], 1, schemaObj, pad);
|
|
509
|
+
}
|
|
510
|
+
else if (schemaObj.type === 'array') {
|
|
511
|
+
// For array as root type, create a type alias to Vec<ItemType>
|
|
512
|
+
let itemType = 'String'; // Default if no items specified
|
|
513
|
+
if (schemaObj.items) {
|
|
514
|
+
if (schemaObj.items.type === 'object' || schemaObj.items.properties) {
|
|
515
|
+
// Create the item type
|
|
516
|
+
const itemSchema = {
|
|
517
|
+
...schemaObj.items,
|
|
518
|
+
title: schemaObj.items.title || `${schemaName}Item`,
|
|
519
|
+
description: schemaObj.items.description || `Item of ${schemaName} array`,
|
|
520
|
+
};
|
|
521
|
+
result += processObject(itemSchema, [`${schemaName}Item`], 1, schemaObj, pad);
|
|
522
|
+
itemType = `${schemaName}Item`;
|
|
523
|
+
}
|
|
524
|
+
else {
|
|
525
|
+
// For primitive array items
|
|
526
|
+
itemType = toRustType(schemaObj.items, [`${schemaName}Item`], schemaObj);
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
result += `${indentFn(1)}pub type ${schemaName} = Vec<${itemType}>;\n\n`;
|
|
530
|
+
}
|
|
531
|
+
});
|
|
532
|
+
result += `${indentFn(0)}}\n`;
|
|
533
|
+
return result;
|
|
534
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "vovk-rust",
|
|
3
|
+
"version": "0.0.1-beta.2",
|
|
4
|
+
"description": "Vovk.ts Rust client",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"build": "tsc",
|
|
7
|
+
"npm-publish": "npm publish",
|
|
8
|
+
"tsc": "tsc --noEmit",
|
|
9
|
+
"ncu": "npm-check-updates -u",
|
|
10
|
+
"pre-test": "npm run build",
|
|
11
|
+
"test-only": "npm run pre-test && node --experimental-strip-types --test --test-only test/test/**/*.mts",
|
|
12
|
+
"unit": "npm run pre-test && node --experimental-strip-types --test --test-concurrency=1 test/*.mts",
|
|
13
|
+
"unit:rs": "RUST_BACKTRACE=full cargo test --manifest-path ./test_rust/Cargo.toml -- --show-output",
|
|
14
|
+
"unit:ts": "npm run pre-test && node --experimental-strip-types --test --test-concurrency=1 test_ts/*.mts",
|
|
15
|
+
"generate": "npm run generate --prefix ../../test -- --config=../packages/vovk-rust/vovk.config.test.mjs",
|
|
16
|
+
"test": "npm run unit:ts && PORT=3210 concurrently 'sleep 60 && npm run unit:rs' 'npm run generate && npm run build --prefix ../../test && npm run start --prefix ../../test' --kill-others --success first"
|
|
17
|
+
},
|
|
18
|
+
"main": "./index.js",
|
|
19
|
+
"type": "module",
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "git+https://github.com/finom/vovk.git"
|
|
23
|
+
},
|
|
24
|
+
"keywords": [
|
|
25
|
+
"Vovk"
|
|
26
|
+
],
|
|
27
|
+
"author": "Andrey Gubanov",
|
|
28
|
+
"license": "MIT",
|
|
29
|
+
"bugs": {
|
|
30
|
+
"url": "https://github.com/finom/vovk/issues"
|
|
31
|
+
},
|
|
32
|
+
"homepage": "https://github.com/finom/vovk#readme"
|
|
33
|
+
}
|