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>where
T: DeserializeOwned,
impl<T> Codec<T>where
T: DeserializeOwned,
sourcepub fn from_msgpack(bytes: &[u8]) -> Result<Self, Error>
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
sourcepub fn from_cbor(bytes: &[u8]) -> Result<Self, Error<Error>>
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
].
sourcepub fn from_yaml(text: &str) -> Result<Self, Error>
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
sourcepub fn from_toml(text: &str) -> Result<Self, Error>
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>
impl<T> Codec<T>
sourcepub fn from_bincode(bytes: &[u8]) -> Result<Self, DecodeError>where
T: Decode,
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
sourcepub fn from_bitcode(bytes: &[u8]) -> Result<Self, Error>where
T: DecodeOwned,
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
.
sourcepub fn from_bytes(
bytes: &[u8],
content_type: ContentType,
) -> Result<Self, CodecRejection>where
T: CodecDecode,
pub fn from_bytes(
bytes: &[u8],
content_type: ContentType,
) -> Result<Self, CodecRejection>where
T: CodecDecode,
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_bitcode(&self) -> Vec<u8>where
T: Encode,
pub fn to_bitcode(&self) -> Vec<u8>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>where
T: CodecEncode,
impl<T> Codec<T>where
T: CodecEncode,
sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Consumes the Codec
and returns the inner value.
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.