UniStructGen OpenAPI Parser
OpenAPI 3.0/3.1 parser for UniStructGen that generates type-safe Rust types from OpenAPI specifications.
Status: parsing and type generation are production-grade; client generation is a scaffold and may require manual adjustments for edge cases.
Features
✨ Comprehensive OpenAPI Support
- OpenAPI 3.0 and 3.1 specifications
- Both YAML and JSON formats
- Schema composition (allOf, oneOf, anyOf)
- Reference ($ref) resolution
- Nested object structures
🎯 Smart Type Generation
- Automatic type inference (UUID, DateTime, etc.)
- Enum generation from string enums
- Validation constraint extraction
- Array and map support
- Optional field detection
🔐 API Client Generation (scaffold)
- Generate client traits and types
- Basic parameter extraction
- Method signatures (best-effort)
✅ Validation Support (best-effort)
- Extracts validation metadata for downstream codegen
- Min/max length constraints
- Range validation
- Format validation (email, URL, etc.)
- Pattern matching
Installation
Add to your Cargo.toml:
[]
= "0.1"
= "0.1"
= "0.1"
# For special types
= { = "1.0", = ["serde", "v4"] }
= { = "0.4", = ["serde"] }
Usage
As a Library
use Parser;
use ;
use RustRenderer;
// Configure the parser
let options = builder
.generate_client
.generate_validation
.derive_serde
.build;
let mut parser = new;
// Parse OpenAPI spec
let spec = read_to_string?;
let ir_module = parser.parse?;
// Generate Rust code
let renderer = new;
let rust_code = renderer.generate?;
println!;
With Proc Macro
Add to your Cargo.toml:
[]
= "0.1"
Then in your code:
use openapi_to_rust;
// From file
openapi_to_rust!
// From URL
openapi_to_rust!
// From inline spec
openapi_to_rust!
// Now use the generated types
let user = User ;
Configuration Options
Parser Options
let options = builder
// Generate API client traits (default: true)
.generate_client
// Add validation derives (default: true)
.generate_validation
// Add serde derives (default: true)
.derive_serde
// Add Default derive (default: false)
.derive_default
// Make all fields optional (default: false)
.make_fields_optional
// Maximum depth for nested schemas (default: 10)
.max_depth
// Filter by tags
.include_tags
// Exclude tags
.exclude_tags
// Generate builder patterns (default: false)
.generate_builders
// Generate documentation (default: true)
.generate_docs
// Type name prefix/suffix
.type_prefix
.type_suffix
// Module name (default: "api")
.module_name
.build;
Examples
Example 1: Pet Store API
# petstore.yaml
openapi: 3.0.0
info:
title: Pet Store
version: 1.0.0
components:
schemas:
Pet:
type: object
required:
- id
- name
properties:
id:
type: string
format: uuid
name:
type: string
minLength: 1
maxLength: 100
status:
type: string
enum:
- available
- pending
- sold
age:
type: integer
minimum: 0
maximum: 50
use openapi_to_rust;
openapi_to_rust!
// Generated code includes:
// - Pet struct with validation
// - PetStatus enum
// - Serde derives for JSON serialization
Example 2: User Management API
openapi_to_rust!
// Use the generated types
let user = User ;
Example 3: With Authentication
// Fetch from authenticated endpoint
openapi_to_rust!
Generated Code Examples
Input Schema
User:
type: object
required:
- id
- username
- email
properties:
id:
type: integer
format: int64
username:
type: string
minLength: 3
maxLength: 20
email:
type: string
format: email
age:
type: integer
minimum: 0
maximum: 150
roles:
type: array
items:
type: string
Generated Rust Code
Type Mappings
| OpenAPI Type | Format | Rust Type |
|---|---|---|
string |
- | String |
string |
date-time |
chrono::DateTime<Utc> |
string |
uuid |
uuid::Uuid |
string |
email |
String (with validation) |
string |
uri, url |
String |
integer |
int32 |
i32 |
integer |
int64 |
i64 |
number |
float |
f32 |
number |
double |
f64 |
boolean |
- | bool |
array |
- | Vec<T> |
object |
- | Nested struct or HashMap |
Advanced Features
Schema Composition
# allOf - merge schemas
User:
allOf:
- type: object
properties:
id:
type: integer
- type: object
properties:
name:
type: string
# oneOf - enum variants
Response:
oneOf:
- $ref: '#/components/schemas/SuccessResponse'
- $ref: '#/components/schemas/ErrorResponse'
Reference Resolution
Address:
type: object
properties:
street:
type: string
User:
type: object
properties:
homeAddress:
$ref: '#/components/schemas/Address'
workAddress:
$ref: '#/components/schemas/Address'
Validation Constraints
Username:
type: string
minLength: 3
maxLength: 20
pattern: '^[a-zA-Z0-9_]+$'
Age:
type: integer
minimum: 0
maximum: 150
Tags:
type: array
minItems: 1
maxItems: 10
Error Handling
The parser provides detailed error messages:
use OpenApiError;
match parser.parse
Testing
Run the tests:
Run examples:
Contributing
Contributions are welcome! Please see CONTRIBUTING.md for guidelines.
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE)
- MIT License (LICENSE-MIT)
at your option.