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,
{
#[cfg(feature = "json")]
#[inline]
pub fn from_json(bytes: &[u8]) -> Result<Self, serde_json::Error> {
serde_json::from_slice(bytes).map(Self)
}
#[cfg(feature = "msgpack")]
#[inline]
pub fn from_msgpack(bytes: &[u8]) -> Result<Self, rmp_serde::decode::Error> {
rmp_serde::from_slice(bytes).map(Self)
}
#[cfg(feature = "cbor")]
#[inline]
pub fn from_cbor(bytes: &[u8]) -> Result<Self, ciborium::de::Error<std::io::Error>> {
ciborium::from_reader(bytes).map(Self)
}
#[cfg(feature = "yaml")]
#[inline]
pub fn from_yaml(text: &str) -> Result<Self, serde_yaml::Error> {
serde_yaml::from_str(text).map(Self)
}
#[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> {
#[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))
}
#[cfg(feature = "bitcode")]
#[inline]
pub fn from_bitcode(bytes: &[u8]) -> Result<Self, bitcode::Error>
where
T: bitcode::DecodeOwned,
{
bitcode::decode(bytes).map(Self)
}
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)
}
}