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