1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
use crate::{codec_trait, Codec, CodecRejection, ContentType};

#[cfg(not(feature = "validator"))]
mod codec {
	use super::codec_trait;

	#[cfg(all(feature = "serde", feature = "bincode", feature = "bitcode"))]
	codec_trait!(
		CodecDecode,
		serde::de::DeserializeOwned + bincode::Decode + bitcode::DecodeOwned
	);

	#[cfg(all(feature = "serde", feature = "bincode", not(feature = "bitcode")))]
	codec_trait!(CodecDecode, serde::de::DeserializeOwned + bincode::Decode);

	#[cfg(all(feature = "serde", not(feature = "bincode"), feature = "bitcode"))]
	codec_trait!(
		CodecDecode,
		serde::de::DeserializeOwned + bitcode::DecodeOwned
	);

	#[cfg(all(feature = "serde", not(feature = "bincode"), not(feature = "bitcode")))]
	codec_trait!(CodecDecode, serde::de::DeserializeOwned);

	#[cfg(all(not(feature = "serde"), feature = "bincode", feature = "bitcode"))]
	codec_trait!(CodecDecode, bincode::Decode + bitcode::DecodeOwned);

	#[cfg(all(not(feature = "serde"), feature = "bincode", not(feature = "bitcode")))]
	codec_trait!(CodecDecode, bincode::Decode);

	#[cfg(all(not(feature = "serde"), not(feature = "bincode"), feature = "bitcode"))]
	codec_trait!(CodecDecode, bitcode::DecodeOwned);

	#[cfg(all(
		not(feature = "serde"),
		not(feature = "bincode"),
		not(feature = "bitcode")
	))]
	codec_trait!(CodecDecode);
}

#[cfg(feature = "validator")]
mod codec {
	use super::codec_trait;

	#[cfg(all(feature = "serde", feature = "bincode", feature = "bitcode"))]
	codec_trait!(
		CodecDecode,
		serde::de::DeserializeOwned + bincode::Decode + bitcode::DecodeOwned + validator::Validate
	);

	#[cfg(all(feature = "serde", feature = "bincode", not(feature = "bitcode")))]
	codec_trait!(
		CodecDecode,
		serde::de::DeserializeOwned + bincode::Decode + validator::Validate
	);

	#[cfg(all(feature = "serde", not(feature = "bincode"), feature = "bitcode"))]
	codec_trait!(
		CodecDecode,
		serde::de::DeserializeOwned + bitcode::DecodeOwned + validator::Validate
	);

	#[cfg(all(feature = "serde", not(feature = "bincode"), not(feature = "bitcode")))]
	codec_trait!(
		CodecDecode,
		serde::de::DeserializeOwned + validator::Validate
	);

	#[cfg(all(not(feature = "serde"), feature = "bincode", feature = "bitcode"))]
	codec_trait!(
		CodecDecode,
		bincode::Decode + bitcode::DecodeOwned + validator::Validate
	);

	#[cfg(all(not(feature = "serde"), feature = "bincode", not(feature = "bitcode")))]
	codec_trait!(CodecDecode, bincode::Decode + validator::Validate);

	#[cfg(all(not(feature = "serde"), not(feature = "bincode"), feature = "bitcode"))]
	codec_trait!(CodecDecode, bitcode::DecodeOwned + validator::Validate);

	#[cfg(all(
		not(feature = "serde"),
		not(feature = "bincode"),
		not(feature = "bitcode")
	))]
	codec_trait!(CodecDecode);
}

pub use codec::CodecDecode;

