connectrpc-codegen 0.9.0

Library for generating ConnectRPC Rust service bindings from proto descriptors
Documentation
//! protoc-gen-connect-rust - Protoc plugin for generating ConnectRPC Rust service stubs.
//!
//! This plugin generates **service stubs only**:
//! - Server traits for implementing RPC handlers
//! - Service registration functions for the Router
//! - Client structs for making RPC calls
//!
//! Message types are NOT generated by this plugin; run `protoc-gen-buffa`
//! (or equivalent) separately. The service stubs reference message types
//! via absolute Rust paths configured with the `buffa_module` option.
//!
//! Usage with buf:
//! ```yaml
//! version: v2
//! plugins:
//!   - local: protoc-gen-buffa
//!     out: src/generated/buffa
//!     opt: [views=true, json=true]
//!   - local: protoc-gen-buffa-packaging
//!     out: src/generated/buffa
//!     strategy: all
//!   - local: protoc-gen-connect-rust
//!     out: src/generated/connect
//!     opt: [buffa_module=crate::proto]
//!   - local: protoc-gen-buffa-packaging
//!     out: src/generated/connect
//!     strategy: all
//!     opt: [filter=services]
//! ```
//!
//! Then mount both trees in your crate:
//! ```rust,ignore
//! #[path = "generated/buffa/mod.rs"]
//! pub mod proto;
//! #[path = "generated/connect/mod.rs"]
//! pub mod connect;
//! ```

use std::io;
use std::io::Read;
use std::io::Write;

use anyhow::Context;
use anyhow::Result;
use buffa::Message;

use connectrpc_codegen::codegen;
use connectrpc_codegen::plugin::CodeGeneratorRequest;

fn main() -> Result<()> {
    // Read the CodeGeneratorRequest from stdin
    let mut input = Vec::new();
    io::stdin()
        .read_to_end(&mut input)
        .context("failed to read from stdin")?;

    // buffa's plugins decode the identical request in the same `buf generate`
    // run, so the bound, the overrides that raise it and the message naming
    // them all come from there rather than from a connect-specific twin.
    //
    // The two overrides differ in reach, which the guide spells out: the
    // environment variable covers every plugin in the run, while `opt:` is
    // per-plugin, so setting the option on one plugin does nothing for this
    // one.
    let request: CodeGeneratorRequest =
        buffa_codegen::decode_request(&input).map_err(|e| anyhow::anyhow!(e))?;

    // Process the request
    let response = codegen::generate(&request)?;

    // Write the CodeGeneratorResponse to stdout
    let output = response.encode_to_vec();
    io::stdout()
        .write_all(&output)
        .context("failed to write to stdout")?;

    Ok(())
}