Skip to main content

trillium_grpc/codec/
mod.rs

1//! Message codecs: the [`Codec`] trait and the default [`Prost`]
2//! implementation.
3
4use crate::Status;
5use bytes::Bytes;
6
7mod prost_codec;
8pub use prost_codec::Prost;
9
10/// Per-RPC value codec — encodes a request/response message to bytes and back.
11///
12/// The default for codegen is [`Prost`]. Other codecs (JSON, custom)
13/// implement this trait so the same call-shape dispatch functions can drive
14/// them without runtime dispatch.
15pub trait Codec<T>: 'static {
16    /// The fragment after `application/grpc+` in the content-type header
17    /// (e.g. `"proto"`, `"json"`). For raw `application/grpc`, return `"proto"`
18    /// — the spec says the bare type implies protobuf.
19    fn content_type_suffix() -> &'static str;
20
21    /// Serialize one message to its wire bytes. The framing layer wraps the
22    /// result in a length-prefixed gRPC frame; this is just the payload.
23    fn encode(value: &T) -> Result<Bytes, Status>;
24
25    /// Deserialize one message from the bytes of a single frame's payload
26    /// (already de-framed and decompressed).
27    fn decode(bytes: &[u8]) -> Result<T, Status>;
28}