use rmp::encode;
use serde::{Serialize, Serializer};
use crate::encode::{Error, UnderlyingWrite};
pub trait SerializerConfig: sealed::SerializerConfig {}
impl<T: sealed::SerializerConfig> SerializerConfig for T {}
mod sealed {
use serde::{Serialize, Serializer};
use crate::encode::{Error, UnderlyingWrite};
pub trait SerializerConfig {
fn write_struct_len<S>(ser: &mut S, len: usize) -> Result<(), Error>
where
S: UnderlyingWrite,
for<'a> &'a mut S: Serializer<Ok = (), Error = Error>;
fn write_struct_field<S, T>(ser: &mut S, key: &'static str, value: &T) -> Result<(), Error>
where
S: UnderlyingWrite,
for<'a> &'a mut S: Serializer<Ok = (), Error = Error>,
T: ?Sized + Serialize;
fn write_variant_ident<S>(
ser: &mut S,
variant_index: u32,
variant: &'static str,
) -> Result<(), Error>
where
S: UnderlyingWrite,
for<'a> &'a mut S: Serializer<Ok = (), Error = Error>;
}
}
#[derive(Copy, Clone, Debug)]
pub struct DefaultConfig;
impl sealed::SerializerConfig for DefaultConfig {
fn write_struct_len<S>(ser: &mut S, len: usize) -> Result<(), Error>
where
S: UnderlyingWrite,
for<'a> &'a mut S: Serializer<Ok = (), Error = Error>,
{
encode::write_array_len(ser.get_mut(), len as u32)?;
Ok(())
}
fn write_struct_field<S, T>(ser: &mut S, _key: &'static str, value: &T) -> Result<(), Error>
where
S: UnderlyingWrite,
for<'a> &'a mut S: Serializer<Ok = (), Error = Error>,
T: ?Sized + Serialize,
{
value.serialize(ser)
}
fn write_variant_ident<S>(
ser: &mut S,
variant_index: u32,
_variant: &'static str,
) -> Result<(), Error>
where
S: UnderlyingWrite,
for<'a> &'a mut S: Serializer<Ok = (), Error = Error>,
{
ser.serialize_u32(variant_index)
}
}
#[derive(Copy, Clone, Debug)]
pub struct StructMapConfig<C>(C);
impl<C> StructMapConfig<C> {
pub fn new(inner: C) -> Self {
StructMapConfig(inner)
}
}
impl<C> sealed::SerializerConfig for StructMapConfig<C>
where
C: sealed::SerializerConfig,
{
fn write_struct_len<S>(ser: &mut S, len: usize) -> Result<(), Error>
where
S: UnderlyingWrite,
for<'a> &'a mut S: Serializer<Ok = (), Error = Error>,
{
encode::write_map_len(ser.get_mut(), len as u32)?;
Ok(())
}
fn write_struct_field<S, T>(ser: &mut S, key: &'static str, value: &T) -> Result<(), Error>
where
S: UnderlyingWrite,
for<'a> &'a mut S: Serializer<Ok = (), Error = Error>,
T: ?Sized + Serialize,
{
encode::write_str(ser.get_mut(), key)?;
value.serialize(ser)
}
fn write_variant_ident<S>(
ser: &mut S,
variant_index: u32,
variant: &'static str,
) -> Result<(), Error>
where
S: UnderlyingWrite,
for<'a> &'a mut S: Serializer<Ok = (), Error = Error>,
{
C::write_variant_ident(ser, variant_index, variant)
}
}
#[derive(Copy, Clone, Debug)]
pub struct StructTupleConfig<C>(C);
impl<C> StructTupleConfig<C> {
pub fn new(inner: C) -> Self {
StructTupleConfig(inner)
}
}
impl<C> sealed::SerializerConfig for StructTupleConfig<C>
where
C: sealed::SerializerConfig,
{
fn write_struct_len<S>(ser: &mut S, len: usize) -> Result<(), Error>
where
S: UnderlyingWrite,
for<'a> &'a mut S: Serializer<Ok = (), Error = Error>,
{
encode::write_array_len(ser.get_mut(), len as u32)?;
Ok(())
}
fn write_struct_field<S, T>(ser: &mut S, _key: &'static str, value: &T) -> Result<(), Error>
where
S: UnderlyingWrite,
for<'a> &'a mut S: Serializer<Ok = (), Error = Error>,
T: ?Sized + Serialize,
{
value.serialize(ser)
}
fn write_variant_ident<S>(
ser: &mut S,
variant_index: u32,
variant: &'static str,
) -> Result<(), Error>
where
S: UnderlyingWrite,
for<'a> &'a mut S: Serializer<Ok = (), Error = Error>,
{
C::write_variant_ident(ser, variant_index, variant)
}
}
#[derive(Copy, Clone, Debug)]
pub struct VariantStringConfig<C>(C);
impl<C> VariantStringConfig<C> {
pub fn new(inner: C) -> Self {
VariantStringConfig(inner)
}
}
impl<C> sealed::SerializerConfig for VariantStringConfig<C>
where
C: sealed::SerializerConfig,
{
fn write_struct_len<S>(ser: &mut S, len: usize) -> Result<(), Error>
where
S: UnderlyingWrite,
for<'a> &'a mut S: Serializer<Ok = (), Error = Error>,
{
C::write_struct_len(ser, len)
}
fn write_struct_field<S, T>(ser: &mut S, key: &'static str, value: &T) -> Result<(), Error>
where
S: UnderlyingWrite,
for<'a> &'a mut S: Serializer<Ok = (), Error = Error>,
T: ?Sized + Serialize,
{
C::write_struct_field(ser, key, value)
}
fn write_variant_ident<S>(
ser: &mut S,
_variant_index: u32,
variant: &'static str,
) -> Result<(), Error>
where
S: UnderlyingWrite,
for<'a> &'a mut S: Serializer<Ok = (), Error = Error>,
{
ser.serialize_str(variant)
}
}
#[derive(Copy, Clone, Debug)]
pub struct VariantIntegerConfig<C>(C);
impl<C> VariantIntegerConfig<C> {
pub fn new(inner: C) -> Self {
VariantIntegerConfig(inner)
}
}
impl<C> sealed::SerializerConfig for VariantIntegerConfig<C>
where
C: sealed::SerializerConfig,
{
fn write_struct_len<S>(ser: &mut S, len: usize) -> Result<(), Error>
where
S: UnderlyingWrite,
for<'a> &'a mut S: Serializer<Ok = (), Error = Error>,
{
C::write_struct_len(ser, len)
}
fn write_struct_field<S, T>(ser: &mut S, key: &'static str, value: &T) -> Result<(), Error>
where
S: UnderlyingWrite,
for<'a> &'a mut S: Serializer<Ok = (), Error = Error>,
T: ?Sized + Serialize,
{
C::write_struct_field(ser, key, value)
}
fn write_variant_ident<S>(
ser: &mut S,
variant_index: u32,
_variant: &'static str,
) -> Result<(), Error>
where
S: UnderlyingWrite,
for<'a> &'a mut S: Serializer<Ok = (), Error = Error>,
{
ser.serialize_u32(variant_index)
}
}