Skip to main content

olai_codegen/
lib.rs

1//! Code generation from protobuf descriptors.
2//!
3//! `proto-gen` turns compiled protobuf descriptor bytes into Rust REST-API glue code
4//! (Axum handlers, HTTP clients, PyO3 bindings, NAPI bindings, and TypeScript clients).
5//!
6//! ## Pipeline overview
7//!
8//! ```text
9//! descriptor bytes
10//!      │
11//!      ▼
12//! parse_file_descriptor_set   ← parsing module
13//!      │  CodeGenMetadata
14//!      ▼
15//! analyze_metadata            ← analysis module
16//!      │  GenerationPlan
17//!      ▼
18//! generate_code               ← codegen module
19//!      │  writes files to CodeGenOutput dirs
20//!      ▼
21//! (generated Rust / Python / TypeScript source files)
22//! ```
23//!
24//! ## Quick-start example
25//!
26//! ```rust,ignore
27//! use std::fs;
28//! use olai_codegen::{CodeGenConfig, CodeGenOutput, generate_code, parse_file_descriptor_set};
29//! use protobuf::Message;
30//! use protobuf::descriptor::FileDescriptorSet;
31//!
32//! // 1. Load descriptor bytes (produced by `buf build`)
33//! let bytes = fs::read("descriptors.bin").unwrap();
34//! let fds = FileDescriptorSet::parse_from_bytes(&bytes).unwrap();
35//!
36//! // 2. Parse into metadata
37//! let metadata = parse_file_descriptor_set(&fds).unwrap();
38//!
39//! // 3. Configure outputs
40//! let output = CodeGenOutput {
41//!     common: "/tmp/out/common".into(),
42//!     models: Some("/tmp/out/models".into()),
43//!     models_subdir: "_gen".into(),
44//!     server: Some("/tmp/out/server".into()),
45//!     client: Some("/tmp/out/client".into()),
46//!     python: None,
47//!     node: None,
48//!     node_ts: None,
49//!     wasm: None,
50//!     python_typings_filename: "client.pyi".into(),
51//!     generate_resource_clients: false,
52//! };
53//!
54//! let config = CodeGenConfig {
55//!     context_type_path: "crate::api::RequestContext".into(),
56//!     result_type_path: "crate::Result".into(),
57//!     models_path_template: "my_crate::models::{service}::v1".into(),
58//!     models_path_crate_template: "crate::models::{service}::v1".into(),
59//!     output,
60//!     generate_resource_enum: true,
61//!     generate_store_integration: false,
62//!     error_type_path: None,
63//!     generate_object_conversions: false,
64//!     bindings: None,
65//!     models_gen_dir: None,
66//!     resource_store_crate_name: "olai_store".into(),
67//!     runtime: olai_codegen::Runtime::Prost,
68//!     transport_type_path: olai_codegen::DEFAULT_TRANSPORT_TYPE_PATH.into(),
69//! };
70//!
71//! // 4. Optionally validate before running
72//! config.validate().unwrap();
73//!
74//! // 5. Generate
75//! generate_code(&metadata, &config).unwrap();
76//! ```
77
78pub use error::*;
79
80pub mod analysis;
81pub mod codegen;
82pub mod error;
83pub mod openapi_enrich;
84pub mod output;
85pub mod parsing;
86pub mod utils;
87
88pub use codegen::{
89    BindingsConfig, CodeGenConfig, CodeGenOutput, DEFAULT_TRANSPORT_TYPE_PATH, GeneratedCode,
90    Runtime, generate_code, generate_models_mod,
91};
92
93pub use analysis::{
94    BodyField, GenerationPlan, ManagedResource, MethodPlan, PathParam, QueryParam, RequestParam,
95    RequestType, ResourceHierarchy, ServicePlan, SkippedMethod, analyze_metadata,
96    extract_managed_resources, split_body_fields,
97};
98// Note: MethodPlanner is pub(crate) — it is an internal helper, not part of the public API.
99pub use openapi_enrich::run as enrich_openapi;
100pub use parsing::http::HttpPattern;
101pub use parsing::types::{BaseType, RenderContext, UnifiedType};
102pub use parsing::{CodeGenMetadata, parse_file_descriptor_set, process_file_descriptor};
103
104/// The `FieldBehavior` enum from `google.api.field_behavior`, re-exported for
105/// consumers that need to inspect field behavior annotations (e.g. in tests).
106pub use google::api::FieldBehavior;
107
108// Prost-generated Google API proto types — internal only.
109pub(crate) mod google {
110    pub mod api {
111        #![allow(unused)]
112        #![allow(clippy::doc_overindented_list_items)]
113        #![allow(clippy::doc_lazy_continuation)]
114        include!("./gen/google.api.rs");
115    }
116}
117
118pub(crate) mod gnostic {
119    pub mod openapi {
120        pub mod v3 {
121            #![allow(unused)]
122            #![allow(clippy::large_enum_variant)]
123            include!("./gen/gnostic.openapi.v3.rs");
124        }
125    }
126}