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//!     python_typings_filename: "client.pyi".into(),
50//! };
51//!
52//! let config = CodeGenConfig {
53//!     context_type_path: "crate::api::RequestContext".into(),
54//!     result_type_path: "crate::Result".into(),
55//!     models_path_template: "my_crate::models::{service}::v1".into(),
56//!     models_path_crate_template: "crate::models::{service}::v1".into(),
57//!     output,
58//!     generate_resource_enum: true,
59//!     generate_store_integration: false,
60//!     error_type_path: None,
61//!     generate_object_conversions: false,
62//!     bindings: None,
63//!     models_gen_dir: None,
64//!     resource_store_crate_name: "olai_store".into(),
65//! };
66//!
67//! // 4. Optionally validate before running
68//! config.validate().unwrap();
69//!
70//! // 5. Generate
71//! generate_code(&metadata, &config).unwrap();
72//! ```
73
74pub use error::*;
75
76pub mod analysis;
77pub mod codegen;
78pub mod error;
79pub mod openapi_enrich;
80pub mod output;
81pub mod parsing;
82pub mod utils;
83
84pub use codegen::{
85    BindingsConfig, CodeGenConfig, CodeGenOutput, GeneratedCode, generate_code, generate_models_mod,
86};
87
88pub use analysis::{
89    BodyField, GenerationPlan, ManagedResource, MethodPlan, PathParam, QueryParam, RequestParam,
90    RequestType, ResourceHierarchy, ServicePlan, SkippedMethod, analyze_metadata,
91    extract_managed_resources, split_body_fields,
92};
93// Note: MethodPlanner is pub(crate) — it is an internal helper, not part of the public API.
94pub use openapi_enrich::run as enrich_openapi;
95pub use parsing::http::HttpPattern;
96pub use parsing::types::{BaseType, RenderContext, UnifiedType};
97pub use parsing::{CodeGenMetadata, parse_file_descriptor_set, process_file_descriptor};
98
99/// The `FieldBehavior` enum from `google.api.field_behavior`, re-exported for
100/// consumers that need to inspect field behavior annotations (e.g. in tests).
101pub use google::api::FieldBehavior;
102
103// Prost-generated Google API proto types — internal only.
104pub(crate) mod google {
105    pub mod api {
106        #![allow(unused)]
107        #![allow(clippy::doc_overindented_list_items)]
108        #![allow(clippy::doc_lazy_continuation)]
109        include!("./gen/google.api.rs");
110    }
111}
112
113pub(crate) mod gnostic {
114    pub mod openapi {
115        pub mod v3 {
116            #![allow(unused)]
117            #![allow(clippy::large_enum_variant)]
118            include!("./gen/gnostic.openapi.v3.rs");
119        }
120    }
121}