openapi-to-rust 0.1.15

Generate strongly-typed Rust structs, HTTP clients, and SSE streaming clients from OpenAPI 3.1 specifications
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
//! 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.toml` files
//! - Comprehensive validation with helpful error messages
//! - Support for all generator features (HTTP client, retry, tracing, Specta)
//! - Conversion to internal [`GeneratorConfig`] for code generation
//!
//! # Quick Start
//!
//! Create an `openapi-to-rust.toml` file:
//!
//! ```toml
//! [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 = 16000
//! ```
//!
//! Load and use the configuration:
//!
//! ```no_run
//! use openapi_to_rust::config::ConfigFile;
//! use std::path::Path;
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // 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...
//! # Ok(())
//! # }
//! ```
//!
//! # Configuration Sections
//!
//! ## Generator Section (Required)
//!
//! ```toml
//! [generator]
//! spec_path = "openapi.json"       # Path to OpenAPI spec
//! output_dir = "src/generated"     # Output directory
//! module_name = "api"              # Module name
//! ```
//!
//! ## Features Section (Optional)
//!
//! ```toml
//! [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)
//!
//! ```toml
//! [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:
//!
//! ```text
//! 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](https://github.com/your-repo/examples) directory for complete examples:
//! - `toml_config_example.rs` - Various configuration patterns
//! - `complete_workflow.rs` - Full generation workflow with TOML
//!
//! # Backward Compatibility
//!
//! The TOML configuration is fully optional. The existing Rust API continues to work:
//!
//! ```no_run
//! 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
//! ```

use crate::{GeneratorError, generator::GeneratorConfig};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use validator::Validate;

/// Root configuration loaded from TOML file
#[derive(Debug, Clone, Deserialize, Serialize, Validate)]
pub struct ConfigFile {
    #[validate(nested)]
    pub generator: GeneratorSection,
    #[validate(nested)]
    pub features: FeaturesSection,
    #[serde(default)]
    #[validate(nested)]
    pub http_client: Option<HttpClientSection>,
    #[serde(default)]
    #[validate(nested)]
    pub streaming: Option<StreamingSection>,
    #[serde(default)]
    pub nullable_overrides: BTreeMap<String, bool>,
    #[serde(default)]
    pub type_mappings: BTreeMap<String, String>,
}

#[derive(Debug, Clone, Deserialize, Serialize, Validate)]
pub struct GeneratorSection {
    #[validate(custom(function = "validate_spec_path_exists"))]
    pub spec_path: PathBuf,
    pub output_dir: PathBuf,
    #[validate(length(min = 1, message = "module_name cannot be empty"))]
    pub module_name: String,
    /// Schema extension files to merge into the main spec before codegen.
    /// Paths are relative to the working directory (same as spec_path).
    #[serde(default)]
    pub schema_extensions: Vec<PathBuf>,
}

#[derive(Debug, Clone, Deserialize, Serialize, Validate)]
pub struct FeaturesSection {
    #[serde(default)]
    pub enable_sse_client: bool,
    #[serde(default)]
    pub enable_async_client: bool,
    #[serde(default)]
    pub enable_specta: bool,
    /// Generate a static operation registry with metadata for CLI/proxy routing
    #[serde(default)]
    pub enable_registry: bool,
    /// Generate only the operation registry (skip types, client, streaming)
    #[serde(default)]
    pub registry_only: bool,
}

#[derive(Debug, Clone, Deserialize, Serialize, Validate)]
pub struct HttpClientSection {
    #[validate(url(message = "base_url must be a valid URL"))]
    pub base_url: Option<String>,
    #[validate(custom(function = "validate_timeout_seconds"))]
    pub timeout_seconds: Option<u64>,
    #[validate(nested)]
    pub auth: Option<AuthConfigSection>,
    #[serde(default)]
    #[validate(nested)]
    pub headers: Vec<HeaderEntry>,
    #[validate(nested)]
    pub retry: Option<RetryConfigSection>,
    #[validate(nested)]
    pub tracing: Option<TracingConfigSection>,
}

