Skip to main content

Crate olai_codegen

Crate olai_codegen 

Source
Expand description

Code generation from protobuf descriptors.

proto-gen turns compiled protobuf descriptor bytes into Rust REST-API glue code (Axum handlers, HTTP clients, PyO3 bindings, NAPI bindings, and TypeScript clients).

§Pipeline overview

descriptor bytes
     │
     ▼
parse_file_descriptor_set   ← parsing module
     │  CodeGenMetadata
     ▼
analyze_metadata            ← analysis module
     │  GenerationPlan
     ▼
generate_code               ← codegen module
     │  writes files to CodeGenOutput dirs
     ▼
(generated Rust / Python / TypeScript source files)

§Quick-start example

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

// 1. Load descriptor bytes (produced by `buf build`)
let bytes = fs::read("descriptors.bin").unwrap();
let fds = FileDescriptorSet::parse_from_bytes(&bytes).unwrap();

// 2. Parse into metadata
let metadata = parse_file_descriptor_set(&fds).unwrap();

// 3. Configure outputs
let output = CodeGenOutput {
    common: "/tmp/out/common".into(),
    models: Some("/tmp/out/models".into()),
    models_subdir: "_gen".into(),
    server: Some("/tmp/out/server".into()),
    client: Some("/tmp/out/client".into()),
    python: None,
    node: None,
    node_ts: None,
    python_typings_filename: "client.pyi".into(),
};

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(),
    output,
    generate_resource_enum: true,
    generate_store_integration: false,
    error_type_path: None,
    generate_object_conversions: false,
    bindings: None,
    models_gen_dir: None,
    resource_store_crate_name: "olai_store".into(),
};

// 4. Optionally validate before running
config.validate().unwrap();

// 5. Generate
generate_code(&metadata, &config).unwrap();

Re-exports§

pub use codegen::BindingsConfig;
pub use codegen::CodeGenConfig;
pub use codegen::CodeGenOutput;
pub use codegen::GeneratedCode;
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
utils
Shared utilities for the proto-gen 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.