Skip to main content

Crate olai_codegen

Crate olai_codegen 

Source
Expand description

Proto-driven code generation from compiled protobuf descriptors.

Turns descriptor bytes (from buf build) into Rust REST-API glue code (Axum handlers, HTTP clients), resource registries, and optional PyO3 / NAPI / TypeScript bindings. Code generation is normally driven by the trestle CLI (olai-trestle); this is the library you embed for custom build tooling.

§Pipeline

descriptor bytes
  └─ parse_file_descriptor_set ─▶ CodeGenMetadata
       └─ generate_code(&metadata, &config) ─▶ files written to CodeGenOutput dirs

§Example

ⓘ
use std::fs;
use olai_codegen::{CodeGenConfig, CodeGenOutput, generate_code, parse_file_descriptor_set};
use protobuf::{Message, descriptor::FileDescriptorSet};

let bytes = fs::read("descriptors.bin")?;           // produced by `buf build`
let fds = FileDescriptorSet::parse_from_bytes(&bytes)?;
let metadata = parse_file_descriptor_set(&fds)?;

let config = CodeGenConfig {
    context_type_path: "crate::api::RequestContext".into(),
    result_type_path: "crate::Result".into(),
    models_path_template: "my_crate::models::{service}::v1".into(),
    models_path_crate_template: "crate::models::{service}::v1".into(),
    generate_resource_enum: true,
    output: CodeGenOutput {
        common: "out/common".into(),
        server: Some("out/server".into()),
        client: Some("out/client".into()),
        ..Default::default()
    },
    ..Default::default()
};
config.validate()?;
generate_code(&metadata, &config)?;

Re-exports§

pub use codegen::BindingsConfig;
pub use codegen::ClientProtocol;
pub use codegen::ClientProtocols;
pub use codegen::CodeGenConfig;
pub use codegen::CodeGenOutput;
pub use codegen::DEFAULT_TRANSPORT_TYPE_PATH;
pub use codegen::GeneratedCode;
pub use codegen::Runtime;
pub use codegen::generate_code;
pub use codegen::generate_models_mod;
pub use analysis::BodyField;
pub use analysis::GenerationPlan;
pub use analysis::ManagedResource;
pub use analysis::MethodPlan;
pub use analysis::PathParam;
pub use analysis::QueryParam;
pub use analysis::RequestParam;
pub use analysis::RequestType;
pub use analysis::ResourceHierarchy;
pub use analysis::ServicePlan;
pub use analysis::SkippedMethod;
pub use analysis::analyze_metadata;
pub use analysis::extract_managed_resources;
pub use analysis::split_body_fields;
pub use openapi_enrich::run as enrich_openapi;
pub use parsing::http::HttpPattern;
pub use parsing::types::BaseType;
pub use parsing::types::RenderContext;
pub use parsing::types::UnifiedType;
pub use parsing::CodeGenMetadata;
pub use parsing::parse_file_descriptor_set;
pub use parsing::process_file_descriptor;
pub use error::*;

Modules§

analysis
Analysis module for processing protobuf metadata into code generation plans
codegen
Code generation module for REST API handlers and language bindings.
error
openapi_enrich
output
parsing
Parsing stage: compiled descriptor → CodeGenMetadata.
utils
Shared utilities for the olai-codegen crate

Enums§

FieldBehavior
The FieldBehavior enum from google.api.field_behavior, re-exported for consumers that need to inspect field behavior annotations (e.g. in tests). An indicator of the behavior of a given field (for example, that a field is required in requests, or given as output but ignored as input). This does not change the behavior in protocol buffers itself; it only denotes the behavior and may affect how API tooling handles the field.