Skip to main content

Codec

Trait Codec 

Source
pub trait Codec:
    Send
    + Sync
    + 'static {
    // Required methods
    fn encoder_content_type(&self, accept: Option<&str>) -> String;
    fn encode<T: Message + Serialize>(
        &self,
        val: &T,
        mime: Option<&str>,
    ) -> Result<Bytes, GatewayError>;
    fn decode<T: Message + Default + DeserializeOwned>(
        &self,
        buf: &[u8],
        mime: Option<&str>,
    ) -> Result<T, GatewayError>;
}
Expand description

Defines how to encode and decode gRPC messages to/from HTTP bodies.

This trait abstracts the serialization logic, enabling the gateway to support various wire formats.

Required Methods§

Source

fn encoder_content_type(&self, accept: Option<&str>) -> String

Returns the content type that this codec will use for encoding.

§Parameters
  • accept: The Accept header value from the request, if any.
Source

fn encode<T: Message + Serialize>( &self, val: &T, mime: Option<&str>, ) -> Result<Bytes, GatewayError>

Encodes a message into a buffer.

§Parameters
  • val: The message to encode. Must implement prost::Message and serde::Serialize.
  • mime: The MIME type requested (e.g. from Accept header).
§Returns

A Result containing the encoded bytes as bytes::Bytes or a GatewayError on failure.

Source

fn decode<T: Message + Default + DeserializeOwned>( &self, buf: &[u8], mime: Option<&str>, ) -> Result<T, GatewayError>

Decodes a buffer into a message.

§Parameters
  • buf: The byte slice to decode.
  • mime: The content type of the incoming data.
§Returns

A Result containing the decoded message of type T or a GatewayError on failure.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§