#[derive(Debug, Clone, Deserialize, Serialize, Validate)]
pub struct TracingConfigSection {
    #[serde(default = "default_tracing_enabled")]
    pub enabled: bool,
}

fn default_tracing_enabled() -> bool {
    true
}

#[derive(Debug, Clone, Deserialize, Serialize, Validate)]
pub struct RetryConfigSection {
    #[serde(default = "default_max_retries")]
    #[validate(custom(function = "validate_max_retries"))]
    pub max_retries: u32,
    #[serde(default = "default_initial_delay_ms")]
    #[validate(custom(function = "validate_initial_delay_ms"))]
    pub initial_delay_ms: u64,
    #[serde(default = "default_max_delay_ms")]
    #[validate(custom(function = "validate_max_delay_ms"))]
    pub max_delay_ms: u64,
}

fn default_max_retries() -> u32 {
    3
}
fn default_initial_delay_ms() -> u64 {
    500
}
fn default_max_delay_ms() -> u64 {
    16000
}

#[derive(Debug, Clone, Deserialize, Serialize, Validate)]
pub struct AuthConfigSection {
    #[serde(rename = "type")]
    #[validate(custom(function = "validate_auth_type"))]
    pub auth_type: String,
    #[validate(length(min = 1, message = "header_name cannot be empty"))]
    pub header_name: String,
}

#[derive(Debug, Clone, Deserialize, Serialize, Validate)]
pub struct HeaderEntry {
    #[validate(length(min = 1, message = "header name cannot be empty"))]
    pub name: String,
    pub value: String,
}

#[derive(Debug, Clone, Deserialize, Serialize, Validate)]
pub struct StreamingSection {
    #[validate(nested)]
    pub endpoints: Vec<StreamingEndpointSection>,
}

#[derive(Debug, Clone, Deserialize, Serialize, Validate)]
pub struct StreamingEndpointSection {
    #[validate(length(min = 1))]
    pub operation_id: String,
    #[validate(length(min = 1))]
    pub path: String,
    /// HTTP method: "GET" or "POST" (default: POST)
    #[serde(default)]
    pub http_method: Option<String>,
    /// Parameter name that controls streaming (only for POST requests)
    #[serde(default)]
    pub stream_parameter: String,
    /// Query parameters for GET requests
    #[serde(default)]
    pub query_parameters: Vec<QueryParameterSection>,
    #[validate(length(min = 1))]
    pub event_union_type: String,
    pub content_type: Option<String>,
    #[validate(nested)]
    pub event_flow: Option<EventFlowSection>,
}

#[derive(Debug, Clone, Deserialize, Serialize, Validate)]
pub struct QueryParameterSection {
    #[validate(length(min = 1))]
    pub name: String,
    #[serde(default)]
    pub required: bool,
}

#[derive(Debug, Clone, Deserialize, Serialize, Validate)]
pub struct EventFlowSection {
    #[serde(rename = "type")]
    #[validate(custom(function = "validate_event_flow_type"))]
    pub flow_type: String,
    pub start_events: Option<Vec<String>>,
    pub delta_events: Option<Vec<String>>,
    pub stop_events: Option<Vec<String>>,
}

// Custom validators
fn validate_spec_path_exists(path: &Path) -> Result<(), validator::ValidationError> {
    if !path.exists() {
        let mut error = validator::ValidationError::new("file_not_found");
        error.message = Some(
            format!(
                "OpenAPI spec file not found: {}. Ensure spec_path points to a valid OpenAPI JSON or YAML file.",
                path.display()
            )
            .into(),
        );
        return Err(error);
    }
    Ok(())
}

fn validate_auth_type(auth_type: &str) -> Result<(), validator::ValidationError> {
    match auth_type {
        "Bearer" | "ApiKey" | "Custom" => Ok(()),
        _ => {
            let mut error = validator::ValidationError::new("invalid_auth_type");
            error.message = Some(
                format!(
                    "Invalid auth type '{}'. Must be one of: Bearer, ApiKey, Custom",
                    auth_type
                )
                .into(),
            );
            Err(error)
        }
    }
}

