tonic-rest
Runtime types for REST + SSE endpoints generated from protobuf google.api.http annotations.
This crate provides the shared types that generated Axum handlers reference at runtime.
The companion crate tonic-rest-build
generates the handler code at build time.
Types
RestError— Convertstonic::Statusto HTTP JSON error responses following the Google API error modelbuild_tonic_request— Bridges Axum HTTP requests totonic::Request, forwarding headers and extensions (e.g., auth info)sse_error_event— Formats gRPC errors as SSE eventsgrpc_to_http_status/grpc_code_name— Maps all 17 gRPC codes to HTTP status codes and canonical names
Serde Adapters
Behind the serde feature (enabled by default), provides #[serde(with)] adapters
for prost well-known types:
| Adapter | Type | Wire format |
|---|---|---|
timestamp / opt_timestamp |
Timestamp / Option<Timestamp> |
RFC 3339 ("2025-01-15T09:30:00Z") |
duration / opt_duration |
Duration / Option<Duration> |
Seconds with suffix ("300s") |
field_mask / opt_field_mask |
FieldMask / Option<FieldMask> |
Comma-separated camelCase ("displayName,email") |
And the define_enum_serde! macro for proto3 enum fields (which are i32 in prost):
define_enum_serde!;
// With prefix stripping:
define_enum_serde!;
Feature Flags
| Feature | Default | Description |
|---|---|---|
serde |
on | WKT serde adapters + define_enum_serde! macro (adds prost-types, chrono, serde deps) |
Quick Start
[]
= "0.1"
[]
= "0.1"
For a complete end-to-end example, see auth-service-rs.
Companion Crates
| Crate | Purpose | Cargo section |
|---|---|---|
| tonic-rest (this) | Runtime types | [dependencies] |
| tonic-rest-build | Build-time codegen | [build-dependencies] |
| tonic-rest-openapi | OpenAPI 3.1 generation | CLI / CI |
Compatibility
| tonic-rest | tonic | axum | prost-types | MSRV |
|---|---|---|---|---|
| 0.1.x | 0.14 | 0.8 | 0.14 | 1.82 |