serde_more/
lib.rs

1#![doc = include_str!("../README.md")]
2#![no_std]
3
4pub use serde_more_derive::SerializeMore;
5
6use serde::ser::{SerializeMap, Serializer};
7
8/// A custom serializer that flattens struct fields into a parent map.
9///
10/// This is NOT a part of the public API and is only intended to be used
11/// in the code generated by [`SerializeMore`] derive macro.
12#[doc(hidden)]
13pub struct FlatMapSerializer<'a, M: SerializeMap> {
14    pub map: &'a mut M,
15}
16
17impl<'a, M: SerializeMap> Serializer for FlatMapSerializer<'a, M> {
18    type Ok = ();
19    type Error = M::Error;
20
21    type SerializeSeq = serde::ser::Impossible<(), M::Error>;
22    type SerializeTuple = serde::ser::Impossible<(), M::Error>;
23    type SerializeTupleStruct = serde::ser::Impossible<(), M::Error>;
24    type SerializeTupleVariant = serde::ser::Impossible<(), M::Error>;
25    type SerializeMap = serde::ser::Impossible<(), M::Error>;
26    type SerializeStruct = Self;
27    type SerializeStructVariant = serde::ser::Impossible<(), M::Error>;
28
29    fn serialize_bool(self, _v: bool) -> Result<(), M::Error> {
30        Err(serde::ser::Error::custom("expected struct"))
31    }
32
33    fn serialize_i8(self, _v: i8) -> Result<(), M::Error> {
34        Err(serde::ser::Error::custom("expected struct"))
35    }
36
37    fn serialize_i16(self, _v: i16) -> Result<(), M::Error> {
38        Err(serde::ser::Error::custom("expected struct"))
39    }
40
41    fn serialize_i32(self, _v: i32) -> Result<(), M::Error> {
42        Err(serde::ser::Error::custom("expected struct"))
43    }
44
45    fn serialize_i64(self, _v: i64) -> Result<(), M::Error> {
46        Err(serde::ser::Error::custom("expected struct"))
47    }
48
49    fn serialize_u8(self, _v: u8) -> Result<(), M::Error> {
50        Err(serde::ser::Error::custom("expected struct"))
51    }
52
53    fn serialize_u16(self, _v: u16) -> Result<(), M::Error> {
54        Err(serde::ser::Error::custom("expected struct"))
55    }
56
57    fn serialize_u32(self, _v: u32) -> Result<(), M::Error> {
58        Err(serde::ser::Error::custom("expected struct"))
59    }
60
61    fn serialize_u64(self, _v: u64) -> Result<(), M::Error> {
62        Err(serde::ser::Error::custom("expected struct"))
63    }
64
65    fn serialize_f32(self, _v: f32) -> Result<(), M::Error> {
66        Err(serde::ser::Error::custom("expected struct"))
67    }
68
69    fn serialize_f64(self, _v: f64) -> Result<(), M::Error> {
70        Err(serde::ser::Error::custom("expected struct"))
71    }
72
73    fn serialize_char(self, _v: char) -> Result<(), M::Error> {
74        Err(serde::ser::Error::custom("expected struct"))
75    }
76
77    fn serialize_str(self, _v: &str) -> Result<(), M::Error> {
78        Err(serde::ser::Error::custom("expected struct"))
79    }
80
81    fn serialize_bytes(self, _v: &[u8]) -> Result<(), M::Error> {
82        Err(serde::ser::Error::custom("expected struct"))
83    }
84
85    fn serialize_none(self) -> Result<(), M::Error> {
86        Err(serde::ser::Error::custom("expected struct"))
87    }
88
89    fn serialize_some<T: ?Sized + serde::Serialize>(self, _value: &T) -> Result<(), M::Error> {
90        Err(serde::ser::Error::custom("expected struct"))
91    }
92
93    fn serialize_unit(self) -> Result<(), M::Error> {
94        Err(serde::ser::Error::custom("expected struct"))
95    }
96
97    fn serialize_unit_struct(self, _name: &'static str) -> Result<(), M::Error> {
98        Err(serde::ser::Error::custom("expected struct"))
99    }
100
101    fn serialize_unit_variant(
102        self,
103        _name: &'static str,
104        _variant_index: u32,
105        _variant: &'static str,
106    ) -> Result<(), M::Error> {
107        Err(serde::ser::Error::custom("expected struct"))
108    }
109
110    fn serialize_newtype_struct<T: ?Sized + serde::Serialize>(
111        self,
112        _name: &'static str,
113        _value: &T,
114    ) -> Result<(), M::Error> {
115        Err(serde::ser::Error::custom("expected struct"))
116    }
117
118    fn serialize_newtype_variant<T: ?Sized + serde::Serialize>(
119        self,
120        _name: &'static str,
121        _variant_index: u32,
122        _variant: &'static str,
123        _value: &T,
124    ) -> Result<(), M::Error> {
125        Err(serde::ser::Error::custom("expected struct"))
126    }
127
128    fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, M::Error> {
129        Err(serde::ser::Error::custom("expected struct"))
130    }
131
132    fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, M::Error> {
133        Err(serde::ser::Error::custom("expected struct"))
134    }
135
136    fn serialize_tuple_struct(
137        self,
138        _name: &'static str,
139        _len: usize,
140    ) -> Result<Self::SerializeTupleStruct, M::Error> {
141        Err(serde::ser::Error::custom("expected struct"))
142    }
143
144    fn serialize_tuple_variant(
145        self,
146        _name: &'static str,
147        _variant_index: u32,
148        _variant: &'static str,
149        _len: usize,
150    ) -> Result<Self::SerializeTupleVariant, M::Error> {
151        Err(serde::ser::Error::custom("expected struct"))
152    }
153
154    fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, M::Error> {
155        Err(serde::ser::Error::custom("expected struct"))
156    }
157
158    fn serialize_struct(
159        self,
160        _name: &'static str,
161        _len: usize,
162    ) -> Result<Self::SerializeStruct, M::Error> {
163        Ok(self)
164    }
165
166    fn serialize_struct_variant(
167        self,
168        _name: &'static str,
169        _variant_index: u32,
170        _variant: &'static str,
171        _len: usize,
172    ) -> Result<Self::SerializeStructVariant, M::Error> {
173        Err(serde::ser::Error::custom("expected struct"))
174    }
175}
176
177impl<'a, M: SerializeMap> serde::ser::SerializeStruct for FlatMapSerializer<'a, M> {
178    type Ok = ();
179    type Error = M::Error;
180
181    fn serialize_field<T: ?Sized + serde::Serialize>(
182        &mut self,
183        key: &'static str,
184        value: &T,
185    ) -> Result<(), M::Error> {
186        self.map.serialize_entry(key, value)
187    }
188
189    fn end(self) -> Result<(), M::Error> {
190        Ok(())
191    }
192}