fn validate_event_flow_type(flow_type: &str) -> Result<(), validator::ValidationError> {
    match flow_type {
        "StartDeltaStop" | "Continuous" => Ok(()),
        _ => {
            let mut error = validator::ValidationError::new("invalid_event_flow_type");
            error.message = Some(
                format!(
                    "Invalid event flow type '{}'. Must be one of: StartDeltaStop, Continuous",
                    flow_type
                )
                .into(),
            );
            Err(error)
        }
    }
}

fn validate_timeout_seconds(timeout: u64) -> Result<(), validator::ValidationError> {
    if !(1..=3600).contains(&timeout) {
        let mut error = validator::ValidationError::new("out_of_range");
        error.message = Some("timeout_seconds must be between 1 and 3600".into());
        return Err(error);
    }
    Ok(())
}

fn validate_max_retries(retries: u32) -> Result<(), validator::ValidationError> {
    if retries > 10 {
        let mut error = validator::ValidationError::new("out_of_range");
        error.message = Some("max_retries must be between 0 and 10".into());
        return Err(error);
    }
    Ok(())
}

fn validate_initial_delay_ms(delay: u64) -> Result<(), validator::ValidationError> {
    if !(100..=10000).contains(&delay) {
        let mut error = validator::ValidationError::new("out_of_range");
        error.message = Some("initial_delay_ms must be between 100 and 10000".into());
        return Err(error);
    }
    Ok(())
}

fn validate_max_delay_ms(delay: u64) -> Result<(), validator::ValidationError> {
    if !(1000..=300000).contains(&delay) {
        let mut error = validator::ValidationError::new("out_of_range");
        error.message = Some("max_delay_ms must be between 1000 and 300000".into());
        return Err(error);
    }
    Ok(())
}

impl ConfigFile {
    /// Load and validate configuration from TOML file
    pub fn load(path: &Path) -> Result<Self, GeneratorError> {
        let content = std::fs::read_to_string(path).map_err(|e| GeneratorError::FileError {
            message: format!("Failed to read config file '{}': {}", path.display(), e),
        })?;

        let config: ConfigFile =
            toml::from_str(&content).map_err(|e| GeneratorError::FileError {
                message: format!(
                    "Failed to parse TOML config: {}\n\nExample config:\n{}",
                    e, EXAMPLE_CONFIG
                ),
            })?;

        // Validate the configuration
        config.validate().map_err(|e| {
            GeneratorError::ValidationError(format!(
                "Configuration validation failed:\n{}",
                format_validation_errors(&e)
            ))
        })?;

        Ok(config)
    }

