Skip to main content

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;
48#[cfg(feature = "cli")]
49pub mod cli;
50pub mod client_generator;
51pub mod config;
52pub mod error;
53pub mod extensions;
54pub mod generator;
55pub mod http_config;
56#[cfg(feature = "http-error")]
57pub mod http_error;
58pub mod openapi;
59pub mod patterns;
60pub mod registry_generator;
61pub mod server;
62pub mod spec_source;
63pub mod streaming;
64pub mod type_mapping;
65
66/// Helpers for generator snapshot and scratch-crate tests.
67///
68/// This module is excluded from default builds so its test-only dependencies
69/// do not become part of the installed CLI. Enable the `test-helpers` feature
70/// when using these helpers outside this repository's test suite.
71#[cfg(feature = "test-helpers")]
72pub mod test_helpers;
73
74/// Crate version, exposed so embedders (e.g. the WASM playground) can report
75/// which generator produced their output.
76pub const VERSION: &str = env!("CARGO_PKG_VERSION");
77
78pub use analysis::{SchemaAnalysis, SchemaAnalyzer, merge_schema_extensions};
79pub use config::ConfigFile;
80pub use error::GeneratorError;
81pub use generator::{CodeGenerator, GeneratedFile, GenerationResult, GeneratorConfig};
82pub use http_config::{AuthConfig, HttpClientConfig, RetryConfig};
83#[cfg(feature = "http-error")]
84pub use http_error::{ApiError, ApiOpError, HttpError, HttpResult};
85pub use openapi::{OpenApiSpec, Schema, SchemaType};
86pub use type_mapping::{
87    ByteStrategy, DepRequirement, MappedType, TypeFeature, TypeMapper, TypeMappingConfig,
88    UsedFeatures,
89};
90
91pub type Result<T> = std::result::Result<T, GeneratorError>;