use paste::paste;
use serde::ser;
#[derive(Debug)]
pub struct EnsureMapLike<T>(pub T);
#[derive(Debug)]
struct EnsureMapLikeSerializer<S>(S);
impl<T: ser::Serialize> ser::Serialize for EnsureMapLike<T> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
self.0.serialize(EnsureMapLikeSerializer(serializer))
}
}
macro_rules! impl_scalar_types_not_supported {
($($target:ident)*) => {paste!{$(
fn [<serialize_ $target>](self, _v: $target) -> Result<Self::Ok, Self::Error> {
Err(ser::Error::custom("scalar types are not supported"))
}
)*}};
}
impl<S: ser::Serializer> ser::Serializer for EnsureMapLikeSerializer<S> {
type Ok = S::Ok;
type Error = S::Error;
type SerializeSeq = ser::Impossible<S::Ok, S::Error>;
type SerializeTuple = ser::Impossible<S::Ok, S::Error>;
type SerializeTupleStruct = ser::Impossible<S::Ok, S::Error>;
type SerializeTupleVariant = ser::Impossible<S::Ok, S::Error>;
type SerializeMap = S::SerializeMap;
type SerializeStruct = S::SerializeStruct;
type SerializeStructVariant = ser::Impossible<S::Ok, S::Error>;
impl_scalar_types_not_supported!(bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char);
fn serialize_str(self, _v: &str) -> Result<Self::Ok, Self::Error> {
Err(ser::Error::custom("scalar types are not supported"))
}
fn serialize_bytes(self, _v: &[u8]) -> Result<Self::Ok, Self::Error> {
Err(ser::Error::custom("scalar types are not supported"))
}
fn serialize_none(self) -> Result<Self::Ok, Self::Error> {
Err(ser::Error::custom("optional types are not supported"))
}
fn serialize_some<T: ?Sized>(self, _value: &T) -> Result<Self::Ok, Self::Error>
where
T: serde::Serialize,
{
Err(ser::Error::custom("optional types are not supported"))
}
fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
Err(ser::Error::custom("unit types are not supported"))
}
fn serialize_unit_struct(self, _name: &'static str) -> Result<Self::Ok, Self::Error> {
Err(ser::Error::custom("unit types are not supported"))
}
fn serialize_unit_variant(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
) -> Result<Self::Ok, Self::Error> {
Err(ser::Error::custom("unit types are not supported"))
}
fn serialize_newtype_struct<T: ?Sized>(
self,
_name: &'static str,
value: &T,
) -> Result<Self::Ok, Self::Error>
where
T: serde::Serialize,
{
value.serialize(self)
}
fn serialize_newtype_variant<T: ?Sized>(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_value: &T,
) -> Result<Self::Ok, Self::Error>
where
T: serde::Serialize,
{
Err(ser::Error::custom("variant types are not supported"))
}
fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
Err(ser::Error::custom("sequence types are not supported"))
}
fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, Self::Error> {
Err(ser::Error::custom("sequence types are not supported"))
}
fn serialize_tuple_struct(
self,
_name: &'static str,
_len: usize,
) -> Result<Self::SerializeTupleStruct, Self::Error> {
Err(ser::Error::custom("sequence types are not supported"))
}
fn serialize_tuple_variant(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_len: usize,
) -> Result<Self::SerializeTupleVariant, Self::Error> {
Err(ser::Error::custom("variant types are not supported"))
}
fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
self.0.serialize_map(len)
}
fn serialize_struct(
self,
name: &'static str,
len: usize,
) -> Result<Self::SerializeStruct, Self::Error> {
self.0.serialize_struct(name, len)
}
fn serialize_struct_variant(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_len: usize,
) -> Result<Self::SerializeStructVariant, Self::Error> {
Err(ser::Error::custom("variant types are not supported"))
}
}