    /// Convert to internal GeneratorConfig
    pub fn into_generator_config(self) -> GeneratorConfig {
        use crate::http_config::{AuthConfig, HttpClientConfig, RetryConfig};

        // Convert HTTP client config
        let http_client_config = self.http_client.as_ref().map(|http| HttpClientConfig {
            base_url: http.base_url.clone(),
            timeout_seconds: http.timeout_seconds,
            default_headers: http
                .headers
                .iter()
                .map(|h| (h.name.clone(), h.value.clone()))
                .collect(),
        });

        // Convert retry config
        let retry_config = self
            .http_client
            .as_ref()
            .and_then(|http| http.retry.as_ref())
            .map(|retry| RetryConfig {
                max_retries: retry.max_retries,
                initial_delay_ms: retry.initial_delay_ms,
                max_delay_ms: retry.max_delay_ms,
            });

        // Convert tracing config
        let tracing_enabled = self
            .http_client
            .as_ref()
            .and_then(|http| http.tracing.as_ref())
            .map(|tracing| tracing.enabled)
            .unwrap_or(true);

        // Convert auth config
        let auth_config = self
            .http_client
            .as_ref()
            .and_then(|http| http.auth.as_ref())
            .map(|auth| match auth.auth_type.as_str() {
                "Bearer" => AuthConfig::Bearer {
                    header_name: auth.header_name.clone(),
                },
                "ApiKey" => AuthConfig::ApiKey {
                    header_name: auth.header_name.clone(),
                },
                "Custom" => AuthConfig::Custom {
                    header_name: auth.header_name.clone(),
                    header_value_prefix: None,
                },
                _ => AuthConfig::Bearer {
                    header_name: "Authorization".to_string(),
                },
            });

        // Convert streaming section to StreamingConfig
        let streaming_config = self.streaming.map(|section| {
            use crate::streaming::{
                EventFlow, HttpMethod, QueryParameter, StreamingConfig, StreamingEndpoint,
            };

            let endpoints = section
                .endpoints
                .into_iter()
                .map(|e| {
                    let event_flow = e
                        .event_flow
                        .map(|ef| match ef.flow_type.as_str() {
                            "start_delta_stop" => EventFlow::StartDeltaStop {
                                start_events: ef.start_events.unwrap_or_default(),
                                delta_events: ef.delta_events.unwrap_or_default(),
                                stop_events: ef.stop_events.unwrap_or_default(),
                            },
                            _ => EventFlow::Simple,
                        })
                        .unwrap_or(EventFlow::Simple);

                    let http_method = e
                        .http_method
                        .map(|m| match m.to_uppercase().as_str() {
                            "GET" => HttpMethod::Get,
                            _ => HttpMethod::Post,
                        })
                        .unwrap_or(HttpMethod::Post);

                    let query_parameters = e
                        .query_parameters
                        .into_iter()
                        .map(|qp| QueryParameter {
                            name: qp.name,
                            required: qp.required,
                        })
                        .collect();

                    StreamingEndpoint {
                        operation_id: e.operation_id,
                        path: e.path,
                        http_method,
                        stream_parameter: e.stream_parameter,
                        query_parameters,
                        event_union_type: e.event_union_type,
                        content_type: e.content_type,
                        event_flow,
                        ..Default::default()
                    }
                })
                .collect();

            StreamingConfig {
                endpoints,
                ..Default::default()
            }
        });

        GeneratorConfig {
            spec_path: self.generator.spec_path,
            output_dir: self.generator.output_dir,
            module_name: self.generator.module_name,
            enable_sse_client: self.features.enable_sse_client,
            enable_async_client: self.features.enable_async_client,
            enable_specta: self.features.enable_specta,
            type_mappings: if self.type_mappings.is_empty() {
                super::generator::default_type_mappings()
            } else {
                self.type_mappings
            },
            streaming_config,
            nullable_field_overrides: self.nullable_overrides,
            schema_extensions: self.generator.schema_extensions,
            http_client_config,
            retry_config,
            tracing_enabled,
            auth_config,
            enable_registry: self.features.enable_registry,
            registry_only: self.features.registry_only,
        }
    }
}

/// Format validator errors into a readable message
fn format_validation_errors(errors: &validator::ValidationErrors) -> String {
    let mut messages = Vec::new();

    // Handle direct field errors
    for (field, field_errors) in errors.field_errors() {
        for error in field_errors {
            let msg = if let Some(message) = &error.message {
                format!("  - {}: {}", field, message)
            } else {
                format!("  - {}: validation failed (code: {})", field, error.code)
            };
            messages.push(msg);
        }
    }

    // Handle nested errors
    for (field, nested_errors) in errors.errors() {
        if let validator::ValidationErrorsKind::Struct(struct_errors) = nested_errors {
            let nested_msgs = format_validation_errors(struct_errors);
            if !nested_msgs.is_empty() {
                messages.push(format!("  - {} (nested):\n{}", field, nested_msgs));
            }
        }
    }

    messages.join("\n")
}

const EXAMPLE_CONFIG: &str = r#"[generator]
spec_path = "openapi.json"
output_dir = "src/generated"
module_name = "types"

[features]
enable_async_client = true

[http_client]
base_url = "https://api.example.com"
timeout_seconds = 30

[http_client.retry]
max_retries = 3

[http_client.auth]
type = "Bearer"
header_name = "Authorization""#;