1 | use crate::ser::key::KeySink; |
2 | use crate::ser::part::PartSerializer; |
3 | use crate::ser::value::ValueSink; |
4 | use crate::ser::Error; |
5 | use form_urlencoded::Serializer as UrlEncodedSerializer; |
6 | use form_urlencoded::Target as UrlEncodedTarget; |
7 | use serde::ser; |
8 | use std::borrow::Cow; |
9 | use std::mem; |
10 | |
11 | pub struct PairSerializer<'input, 'target, Target: UrlEncodedTarget> { |
12 | urlencoder: &'target mut UrlEncodedSerializer<'input, Target>, |
13 | state: PairState, |
14 | } |
15 | |
16 | impl<'input, 'target, Target> PairSerializer<'input, 'target, Target> |
17 | where |
18 | Target: 'target + UrlEncodedTarget, |
19 | { |
20 | pub fn new( |
21 | urlencoder: &'target mut UrlEncodedSerializer<'input, Target>, |
22 | ) -> Self { |
23 | PairSerializer { |
24 | urlencoder, |
25 | state: PairState::WaitingForKey, |
26 | } |
27 | } |
28 | } |
29 | |
30 | impl<'input, 'target, Target> ser::Serializer |
31 | for PairSerializer<'input, 'target, Target> |
32 | where |
33 | Target: 'target + UrlEncodedTarget, |
34 | { |
35 | type Ok = (); |
36 | type Error = Error; |
37 | type SerializeSeq = ser::Impossible<(), Error>; |
38 | type SerializeTuple = Self; |
39 | type SerializeTupleStruct = ser::Impossible<(), Error>; |
40 | type SerializeTupleVariant = ser::Impossible<(), Error>; |
41 | type SerializeMap = ser::Impossible<(), Error>; |
42 | type SerializeStruct = ser::Impossible<(), Error>; |
43 | type SerializeStructVariant = ser::Impossible<(), Error>; |
44 | |
45 | fn serialize_bool(self, _v: bool) -> Result<(), Error> { |
46 | Err(Error::unsupported_pair()) |
47 | } |
48 | |
49 | fn serialize_i8(self, _v: i8) -> Result<(), Error> { |
50 | Err(Error::unsupported_pair()) |
51 | } |
52 | |
53 | fn serialize_i16(self, _v: i16) -> Result<(), Error> { |
54 | Err(Error::unsupported_pair()) |
55 | } |
56 | |
57 | fn serialize_i32(self, _v: i32) -> Result<(), Error> { |
58 | Err(Error::unsupported_pair()) |
59 | } |
60 | |
61 | fn serialize_i64(self, _v: i64) -> Result<(), Error> { |
62 | Err(Error::unsupported_pair()) |
63 | } |
64 | |
65 | fn serialize_u8(self, _v: u8) -> Result<(), Error> { |
66 | Err(Error::unsupported_pair()) |
67 | } |
68 | |
69 | fn serialize_u16(self, _v: u16) -> Result<(), Error> { |
70 | Err(Error::unsupported_pair()) |
71 | } |
72 | |
73 | fn serialize_u32(self, _v: u32) -> Result<(), Error> { |
74 | Err(Error::unsupported_pair()) |
75 | } |
76 | |
77 | fn serialize_u64(self, _v: u64) -> Result<(), Error> { |
78 | Err(Error::unsupported_pair()) |
79 | } |
80 | |
81 | fn serialize_f32(self, _v: f32) -> Result<(), Error> { |
82 | Err(Error::unsupported_pair()) |
83 | } |
84 | |
85 | fn serialize_f64(self, _v: f64) -> Result<(), Error> { |
86 | Err(Error::unsupported_pair()) |
87 | } |
88 | |
89 | fn serialize_char(self, _v: char) -> Result<(), Error> { |
90 | Err(Error::unsupported_pair()) |
91 | } |
92 | |
93 | fn serialize_str(self, _value: &str) -> Result<(), Error> { |
94 | Err(Error::unsupported_pair()) |
95 | } |
96 | |
97 | fn serialize_bytes(self, _value: &[u8]) -> Result<(), Error> { |
98 | Err(Error::unsupported_pair()) |
99 | } |
100 | |
101 | fn serialize_unit(self) -> Result<(), Error> { |
102 | Err(Error::unsupported_pair()) |
103 | } |
104 | |
105 | fn serialize_unit_struct(self, _name: &'static str) -> Result<(), Error> { |
106 | Err(Error::unsupported_pair()) |
107 | } |
108 | |
109 | fn serialize_unit_variant( |
110 | self, |
111 | _name: &'static str, |
112 | _variant_index: u32, |
113 | _variant: &'static str, |
114 | ) -> Result<(), Error> { |
115 | Err(Error::unsupported_pair()) |
116 | } |
117 | |
118 | fn serialize_newtype_struct<T: ?Sized + ser::Serialize>( |
119 | self, |
120 | _name: &'static str, |
121 | value: &T, |
122 | ) -> Result<(), Error> { |
123 | value.serialize(self) |
124 | } |
125 | |
126 | fn serialize_newtype_variant<T: ?Sized + ser::Serialize>( |
127 | self, |
128 | _name: &'static str, |
129 | _variant_index: u32, |
130 | _variant: &'static str, |
131 | _value: &T, |
132 | ) -> Result<(), Error> { |
133 | Err(Error::unsupported_pair()) |
134 | } |
135 | |
136 | fn serialize_none(self) -> Result<(), Error> { |
137 | Ok(()) |
138 | } |
139 | |
140 | fn serialize_some<T: ?Sized + ser::Serialize>( |
141 | self, |
142 | value: &T, |
143 | ) -> Result<(), Error> { |
144 | value.serialize(self) |
145 | } |
146 | |
147 | fn serialize_seq( |
148 | self, |
149 | _len: Option<usize>, |
150 | ) -> Result<Self::SerializeSeq, Error> { |
151 | Err(Error::unsupported_pair()) |
152 | } |
153 | |
154 | fn serialize_tuple(self, len: usize) -> Result<Self, Error> { |
155 | if len == 2 { |
156 | Ok(self) |
157 | } else { |
158 | Err(Error::unsupported_pair()) |
159 | } |
160 | } |
161 | |
162 | fn serialize_tuple_struct( |
163 | self, |
164 | _name: &'static str, |
165 | _len: usize, |
166 | ) -> Result<Self::SerializeTupleStruct, Error> { |
167 | Err(Error::unsupported_pair()) |
168 | } |
169 | |
170 | fn serialize_tuple_variant( |
171 | self, |
172 | _name: &'static str, |
173 | _variant_index: u32, |
174 | _variant: &'static str, |
175 | _len: usize, |
176 | ) -> Result<Self::SerializeTupleVariant, Error> { |
177 | Err(Error::unsupported_pair()) |
178 | } |
179 | |
180 | fn serialize_map( |
181 | self, |
182 | _len: Option<usize>, |
183 | ) -> Result<Self::SerializeMap, Error> { |
184 | Err(Error::unsupported_pair()) |
185 | } |
186 | |
187 | fn serialize_struct( |
188 | self, |
189 | _name: &'static str, |
190 | _len: usize, |
191 | ) -> Result<Self::SerializeStruct, Error> { |
192 | Err(Error::unsupported_pair()) |
193 | } |
194 | |
195 | fn serialize_struct_variant( |
196 | self, |
197 | _name: &'static str, |
198 | _variant_index: u32, |
199 | _variant: &'static str, |
200 | _len: usize, |
201 | ) -> Result<Self::SerializeStructVariant, Error> { |
202 | Err(Error::unsupported_pair()) |
203 | } |
204 | } |
205 | |
206 | impl<'input, 'target, Target> ser::SerializeTuple |
207 | for PairSerializer<'input, 'target, Target> |
208 | where |
209 | Target: 'target + UrlEncodedTarget, |
210 | { |
211 | type Ok = (); |
212 | type Error = Error; |
213 | |
214 | fn serialize_element<T: ?Sized + ser::Serialize>( |
215 | &mut self, |
216 | value: &T, |
217 | ) -> Result<(), Error> { |
218 | match mem::replace(&mut self.state, PairState::Done) { |
219 | PairState::WaitingForKey => { |
220 | let key_sink = KeySink::new(|key| Ok(key.into())); |
221 | let key_serializer = PartSerializer::new(key_sink); |
222 | self.state = PairState::WaitingForValue { |
223 | key: value.serialize(key_serializer)?, |
224 | }; |
225 | Ok(()) |
226 | } |
227 | PairState::WaitingForValue { key } => { |
228 | let result = { |
229 | let value_sink = ValueSink::new(self.urlencoder, &key); |
230 | let value_serializer = PartSerializer::new(value_sink); |
231 | value.serialize(value_serializer) |
232 | }; |
233 | if result.is_ok() { |
234 | self.state = PairState::Done; |
235 | } else { |
236 | self.state = PairState::WaitingForValue { key }; |
237 | } |
238 | result |
239 | } |
240 | PairState::Done => Err(Error::done()), |
241 | } |
242 | } |
243 | |
244 | fn end(self) -> Result<(), Error> { |
245 | if let PairState::Done = self.state { |
246 | Ok(()) |
247 | } else { |
248 | Err(Error::not_done()) |
249 | } |
250 | } |
251 | } |
252 | |
253 | enum PairState { |
254 | WaitingForKey, |
255 | WaitingForValue { key: Cow<'static, str> }, |
256 | Done, |
257 | } |
258 | |
259 | impl Error { |
260 | fn done() -> Self { |
261 | Error::Custom("this pair has already been serialized" .into()) |
262 | } |
263 | |
264 | fn not_done() -> Self { |
265 | Error::Custom("this pair has not yet been serialized" .into()) |
266 | } |
267 | |
268 | fn unsupported_pair() -> Self { |
269 | Error::Custom("unsupported pair" .into()) |
270 | } |
271 | } |
272 | |