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 utils;
56
57pub use codegen::{
58    BindingsConfig, ClientProtocol, ClientProtocols, CodeGenConfig, CodeGenOutput,
59    DEFAULT_TRANSPORT_TYPE_PATH, GeneratedCode, PythonApi, Runtime, generate_code,
60    generate_models_mod,
61};
62
63pub use analysis::{
64    BodyField, GenerationPlan, ManagedResource, MethodPlan, PathParam, QueryParam, RequestParam,
65    RequestType, ResourceHierarchy, ServicePlan, SkippedMethod, analyze_metadata,
66    extract_managed_resources, split_body_fields,
67};
68// Note: MethodPlanner is pub(crate) — it is an internal helper, not part of the public API.
69pub use openapi_enrich::run as enrich_openapi;
70pub use parsing::http::HttpPattern;
71pub use parsing::types::{BaseType, RenderContext, UnifiedType};
72pub use parsing::{CodeGenMetadata, parse_file_descriptor_set, process_file_descriptor};
73
74/// The `FieldBehavior` enum from `google.api.field_behavior`, re-exported for
75/// consumers that need to inspect field behavior annotations (e.g. in tests).
76pub use google::api::FieldBehavior;
77
78// Prost-generated Google API proto types — internal only.
79pub(crate) mod google {
80    pub mod api {
81        #![allow(unused)]
82        #![allow(clippy::doc_overindented_list_items)]
83        #![allow(clippy::doc_lazy_continuation)]
84        include!("./gen/google.api.rs");
85    }
86}
87
88pub(crate) mod gnostic {
89    pub mod openapi {
90        pub mod v3 {
91            #![allow(unused)]
92            #![allow(clippy::large_enum_variant)]
93            include!("./gen/gnostic.openapi.v3.rs");
94        }
95    }
96}