Skip to main content

olai_codegen/
lib.rs

1//! Proto-driven code generation from compiled protobuf descriptors.
2//!
3//! Turns descriptor bytes (from `buf build`) into Rust REST-API glue code (Axum
4//! handlers, HTTP clients), resource registries, and optional PyO3 / NAPI /
5//! TypeScript bindings. Code generation is normally driven by the `trestle` CLI
6//! ([`olai-trestle`]); this is the library you embed for custom build tooling.
7//!
8//! ## Pipeline
9//!
10//! ```text
11//! descriptor bytes
12//!   └─ parse_file_descriptor_set ─▶ CodeGenMetadata
13//!        └─ generate_code(&metadata, &config) ─▶ files written to CodeGenOutput dirs
14//! ```
15//!
16//! ## Example
17//!
18//! ```rust,ignore
19//! use std::fs;
20//! use olai_codegen::{CodeGenConfig, CodeGenOutput, generate_code, parse_file_descriptor_set};
21//! use protobuf::{Message, descriptor::FileDescriptorSet};
22//!
23//! let bytes = fs::read("descriptors.bin")?;           // produced by `buf build`
24//! let fds = FileDescriptorSet::parse_from_bytes(&bytes)?;
25//! let metadata = parse_file_descriptor_set(&fds)?;
26//!
27//! let config = CodeGenConfig {
28//!     context_type_path: "crate::api::RequestContext".into(),
29//!     result_type_path: "crate::Result".into(),
30//!     models_path_template: "my_crate::models::{service}::v1".into(),
31//!     models_path_crate_template: "crate::models::{service}::v1".into(),
32//!     generate_resource_enum: true,
33//!     output: CodeGenOutput {
34//!         common: "out/common".into(),
35//!         server: Some("out/server".into()),
36//!         client: Some("out/client".into()),
37//!         ..Default::default()
38//!     },
39//!     ..Default::default()
40//! };
41//! config.validate()?;
42//! generate_code(&metadata, &config)?;
43//! ```
44//!
45//! [`olai-trestle`]: https://crates.io/crates/olai-trestle
46
47pub use error::*;
48
49pub mod analysis;
50pub mod codegen;
51pub mod error;
52pub mod openapi_enrich;
53pub mod output;
54pub mod parsing;
55pub mod postprocess;
56pub mod utils;
57
58pub use codegen::{
59    BindingsConfig, ClientHandlerImpls, ClientProtocol, ClientProtocols, CodeGenConfig,
60    CodeGenOutput, DEFAULT_TRANSPORT_TYPE_PATH, ErrorModuleConfig, GeneratedCode, PyPackageConfig,
61    PythonOptions, REQWEST_TRANSPORT_TYPE_PATH, RestClientOptions, Runtime, generate_code,
62    generate_models_mod,
63};
64
65pub use analysis::{
66    BodyField, GenerationPlan, ManagedResource, MethodPlan, PathParam, QueryParam, RequestParam,
67    RequestType, ResourceHierarchy, ServicePlan, analyze_metadata, extract_managed_resources,
68    split_body_fields,
69};
70// Note: MethodPlanner is pub(crate) — it is an internal helper, not part of the public API.
71pub use openapi_enrich::run as enrich_openapi;
72pub use parsing::http::HttpPattern;
73pub use parsing::types::{BaseType, RenderContext, UnifiedType};
74pub use parsing::{CodeGenMetadata, parse_file_descriptor_set, process_file_descriptor};
75
76/// The `FieldBehavior` enum from `google.api.field_behavior`, re-exported for
77/// consumers that need to inspect field behavior annotations (e.g. in tests).
78pub use google::api::FieldBehavior;
79
80// Prost-generated Google API proto types — internal only.
81pub(crate) mod google {
82    pub mod api {
83        #![allow(unused)]
84        #![allow(clippy::doc_overindented_list_items)]
85        #![allow(clippy::doc_lazy_continuation)]
86        include!("./gen/google.api.rs");
87    }
88}
89
90pub(crate) mod gnostic {
91    pub mod openapi {
92        pub mod v3 {
93            #![allow(unused)]
94            #![allow(clippy::large_enum_variant)]
95            include!("./gen/gnostic.openapi.v3.rs");
96        }
97    }
98}