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