axum_codec::extract

Struct Codec

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

Codec extractor / response.

The serialized data is not specified. Upon deserialization, the request’s Content-Type header is used to determine the format of the data.

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

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

Attempts to deserialize the given bytes as MessagePack. Does not perform any validation if the validator feature is enabled. For validation, use Self::from_bytes.

§Errors

See rmp_serde::from_slice.

source

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

Attemps to deserialize the given bytes as CBOR. Does not perform any validation if the validator feature is enabled. For validation, use Self::from_bytes.

§Errors

See [ciborium::from_slice].

source

pub fn from_yaml(text: &str) -> Result<Self, Error>

Attempts to deserialize the given text as YAML. Does not perform any validation if the validator feature is enabled. For validation, use Self::from_bytes.

§Errors

See serde_yaml::from_slice.

source

pub fn from_toml(text: &str) -> Result<Self, Error>

Attempts to deserialize the given text as TOML. Does not perform any validation if the validator feature is enabled. For validation, use Self::from_bytes.

§Errors

See toml::from_str.

source§

impl<T> Codec<T>

source

pub fn from_bincode(bytes: &[u8]) -> Result<Self, DecodeError>
where T: Decode,

Attempts to deserialize the given bytes as Bincode. Does not perform any validation if the validator feature is enabled. For validation, use Self::from_bytes.

§Errors

See bincode::decode_from_slice.

source

pub fn from_bitcode(bytes: &[u8]) -> Result<Self, Error>
where T: DecodeOwned,

Attempts to deserialize the given bytes as Bitcode. Does not perform any validation if the validator feature is enabled. For validation, use Self::from_bytes.

§Errors

See bitcode::decode.

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

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

Attempts to serialize the given value as MessagePack.

§Errors

See rmp_serde::to_vec_named.

source

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

Attempts to serialize the given value as CBOR.

§Errors

See ciborium::into_writer.

source

pub fn to_yaml(&self) -> Result<String, Error>

Attempts to serialize the given value as YAML.

§Errors

See [serde_yaml::to_vec].

source

pub fn to_toml(&self) -> Result<String, Error>

Attempts to serialize the given value as TOML.

§Errors

See toml::to_string.

source§

impl<T> Codec<T>

source

pub fn to_bincode(&self) -> Result<Vec<u8>, EncodeError>
where T: Encode,

Attempts to serialize the given value as Bincode

§Errors

See [bincode::serialize].

source

pub fn to_bitcode(&self) -> Vec<u8>
where T: Encode,

Attempts to serialize the given value as Bitcode

§Errors

See bitcode::encode.

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 [Error].

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>

source§

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,

source§

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, Self: OperationOutput,

source§

impl<T> OperationInput for Codec<T>
where T: JsonSchema,

source§

fn operation_input(ctx: &mut GenContext, operation: &mut Operation)

Modify the operation. Read more
source§

fn inferred_early_responses( ctx: &mut GenContext, operation: &mut Operation, ) -> Vec<(Option<u16>, Response)>

Inferred early responses are used to document early returns for extractors, guards inside handlers. For example these could represent JSON parsing errors, authentication failures. Read more
source§

impl<T> OperationOutput for Codec<T>
where T: JsonSchema,

source§

type Inner = T

The type that is used in examples. Read more
source§

fn operation_response( ctx: &mut GenContext, operation: &mut Operation, ) -> Option<Response>

Return a response documentation for this type, alternatively modify the operation if required. Read more
source§

fn inferred_responses( ctx: &mut GenContext, operation: &mut Operation, ) -> Vec<(Option<u16>, Response)>

Inferred responses are used when the type is used as a request handler return type. Read more
source§

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

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> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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>,

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

impl<T> ValidateIp for T
where T: ToString,

source§

fn validate_ipv4(&self) -> bool

Validates whether the given string is an IP V4
source§

fn validate_ipv6(&self) -> bool

Validates whether the given string is an IP V6
source§

fn validate_ip(&self) -> bool

Validates whether the given string is an IP
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

impl<T> Input for T
where T: OperationInput,