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