1use byteorder::{BigEndian, WriteBytesExt};
2use serde::ser::{self, Serialize};
3use std::io;
4use super::error::{ProtoError, ProtoResult};
5
6#[derive(Debug)]
7pub struct Serializer<W: io::Write> {
8 writer: W
10}
11
12impl<W: io::Write> Serializer<W> {
13 pub fn from_writer(writer: W) -> Self {
14 Self {
15 writer: writer
16 }
17 }
18}
19
20impl<'a, W: io::Write> ser::Serializer for &'a mut Serializer<W> {
21 type Ok = ();
22 type Error = ProtoError;
23
24 type SerializeSeq = Self;
25 type SerializeTuple = Self;
26 type SerializeTupleStruct = Self;
27 type SerializeTupleVariant = Self;
28 type SerializeMap = Self;
29 type SerializeStruct = Self;
30 type SerializeStructVariant = Self;
31
32
33 fn serialize_bool(self, _v: bool) -> ProtoResult<()> {
34 unimplemented!()
35 }
36
37 fn serialize_i8(self, v: i8) -> ProtoResult<()> {
38 self.writer.write_i8(v).map_err(Into::into)
39 }
40
41 fn serialize_i16(self, v: i16) -> ProtoResult<()> {
42 self.writer.write_i16::<BigEndian>(v).map_err(Into::into)
43 }
44
45 fn serialize_i32(self, v: i32) -> ProtoResult<()> {
46 self.writer.write_i32::<BigEndian>(v).map_err(Into::into)
47 }
48
49 fn serialize_i64(self, v: i64) -> ProtoResult<()> {
50 self.writer.write_i64::<BigEndian>(v).map_err(Into::into)
51 }
52
53 fn serialize_u8(self, v: u8) -> ProtoResult<()> {
54 self.writer.write_u8(v).map_err(Into::into)
55 }
56
57 fn serialize_u16(self, v: u16) -> ProtoResult<()> {
58 self.writer.write_u16::<BigEndian>(v).map_err(Into::into)
59 }
60
61 fn serialize_u32(self, v: u32) -> ProtoResult<()> {
62 self.writer.write_u32::<BigEndian>(v).map_err(Into::into)
63 }
64
65 fn serialize_u64(self, v: u64) -> ProtoResult<()> {
66 self.writer.write_u64::<BigEndian>(v).map_err(Into::into)
67 }
68
69 fn serialize_f32(self, v: f32) -> ProtoResult<()> {
70 self.writer.write_f32::<BigEndian>(v).map_err(Into::into)
71 }
72
73 fn serialize_f64(self, v: f64) -> ProtoResult<()> {
74 self.writer.write_f64::<BigEndian>(v).map_err(Into::into)
75 }
76
77 fn serialize_char(self, _v: char) -> ProtoResult<()> {
78 unimplemented!()
79 }
80
81 fn serialize_str(self, v: &str) -> ProtoResult<()> {
82 self.serialize_bytes(v.as_bytes())
83 }
84
85 fn serialize_bytes(self, v: &[u8]) -> ProtoResult<()> {
86 (v.len() as u32).serialize(&mut *self)?;
87 self.writer.write_all(v).map_err(Into::into)
88 }
89
90 fn serialize_none(self) -> ProtoResult<()> {
91 unimplemented!()
92 }
93
94 fn serialize_some<T>(self, _value: &T) -> ProtoResult<()>
95 where
96 T: ?Sized + Serialize,
97 {
98 unimplemented!()
99 }
100
101 fn serialize_unit(self) -> ProtoResult<()> {
102 unimplemented!()
103 }
104
105 fn serialize_unit_struct(self, _name: &'static str) -> ProtoResult<()> {
106 unimplemented!()
107 }
108
109 fn serialize_unit_variant(
110 self,
111 _name: &'static str,
112 variant_index: u32,
113 _variant: &'static str,
114 ) -> ProtoResult<()> {
115 (variant_index as u8).serialize(self)
116 }
117
118 fn serialize_newtype_struct<T>(
119 self,
120 _name: &'static str,
121 value: &T,
122 ) -> ProtoResult<()>
123 where
124 T: ?Sized + Serialize,
125 {
126 value.serialize(&mut *self)
127 }
128
129 fn serialize_newtype_variant<T>(
130 self,
131 _name: &'static str,
132 variant_index: u32,
133 _variant: &'static str,
134 value: &T,
135 ) -> ProtoResult<()>
136 where
137 T: ?Sized + Serialize,
138 {
139 (variant_index as u8).serialize(&mut *self)?;
140 value.serialize(&mut *self)
141 }
142
143 fn serialize_seq(self, len: Option<usize>) -> ProtoResult<Self::SerializeSeq> {
144 if let Some(len) = len {
145 (len as u32).serialize(&mut *self).map(|()| self)
146 } else {
147 unimplemented!()
148 }
149 }
150
151 fn serialize_tuple(self, _len: usize) -> ProtoResult<Self::SerializeTuple> {
152 Ok(self)
153 }
154
155 fn serialize_tuple_struct(
156 self,
157 _name: &'static str,
158 _len: usize,
159 ) -> ProtoResult<Self::SerializeTupleStruct> {
160 Ok(self)
161 }
162
163 fn serialize_tuple_variant(
164 self,
165 _name: &'static str,
166 variant_index: u32,
167 _variant: &'static str,
168 _len: usize,
169 ) -> ProtoResult<Self::SerializeTupleVariant> {
170 (variant_index as u8).serialize(&mut *self)?;
171 Ok(self)
172 }
173
174 fn serialize_map(self, _len: Option<usize>) -> ProtoResult<Self::SerializeMap> {
175 unimplemented!()
176 }
177
178 fn serialize_struct(
179 self,
180 _name: &'static str,
181 _len: usize,
182 ) -> ProtoResult<Self::SerializeStruct> {
183 Ok(self)
184 }
185
186 fn serialize_struct_variant(
187 self,
188 _name: &'static str,
189 _variant_index: u32,
190 _variant: &'static str,
191 _len: usize,
192 ) -> ProtoResult<Self::SerializeStructVariant> {
193 Ok(self)
194 }
195}
196
197impl<'a, W: io::Write> ser::SerializeSeq for &'a mut Serializer<W> {
198 type Ok = ();
199 type Error = ProtoError;
200
201 fn serialize_element<T>(&mut self, value: &T) -> ProtoResult<()>
202 where
203 T: ?Sized + Serialize,
204 {
205 value.serialize(&mut **self)
206 }
207
208 fn end(self) -> ProtoResult<()> {
209 Ok(())
210 }
211}
212
213impl<'a, W: io::Write> ser::SerializeTuple for &'a mut Serializer<W> {
214 type Ok = ();
215 type Error = ProtoError;
216
217 fn serialize_element<T>(&mut self, value: &T) -> ProtoResult<()>
218 where
219 T: ?Sized + Serialize,
220 {
221 value.serialize(&mut **self)
222 }
223
224 fn end(self) -> ProtoResult<()> {
225 Ok(())
226 }
227}
228
229impl<'a, W: io::Write> ser::SerializeTupleStruct for &'a mut Serializer<W> {
230 type Ok = ();
231 type Error = ProtoError;
232
233 fn serialize_field<T>(&mut self, value: &T) -> ProtoResult<()>
234 where
235 T: ?Sized + Serialize,
236 {
237 value.serialize(&mut **self)
238 }
239
240 fn end(self) -> ProtoResult<()> {
241 Ok(())
242 }
243}
244
245impl<'a, W: io::Write> ser::SerializeTupleVariant for &'a mut Serializer<W> {
246 type Ok = ();
247 type Error = ProtoError;
248
249 fn serialize_field<T>(&mut self, value: &T) -> ProtoResult<()>
250 where
251 T: ?Sized + Serialize,
252 {
253 value.serialize(&mut **self)
254 }
255
256 fn end(self) -> ProtoResult<()> {
257 Ok(())
258 }
259}
260
261impl<'a, W: io::Write> ser::SerializeMap for &'a mut Serializer<W> {
262 type Ok = ();
263 type Error = ProtoError;
264
265 fn serialize_key<T>(&mut self, _key: &T) -> ProtoResult<()>
266 where
267 T: ?Sized + Serialize,
268 {
269 unimplemented!()
270 }
271
272 fn serialize_value<T>(&mut self, _value: &T) -> ProtoResult<()>
273 where
274 T: ?Sized + Serialize,
275 {
276 unimplemented!()
277 }
278
279 fn end(self) -> ProtoResult<()> {
280 unimplemented!()
281 }
282}
283
284impl<'a, W: io::Write> ser::SerializeStruct for &'a mut Serializer<W> {
285 type Ok = ();
286 type Error = ProtoError;
287
288 fn serialize_field<T>(&mut self, _key: &'static str, value: &T) -> ProtoResult<()>
289 where
290 T: ?Sized + Serialize,
291 {
292 value.serialize(&mut **self)
293 }
294
295 fn end(self) -> ProtoResult<()> {
296 Ok(())
297 }
298}
299
300impl<'a, W: io::Write> ser::SerializeStructVariant for &'a mut Serializer<W> {
301 type Ok = ();
302 type Error = ProtoError;
303
304 fn serialize_field<T>(&mut self, _key: &'static str, value: &T) -> ProtoResult<()>
305 where
306 T: ?Sized + Serialize,
307 {
308 value.serialize(&mut **self)
309 }
310
311 fn end(self) -> ProtoResult<()> {
312 Ok(())
313 }
314}
315
316
317pub fn to_bytes<T: Serialize>(value: &T) -> ProtoResult<Vec<u8>> {
318 let mut serializer = Serializer::from_writer(Vec::new());
319 value.serialize(&mut serializer)?;
320 Ok(serializer.writer)
321}
322