serde_more 0.1.4

Procedural macro to add arbitrary data when serializing using serde
Documentation
#![doc = include_str!("../README.md")]
#![no_std]

pub use serde_more_derive::SerializeMore;

use serde::ser::{SerializeMap, Serializer};

/// A custom serializer that flattens struct fields into a parent map.
///
/// This is NOT a part of the public API and is only intended to be used
/// in the code generated by [`SerializeMore`] derive macro.
#[doc(hidden)]
pub struct FlatMapSerializer<'a, M: SerializeMap> {
    pub map: &'a mut M,
}

impl<'a, M: SerializeMap> Serializer for FlatMapSerializer<'a, M> {
    type Ok = ();
    type Error = M::Error;

    type SerializeSeq = serde::ser::Impossible<(), M::Error>;
    type SerializeTuple = serde::ser::Impossible<(), M::Error>;
    type SerializeTupleStruct = serde::ser::Impossible<(), M::Error>;
    type SerializeTupleVariant = serde::ser::Impossible<(), M::Error>;
    type SerializeMap = serde::ser::Impossible<(), M::Error>;
    type SerializeStruct = Self;
    type SerializeStructVariant = serde::ser::Impossible<(), M::Error>;

    fn serialize_bool(self, _v: bool) -> Result<(), M::Error> {
        Err(serde::ser::Error::custom("expected struct"))
    }

    fn serialize_i8(self, _v: i8) -> Result<(), M::Error> {
        Err(serde::ser::Error::custom("expected struct"))
    }

    fn serialize_i16(self, _v: i16) -> Result<(), M::Error> {
        Err(serde::ser::Error::custom("expected struct"))
    }

    fn serialize_i32(self, _v: i32) -> Result<(), M::Error> {
        Err(serde::ser::Error::custom("expected struct"))
    }

    fn serialize_i64(self, _v: i64) -> Result<(), M::Error> {
        Err(serde::ser::Error::custom("expected struct"))
    }

    fn serialize_u8(self, _v: u8) -> Result<(), M::Error> {
        Err(serde::ser::Error::custom("expected struct"))
    }

    fn serialize_u16(self, _v: u16) -> Result<(), M::Error> {
        Err(serde::ser::Error::custom("expected struct"))
    }

    fn serialize_u32(self, _v: u32) -> Result<(), M::Error> {
        Err(serde::ser::Error::custom("expected struct"))
    }

    fn serialize_u64(self, _v: u64) -> Result<(), M::Error> {
        Err(serde::ser::Error::custom("expected struct"))
    }

    fn serialize_f32(self, _v: f32) -> Result<(), M::Error> {
        Err(serde::ser::Error::custom("expected struct"))
    }

    fn serialize_f64(self, _v: f64) -> Result<(), M::Error> {
        Err(serde::ser::Error::custom("expected struct"))
    }

    fn serialize_char(self, _v: char) -> Result<(), M::Error> {
        Err(serde::ser::Error::custom("expected struct"))
    }

    fn serialize_str(self, _v: &str) -> Result<(), M::Error> {
        Err(serde::ser::Error::custom("expected struct"))
    }

    fn serialize_bytes(self, _v: &[u8]) -> Result<(), M::Error> {
        Err(serde::ser::Error::custom("expected struct"))
    }

    fn serialize_none(self) -> Result<(), M::Error> {
        Err(serde::ser::Error::custom("expected struct"))
    }

    fn serialize_some<T: ?Sized + serde::Serialize>(self, _value: &T) -> Result<(), M::Error> {
        Err(serde::ser::Error::custom("expected struct"))
    }

    fn serialize_unit(self) -> Result<(), M::Error> {
        Err(serde::ser::Error::custom("expected struct"))
    }

    fn serialize_unit_struct(self, _name: &'static str) -> Result<(), M::Error> {
        Err(serde::ser::Error::custom("expected struct"))
    }

    fn serialize_unit_variant(
        self,
        _name: &'static str,
        _variant_index: u32,
        _variant: &'static str,
    ) -> Result<(), M::Error> {
        Err(serde::ser::Error::custom("expected struct"))
    }

    fn serialize_newtype_struct<T: ?Sized + serde::Serialize>(
        self,
        _name: &'static str,
        _value: &T,
    ) -> Result<(), M::Error> {
        Err(serde::ser::Error::custom("expected struct"))
    }

    fn serialize_newtype_variant<T: ?Sized + serde::Serialize>(
        self,
        _name: &'static str,
        _variant_index: u32,
        _variant: &'static str,
        _value: &T,
    ) -> Result<(), M::Error> {
        Err(serde::ser::Error::custom("expected struct"))
    }

    fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, M::Error> {
        Err(serde::ser::Error::custom("expected struct"))
    }

    fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, M::Error> {
        Err(serde::ser::Error::custom("expected struct"))
    }

    fn serialize_tuple_struct(
        self,
        _name: &'static str,
        _len: usize,
    ) -> Result<Self::SerializeTupleStruct, M::Error> {
        Err(serde::ser::Error::custom("expected struct"))
    }

    fn serialize_tuple_variant(
        self,
        _name: &'static str,
        _variant_index: u32,
        _variant: &'static str,
        _len: usize,
    ) -> Result<Self::SerializeTupleVariant, M::Error> {
        Err(serde::ser::Error::custom("expected struct"))
    }

    fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, M::Error> {
        Err(serde::ser::Error::custom("expected struct"))
    }

    fn serialize_struct(
        self,
        _name: &'static str,
        _len: usize,
    ) -> Result<Self::SerializeStruct, M::Error> {
        Ok(self)
    }

    fn serialize_struct_variant(
        self,
        _name: &'static str,
        _variant_index: u32,
        _variant: &'static str,
        _len: usize,
    ) -> Result<Self::SerializeStructVariant, M::Error> {
        Err(serde::ser::Error::custom("expected struct"))
    }
}

impl<'a, M: SerializeMap> serde::ser::SerializeStruct for FlatMapSerializer<'a, M> {
    type Ok = ();
    type Error = M::Error;

    fn serialize_field<T: ?Sized + serde::Serialize>(
        &mut self,
        key: &'static str,
        value: &T,
    ) -> Result<(), M::Error> {
        self.map.serialize_entry(key, value)
    }

    fn end(self) -> Result<(), M::Error> {
        Ok(())
    }
}