1use crate::{Codec, CodecRejection, ContentType};
2
3crate::macros::__private_decode_trait! {
4 }
10
11#[cfg(feature = "serde")]
12impl<'b, T> Codec<T>
13where
14 T: serde::de::Deserialize<'b>,
15{
16 #[cfg(feature = "json")]
22 #[inline]
23 pub fn from_json(bytes: &'b [u8]) -> Result<Self, serde_json::Error> {
24 serde_json::from_slice(bytes).map(Self)
25 }
26
27 #[cfg(feature = "form")]
33 #[inline]
34 pub fn from_form(bytes: &'b [u8]) -> Result<Self, serde_urlencoded::de::Error> {
35 serde_urlencoded::from_bytes(bytes).map(Self)
36 }
37
38 #[cfg(feature = "msgpack")]
46 #[inline]
47 pub fn from_msgpack(bytes: &'b [u8]) -> Result<Self, rmp_serde::decode::Error> {
48 let mut deserializer = rmp_serde::Deserializer::new(bytes).with_human_readable();
49
50 serde::Deserialize::deserialize(&mut deserializer).map(Self)
51 }
52
53 #[cfg(feature = "cbor")]
61 #[inline]
62 pub fn from_cbor(bytes: &'b [u8]) -> Result<Self, ciborium::de::Error<std::io::Error>> {
63 ciborium::from_reader(bytes).map(Self)
64 }
65
66 #[cfg(feature = "yaml")]
74 #[inline]
75 pub fn from_yaml(text: &'b str) -> Result<Self, serde_yaml::Error> {
76 serde_yaml::from_str(text).map(Self)
77 }
78
79 #[cfg(feature = "toml")]
87 #[inline]
88 pub fn from_toml(text: &'b str) -> Result<Self, toml::de::Error> {
89 toml::from_str(text).map(Self)
90 }
91}
92
93impl<'b, T> Codec<T> {
94 #[cfg(feature = "bincode")]
102 #[inline]
103 pub fn from_bincode(bytes: &'b [u8]) -> Result<Self, bincode::error::DecodeError>
104 where
105 T: bincode::BorrowDecode<'b, ()>,
106 {
107 bincode::borrow_decode_from_slice(bytes, bincode::config::standard()).map(|t| Self(t.0))
108 }
109
110 #[cfg(feature = "bitcode")]
118 #[inline]
119 pub fn from_bitcode(bytes: &'b [u8]) -> Result<Self, bitcode::Error>
120 where
121 T: bitcode::Decode<'b>,
122 {
123 bitcode::decode(bytes).map(Self)
124 }
125
126 pub fn from_bytes(bytes: &'b [u8], content_type: ContentType) -> Result<Self, CodecRejection>
132 where
133 T: CodecDecode<'b>,
134 {
135 let codec = match content_type {
136 #[cfg(feature = "json")]
137 ContentType::Json => Self::from_json(bytes)?,
138 #[cfg(feature = "form")]
139 ContentType::Form => Self::from_form(bytes)?,
140 #[cfg(feature = "msgpack")]
141 ContentType::MsgPack => Self::from_msgpack(bytes)?,
142 #[cfg(feature = "bincode")]
143 ContentType::Bincode => Self::from_bincode(bytes)?,
144 #[cfg(feature = "bitcode")]
145 ContentType::Bitcode => Self::from_bitcode(bytes)?,
146 #[cfg(feature = "cbor")]
147 ContentType::Cbor => Self::from_cbor(bytes)?,
148 #[cfg(feature = "yaml")]
149 ContentType::Yaml => Self::from_yaml(core::str::from_utf8(bytes)?)?,
150 #[cfg(feature = "toml")]
151 ContentType::Toml => Self::from_toml(core::str::from_utf8(bytes)?)?,
152 };
153
154 #[cfg(feature = "validator")]
155 validator::Validate::validate(&codec)?;
156
157 Ok(codec)
158 }
159}