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<'b, T> Codec<T>where
T: Deserialize<'b>,
impl<'b, T> Codec<T>where
T: Deserialize<'b>,
Sourcepub fn from_form(bytes: &'b [u8]) -> Result<Self, Error>
pub fn from_form(bytes: &'b [u8]) -> Result<Self, Error>
Attempts to deserialize the given bytes as URL-encoded form data.
§Errors
Sourcepub fn from_msgpack(bytes: &'b [u8]) -> Result<Self, Error>
pub fn from_msgpack(bytes: &'b [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
Sourcepub fn from_yaml(text: &'b str) -> Result<Self, Error>
pub fn from_yaml(text: &'b 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
Sourcepub fn from_toml(text: &'b str) -> Result<Self, Error>
pub fn from_toml(text: &'b 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<'b, T> Codec<T>
impl<'b, T> Codec<T>
Sourcepub fn from_bincode(bytes: &'b [u8]) -> Result<Self, DecodeError>where
T: BorrowDecode<'b>,
pub fn from_bincode(bytes: &'b [u8]) -> Result<Self, DecodeError>where
T: BorrowDecode<'b>,
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
Sourcepub fn from_bitcode(bytes: &'b [u8]) -> Result<Self, Error>where
T: Decode<'b>,
pub fn from_bitcode(bytes: &'b [u8]) -> Result<Self, Error>where
T: Decode<'b>,
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
.
Sourcepub fn from_bytes(
bytes: &'b [u8],
content_type: ContentType,
) -> Result<Self, CodecRejection>where
T: CodecDecode<'b>,
pub fn from_bytes(
bytes: &'b [u8],
content_type: ContentType,
) -> Result<Self, CodecRejection>where
T: CodecDecode<'b>,
Source§impl<T> Codec<T>where
T: Serialize,
impl<T> Codec<T>where
T: Serialize,
Source§impl<T> Codec<T>
impl<T> Codec<T>
Sourcepub fn to_bincode(&self) -> Result<Vec<u8>, EncodeError>where
T: Encode,
pub fn to_bincode(&self) -> Result<Vec<u8>, EncodeError>where
T: Encode,
Sourcepub fn to_bytes(&self, content_type: ContentType) -> Result<Vec<u8>, Error>where
T: CodecEncode,
pub fn to_bytes(&self, content_type: ContentType) -> Result<Vec<u8>, Error>where
T: CodecEncode,
Source§impl<T> Codec<T>
impl<T> Codec<T>
Sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Consumes the Codec
and returns the inner value.
Source§impl<T> Codec<T>where
T: CodecEncode,
impl<T> Codec<T>where
T: CodecEncode,
Sourcepub fn to_response<C: Into<ContentType>>(&self, content_type: C) -> Response
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, S> FromRequest<S> for Codec<T>
impl<T, S> FromRequest<S> for Codec<T>
Source§impl<D> IntoCodecResponse for Codec<D>where
D: CodecEncode,
Self: OperationOutput,
impl<D> IntoCodecResponse for Codec<D>where
D: CodecEncode,
Self: OperationOutput,
fn into_codec_response(self, content_type: ContentType) -> Response
Source§impl<T> OperationInput for Codec<T>where
T: JsonSchema,
impl<T> OperationInput for Codec<T>where
T: JsonSchema,
Source§fn operation_input(ctx: &mut GenContext, operation: &mut Operation)
fn operation_input(ctx: &mut GenContext, operation: &mut Operation)
Source§fn inferred_early_responses(
ctx: &mut GenContext,
operation: &mut Operation,
) -> Vec<(Option<u16>, Response)>
fn inferred_early_responses( ctx: &mut GenContext, operation: &mut Operation, ) -> Vec<(Option<u16>, Response)>
Source§impl<T> OperationOutput for Codec<T>where
T: JsonSchema,
impl<T> OperationOutput for Codec<T>where
T: JsonSchema,
Source§fn operation_response(
ctx: &mut GenContext,
operation: &mut Operation,
) -> Option<Response>
fn operation_response( ctx: &mut GenContext, operation: &mut Operation, ) -> Option<Response>
Source§fn inferred_responses(
ctx: &mut GenContext,
operation: &mut Operation,
) -> Vec<(Option<u16>, Response)>
fn inferred_responses( ctx: &mut GenContext, operation: &mut Operation, ) -> Vec<(Option<u16>, Response)>
Source§impl<T: Ord> Ord for Codec<T>
impl<T: Ord> Ord for Codec<T>
Source§impl<T: PartialOrd> PartialOrd for Codec<T>
impl<T: PartialOrd> PartialOrd for Codec<T>
impl<T: Eq> Eq for Codec<T>
impl<T> StructuralPartialEq for Codec<T>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.