Skip to main content

RestCodegenConfig

Struct RestCodegenConfig 

Source
pub struct RestCodegenConfig { /* private fields */ }
Expand description

Configuration for REST route code generation.

Decouples the generator from any specific service — all project-specific knowledge (which packages to process, which methods are public) is passed in rather than hardcoded.

§Auto-Discovery

When no packages are registered, generate automatically discovers all services with google.api.http annotations in the descriptor set, inferring Rust module paths from proto package names (dots → ::, e.g., auth.v1auth::v1). This matches standard prost-build module generation.

§Examples

Minimal — auto-discovers packages from descriptor set:

let config = RestCodegenConfig::new();
let code = tonic_rest_build::generate(&descriptor_bytes, &config)?;

Explicit package mapping (e.g., when using pub use v1::*; re-exports):

let config = RestCodegenConfig::new()
    .package("auth.v1", "auth")
    .package("users.v1", "users")
    .wrapper_type("crate::core::Uuid")
    .extension_type("my_app::AuthInfo")
    .public_methods(&["Login", "SignUp"]);

let code = tonic_rest_build::generate(&descriptor_bytes, &config)?;

Implementations§

Source§

impl RestCodegenConfig

Source

pub fn new() -> Self

Create a new config with defaults.

Source

pub fn package(self, proto_package: &str, rust_module: &str) -> Self

Register a proto package for REST route generation.

When at least one package is registered, only registered packages are processed (auto-discovery is disabled).

§Example
config.package("auth.v1", "auth")
      .package("users.v1", "users");
Source

pub fn public_methods(self, methods: &[&str]) -> Self

Set proto method names whose REST paths bypass authentication.

Method names should be in PascalCase as defined in proto (e.g., "Authenticate").

Source

pub fn proto_root(self, root: &str) -> Self

Set the root module path for proto-generated types.

Default: "crate" — converts .auth.v1.Usercrate::auth::User.

Source

pub fn runtime_crate(self, path: &str) -> Self

Set the runtime crate/module path for generated handler imports.

Default: "tonic_rest" — generates tonic_rest::RestError, etc. Set to "crate::rest" if the runtime types live alongside the generated code.

Source

pub fn wrapper_type(self, type_path: &str) -> Self

Set the Rust type path for single-field wrapper messages.

Required when proto paths contain nested params like {user_id.value}. Commonly used for UUID wrapper types. Without this, generate returns a GenerateError for nested path params.

Source

pub fn sse_keep_alive_secs(self, secs: u64) -> Self

Set the SSE keep-alive interval in seconds (default: 15).

Values less than 1 are clamped to 1 to prevent continuous keep-alive spam.

Source

pub fn extension_type(self, type_path: &str) -> Self

Set the extension type extracted from Axum request extensions.

When set, generated handlers use Option<Extension<T>> to extract the value and forward it to build_tonic_request. Typically used for auth info (e.g., "my_app::AuthInfo"). When None, handlers skip extension extraction entirely.

§Example
config.extension_type("my_app::AuthInfo")
Source

pub fn extra_forwarded_headers(self, headers: &[&str]) -> Self

Add extra HTTP headers to forward from REST requests to gRPC metadata.

These are combined with the default FORWARDED_HEADERS at startup. Use for vendor-specific headers like Cloudflare’s cf-connecting-ip.

§Example
// Forward Cloudflare client IP header
config.extra_forwarded_headers(&["cf-connecting-ip"])

Trait Implementations§

Source§

impl Clone for RestCodegenConfig

Source§

fn clone(&self) -> RestCodegenConfig

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for RestCodegenConfig

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for RestCodegenConfig

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.