Struct axum_codec::Codec

source ·
pub struct Codec<T>(pub T);
Expand description

Codec extractor / response.

The serialized data is not specified, unlike [axum::Json]. Upon deserialization, the request’s Content-Type header is used to determine the format of the data.

The supported formats are:

  • JSON
  • MessagePack
  • Bincode
  • Bitcode
  • YAML
  • TOML

By default, only JSON is supported. To enable other formats, use the corresponding feature flags.

Note that IntoResponse is not implemented for this type, as the headers are not available when serializing the data. Instead, use Codec::to_response to create a response with the appropriate Content-Type header extracted from the request with Accept.

§Examples

#[axum_codec::apply(decode)]
struct Greeting {
  hello: String
}

let bytes = b"{\"hello\": \"world\"}";
let content_type = ContentType::Json;

let Codec(data) = Codec::<Greeting>::from_bytes(bytes, content_type).unwrap();

assert_eq!(data.hello, "world");

Tuple Fields§

§0: T

Implementations§

source§

impl<T> Codec<T>

source

pub fn from_json(bytes: &[u8]) -> Result<Self, Error>

Attempts to deserialize the given bytes as JSON.

§Errors

See serde_json::from_slice.

source§

impl<T> Codec<T>

source

pub fn from_bytes( bytes: &[u8], content_type: ContentType, ) -> Result<Self, CodecRejection>
where T: CodecDecode,

Attempts to deserialize the given bytes as the specified ContentType.

§Errors

See CodecRejection.

source§

impl<T> Codec<T>
where T: Serialize,

source

pub fn to_json(&self) -> Result<Vec<u8>, Error>

Attempts to serialize the given value as JSON.

§Errors

See serde_json::to_vec.

source§

impl<T> Codec<T>

source

pub fn to_bytes(&self, content_type: ContentType) -> Result<Vec<u8>, Error>
where T: CodecEncode,

Attempts to serialize the given value as the specified ContentType.

§Errors

See [CodecRejection].

source§

impl<T> Codec<T>
where T: CodecEncode,

source

pub fn into_inner(self) -> T

Consumes the Codec and returns the inner value.

source

pub fn to_response<C: Into<ContentType>>(&self, content_type: C) -> Response

Converts the inner value into a response with the given content type.

If serialization fails, the rejection is converted into a response. See [encode::Error] for possible errors.

Trait Implementations§

source§

impl<T> Deref for Codec<T>

§

type Target = T

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
source§

impl<T> DerefMut for Codec<T>

source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
source§

impl<T: Display> Display for Codec<T>

source§

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

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

impl<T, S> FromRequest<S> for Codec<T>
where T: CodecDecode, S: Send + Sync + 'static,

§

type Rejection = Response<Body>

If the extractor fails it’ll use this “rejection” type. A rejection is a kind of error that can be converted into a response.
source§

fn from_request<'life0, 'async_trait>( req: Request, state: &'life0 S, ) -> Pin<Box<dyn Future<Output = Result<Self, Self::Rejection>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Perform the extraction.
source§

impl<D> IntoCodecResponse for Codec<D>
where D: CodecEncode,

Auto Trait Implementations§

§

impl<T> Freeze for Codec<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Codec<T>
where T: RefUnwindSafe,

§

impl<T> Send for Codec<T>
where T: Send,

§

impl<T> Sync for Codec<T>
where T: Sync,

§

impl<T> Unpin for Codec<T>
where T: Unpin,

§

impl<T> UnwindSafe for Codec<T>
where T: UnwindSafe,

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> 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> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

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

§

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>,

§

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.
source§

impl<T> Input for T