#[cfg(feature = "serde")]
impl<T> Codec<T>
where
	T: serde::de::DeserializeOwned,
{
	/// Attempts to deserialize the given bytes as [JSON](https://www.json.org).
	///
	/// # Errors
	///
	/// See [`serde_json::from_slice`].
	#[cfg(feature = "json")]
	#[inline]
	pub fn from_json(bytes: &[u8]) -> Result<Self, serde_json::Error> {
		serde_json::from_slice(bytes).map(Self)
	}

	/// Attempts to deserialize the given bytes as [MessagePack](https://msgpack.org).
	/// Does not perform any validation if the `validator` feature is enabled. For validation,
	/// use [`Self::from_bytes`].
	///
	/// # Errors
	///
	/// See [`rmp_serde::from_slice`].
	#[cfg(feature = "msgpack")]
	#[inline]
	pub fn from_msgpack(bytes: &[u8]) -> Result<Self, rmp_serde::decode::Error> {
		rmp_serde::from_slice(bytes).map(Self)
	}

	/// Attemps to deserialize the given bytes as [CBOR](https://cbor.io).
	/// Does not perform any validation if the `validator` feature is enabled. For validation,
	/// use [`Self::from_bytes`].
	///
	/// # Errors
	///
	/// See [`ciborium::from_slice`].
	#[cfg(feature = "cbor")]
	#[inline]
	pub fn from_cbor(bytes: &[u8]) -> Result<Self, ciborium::de::Error<std::io::Error>> {
		ciborium::from_reader(bytes).map(Self)
	}

	/// Attempts to deserialize the given text as [YAML](https://yaml.org).
	/// Does not perform any validation if the `validator` feature is enabled. For validation,
	/// use [`Self::from_bytes`].
	///
	/// # Errors
	///
	/// See [`serde_yaml::from_slice`].
	#[cfg(feature = "yaml")]
	#[inline]
	pub fn from_yaml(text: &str) -> Result<Self, serde_yaml::Error> {
		serde_yaml::from_str(text).map(Self)
	}

	/// Attempts to deserialize the given text as [TOML](https://toml.io).
	/// Does not perform any validation if the `validator` feature is enabled. For validation,
	/// use [`Self::from_bytes`].
	///
	/// # Errors
	///
	/// See [`toml::from_str`].
	#[cfg(feature = "toml")]
	#[inline]
	pub fn from_toml(text: &str) -> Result<Self, toml::de::Error> {
		toml::from_str(text).map(Self)
	}
}

impl<T> Codec<T> {
	/// Attempts to deserialize the given bytes as [Bincode](https://github.com/bincode-org/bincode).
	/// Does not perform any validation if the `validator` feature is enabled. For validation,
	/// use [`Self::from_bytes`].
	///
	/// # Errors
	///
	/// See [`bincode::decode_from_slice`].
	#[cfg(feature = "bincode")]
	#[inline]
	pub fn from_bincode(bytes: &[u8]) -> Result<Self, bincode::error::DecodeError>
	where
		T: bincode::Decode,
	{
		bincode::decode_from_slice(bytes, bincode::config::standard()).map(|t| Self(t.0))
	}

	/// Attempts to deserialize the given bytes as [Bitcode](https://github.com/SoftbearStudios/bitcode).
	/// Does not perform any validation if the `validator` feature is enabled. For validation,
	/// use [`Self::from_bytes`].
	///
	/// # Errors
	///
	/// See [`bitcode::decode`].
	#[cfg(feature = "bitcode")]
	#[inline]
	pub fn from_bitcode(bytes: &[u8]) -> Result<Self, bitcode::Error>
	where
		T: bitcode::DecodeOwned,
	{
		bitcode::decode(bytes).map(Self)
	}

	/// Attempts to deserialize the given bytes as the specified [`ContentType`].
	///
	/// # Errors
	///
	/// See [`CodecRejection`].
	pub fn from_bytes(bytes: &[u8], content_type: ContentType) -> Result<Self, CodecRejection>
	where
		T: CodecDecode,
	{
		let codec = match content_type {
			#[cfg(feature = "json")]
			ContentType::Json => Self::from_json(bytes)?,
			#[cfg(feature = "msgpack")]
			ContentType::MsgPack => Self::from_msgpack(bytes)?,
			#[cfg(feature = "bincode")]
			ContentType::Bincode => Self::from_bincode(bytes)?,
			#[cfg(feature = "bitcode")]
			ContentType::Bitcode => Self::from_bitcode(bytes)?,
			#[cfg(feature = "cbor")]
			ContentType::Cbor => Self::from_cbor(bytes)?,
			#[cfg(feature = "yaml")]
			ContentType::Yaml => Self::from_yaml(core::str::from_utf8(bytes)?)?,
			#[cfg(feature = "toml")]
			ContentType::Toml => Self::from_toml(core::str::from_utf8(bytes)?)?,
		};

		#[cfg(feature = "validator")]
		validator::Validate::validate(&codec)?;

		Ok(codec)
	}
}