Expand description
TOML configuration file support for OpenAPI code generation.
This module provides TOML-based configuration as an alternative to the Rust API. It enables CLI-based code generation without requiring the generator as a build dependency.
§Overview
The TOML configuration system provides:
- Declarative configuration in
openapi-to-rust.tomlfiles - Comprehensive validation with helpful error messages
- Support for all generator features (HTTP client, retry, tracing, Specta)
- Conversion to internal
GeneratorConfigfor code generation
§Quick Start
Create an openapi-to-rust.toml file:
[generator]
spec_path = "openapi.json"
output_dir = "src/generated"
module_name = "api"
[features]
enable_async_client = true
[http_client]
base_url = "https://api.example.com"
timeout_seconds = 30
[http_client.retry]
max_retries = 3
initial_delay_ms = 500
max_delay_ms = 16000Load and use the configuration:
use openapi_to_rust::config::ConfigFile;
use std::path::Path;
// Load configuration from TOML file
let config_file = ConfigFile::load(Path::new("openapi-to-rust.toml"))?;
// Convert to internal GeneratorConfig
let generator_config = config_file.into_generator_config();
// Use with CodeGenerator...§Configuration Sections
§Generator Section (Required)
[generator]
spec_path = "openapi.json" # Path to OpenAPI spec
output_dir = "src/generated" # Output directory
module_name = "api" # Module name§Features Section (Optional)
[features]
enable_sse_client = true # Generate SSE streaming client
enable_async_client = true # Generate HTTP REST client
enable_specta = false # Add specta::Type derives§HTTP Client Section (Optional)
[http_client]
base_url = "https://api.example.com"
timeout_seconds = 30
[http_client.retry]
max_retries = 3 # 0-10 retries
initial_delay_ms = 500 # 100-10000ms
max_delay_ms = 16000 # 1000-300000ms
[http_client.tracing]
enabled = true # Enable request tracing (default: true)
[http_client.auth]
type = "Bearer" # Bearer, ApiKey, or Custom
header_name = "Authorization"
[[http_client.headers]]
name = "content-type"
value = "application/json"§Validation
The configuration is validated on load using the validator crate:
- File paths are checked for existence
- Numeric ranges are enforced (timeout, retry counts, delays)
- Enum values are validated (auth types, event flow types)
- Required fields are checked
Invalid configurations produce helpful error messages:
Configuration validation failed:
- generator.spec_path: OpenAPI spec file not found: missing.json
- http_client.retry.max_retries: max_retries must be between 0 and 10§Examples
See the examples directory for complete examples:
toml_config_example.rs- Various configuration patternscomplete_workflow.rs- Full generation workflow with TOML
§Backward Compatibility
The TOML configuration is fully optional. The existing Rust API continues to work:
use openapi_to_rust::{GeneratorConfig, CodeGenerator};
use std::path::PathBuf;
let config = GeneratorConfig {
spec_path: PathBuf::from("openapi.json"),
enable_async_client: true,
// ... other fields
..Default::default()
};
let generator = CodeGenerator::new(config);
// ... generate code