openapi_to_rust/lib.rs
1//! Generate typed Rust models, async HTTP/SSE clients, and opt-in Axum server
2//! scaffolding from OpenAPI 3.0 and 3.1 documents (with experimental 3.2
3//! parsing).
4//!
5//! Most users should install the `openapi-to-rust` CLI and start with
6//! `openapi-to-rust generate <SOURCE>`. The library API is useful when a build
7//! tool or application needs to analyze and render a document in memory.
8//!
9//! # Library example
10//!
11//! ```
12//! use openapi_to_rust::{CodeGenerator, GeneratorConfig, SchemaAnalyzer};
13//! use serde_json::json;
14//!
15//! # fn main() -> openapi_to_rust::Result<()> {
16//! let spec = json!({
17//! "openapi": "3.1.0",
18//! "info": { "title": "Example", "version": "1.0.0" },
19//! "paths": {},
20//! "components": {
21//! "schemas": {
22//! "Greeting": {
23//! "type": "object",
24//! "required": ["message"],
25//! "properties": { "message": { "type": "string" } }
26//! }
27//! }
28//! }
29//! });
30//!
31//! let mut analyzer = SchemaAnalyzer::new(spec)?;
32//! let mut analysis = analyzer.analyze()?;
33//! let generator = CodeGenerator::new(GeneratorConfig {
34//! enable_async_client: false,
35//! ..GeneratorConfig::default()
36//! });
37//! let source = generator.generate(&mut analysis)?;
38//! assert!(source.contains("pub struct Greeting"));
39//! # Ok(())
40//! # }
41//! ```
42//!
43//! Configuration is documented in [`config`]. Generated dependency fragments
44//! are represented by [`DepRequirement`], and generated files can be written
45//! with [`CodeGenerator::write_files`].
46
47pub mod analysis;
48pub mod cli;
49pub mod client_generator;
50pub mod config;
51pub mod error;
52pub mod extensions;
53pub mod generator;
54pub mod http_config;
55pub mod http_error;
56pub mod openapi;
57pub mod patterns;
58pub mod registry_generator;
59pub mod server;
60pub mod streaming;
61pub mod type_mapping;
62
63/// Helpers for generator snapshot and scratch-crate tests.
64///
65/// This module is excluded from default builds so its test-only dependencies
66/// do not become part of the installed CLI. Enable the `test-helpers` feature
67/// when using these helpers outside this repository's test suite.
68#[cfg(feature = "test-helpers")]
69pub mod test_helpers;
70
71pub use analysis::{SchemaAnalysis, SchemaAnalyzer, merge_schema_extensions};
72pub use config::ConfigFile;
73pub use error::GeneratorError;
74pub use generator::{CodeGenerator, GeneratedFile, GenerationResult, GeneratorConfig};
75pub use http_config::{AuthConfig, HttpClientConfig, RetryConfig};
76pub use http_error::{ApiError, ApiOpError, HttpError, HttpResult};
77pub use openapi::{OpenApiSpec, Schema, SchemaType};
78pub use type_mapping::{
79 ByteStrategy, DepRequirement, MappedType, TypeFeature, TypeMapper, TypeMappingConfig,
80 UsedFeatures,
81};
82
83pub type Result<T> = std::result::Result<T, GeneratorError>;