1 | use crate::ser::Error; |
2 | use serde::ser; |
3 | use std::str; |
4 | |
5 | pub struct PartSerializer<S> { |
6 | sink: S, |
7 | } |
8 | |
9 | impl<S: Sink> PartSerializer<S> { |
10 | pub fn new(sink: S) -> Self { |
11 | PartSerializer { sink } |
12 | } |
13 | } |
14 | |
15 | pub trait Sink: Sized { |
16 | type Ok; |
17 | |
18 | fn serialize_static_str( |
19 | self, |
20 | value: &'static str, |
21 | ) -> Result<Self::Ok, Error>; |
22 | |
23 | fn serialize_str(self, value: &str) -> Result<Self::Ok, Error>; |
24 | fn serialize_string(self, value: String) -> Result<Self::Ok, Error>; |
25 | fn serialize_none(self) -> Result<Self::Ok, Error>; |
26 | |
27 | fn serialize_some<T: ?Sized + ser::Serialize>( |
28 | self, |
29 | value: &T, |
30 | ) -> Result<Self::Ok, Error>; |
31 | |
32 | fn unsupported(self) -> Error; |
33 | } |
34 | |
35 | impl<S: Sink> ser::Serializer for PartSerializer<S> { |
36 | type Ok = S::Ok; |
37 | type Error = Error; |
38 | type SerializeSeq = ser::Impossible<S::Ok, Error>; |
39 | type SerializeTuple = ser::Impossible<S::Ok, Error>; |
40 | type SerializeTupleStruct = ser::Impossible<S::Ok, Error>; |
41 | type SerializeTupleVariant = ser::Impossible<S::Ok, Error>; |
42 | type SerializeMap = ser::Impossible<S::Ok, Error>; |
43 | type SerializeStruct = ser::Impossible<S::Ok, Error>; |
44 | type SerializeStructVariant = ser::Impossible<S::Ok, Error>; |
45 | |
46 | fn serialize_bool(self, v: bool) -> Result<S::Ok, Error> { |
47 | self.sink |
48 | .serialize_static_str(if v { "true" } else { "false" }) |
49 | } |
50 | |
51 | fn serialize_i8(self, v: i8) -> Result<S::Ok, Error> { |
52 | self.serialize_integer(v) |
53 | } |
54 | |
55 | fn serialize_i16(self, v: i16) -> Result<S::Ok, Error> { |
56 | self.serialize_integer(v) |
57 | } |
58 | |
59 | fn serialize_i32(self, v: i32) -> Result<S::Ok, Error> { |
60 | self.serialize_integer(v) |
61 | } |
62 | |
63 | fn serialize_i64(self, v: i64) -> Result<S::Ok, Error> { |
64 | self.serialize_integer(v) |
65 | } |
66 | |
67 | fn serialize_u8(self, v: u8) -> Result<S::Ok, Error> { |
68 | self.serialize_integer(v) |
69 | } |
70 | |
71 | fn serialize_u16(self, v: u16) -> Result<S::Ok, Error> { |
72 | self.serialize_integer(v) |
73 | } |
74 | |
75 | fn serialize_u32(self, v: u32) -> Result<S::Ok, Error> { |
76 | self.serialize_integer(v) |
77 | } |
78 | |
79 | fn serialize_u64(self, v: u64) -> Result<S::Ok, Error> { |
80 | self.serialize_integer(v) |
81 | } |
82 | |
83 | fn serialize_u128(self, v: u128) -> Result<S::Ok, Error> { |
84 | self.serialize_integer(v) |
85 | } |
86 | |
87 | fn serialize_i128(self, v: i128) -> Result<S::Ok, Error> { |
88 | self.serialize_integer(v) |
89 | } |
90 | |
91 | fn serialize_f32(self, v: f32) -> Result<S::Ok, Error> { |
92 | self.serialize_floating(v) |
93 | } |
94 | |
95 | fn serialize_f64(self, v: f64) -> Result<S::Ok, Error> { |
96 | self.serialize_floating(v) |
97 | } |
98 | |
99 | fn serialize_char(self, v: char) -> Result<S::Ok, Error> { |
100 | self.sink.serialize_string(v.to_string()) |
101 | } |
102 | |
103 | fn serialize_str(self, value: &str) -> Result<S::Ok, Error> { |
104 | self.sink.serialize_str(value) |
105 | } |
106 | |
107 | fn serialize_bytes(self, value: &[u8]) -> Result<S::Ok, Error> { |
108 | match str::from_utf8(value) { |
109 | Ok(value) => self.sink.serialize_str(value), |
110 | Err(err) => Err(Error::Utf8(err)), |
111 | } |
112 | } |
113 | |
114 | fn serialize_unit(self) -> Result<S::Ok, Error> { |
115 | Err(self.sink.unsupported()) |
116 | } |
117 | |
118 | fn serialize_unit_struct(self, name: &'static str) -> Result<S::Ok, Error> { |
119 | self.sink.serialize_static_str(name) |
120 | } |
121 | |
122 | fn serialize_unit_variant( |
123 | self, |
124 | _name: &'static str, |
125 | _variant_index: u32, |
126 | variant: &'static str, |
127 | ) -> Result<S::Ok, Error> { |
128 | self.sink.serialize_static_str(variant) |
129 | } |
130 | |
131 | fn serialize_newtype_struct<T: ?Sized + ser::Serialize>( |
132 | self, |
133 | _name: &'static str, |
134 | value: &T, |
135 | ) -> Result<S::Ok, Error> { |
136 | value.serialize(self) |
137 | } |
138 | |
139 | fn serialize_newtype_variant<T: ?Sized + ser::Serialize>( |
140 | self, |
141 | _name: &'static str, |
142 | _variant_index: u32, |
143 | _variant: &'static str, |
144 | _value: &T, |
145 | ) -> Result<S::Ok, Error> { |
146 | Err(self.sink.unsupported()) |
147 | } |
148 | |
149 | fn serialize_none(self) -> Result<S::Ok, Error> { |
150 | self.sink.serialize_none() |
151 | } |
152 | |
153 | fn serialize_some<T: ?Sized + ser::Serialize>( |
154 | self, |
155 | value: &T, |
156 | ) -> Result<S::Ok, Error> { |
157 | self.sink.serialize_some(value) |
158 | } |
159 | |
160 | fn serialize_seq( |
161 | self, |
162 | _len: Option<usize>, |
163 | ) -> Result<Self::SerializeSeq, Error> { |
164 | Err(self.sink.unsupported()) |
165 | } |
166 | |
167 | fn serialize_tuple( |
168 | self, |
169 | _len: usize, |
170 | ) -> Result<Self::SerializeTuple, Error> { |
171 | Err(self.sink.unsupported()) |
172 | } |
173 | |
174 | fn serialize_tuple_struct( |
175 | self, |
176 | _name: &'static str, |
177 | _len: usize, |
178 | ) -> Result<Self::SerializeTuple, Error> { |
179 | Err(self.sink.unsupported()) |
180 | } |
181 | |
182 | fn serialize_tuple_variant( |
183 | self, |
184 | _name: &'static str, |
185 | _variant_index: u32, |
186 | _variant: &'static str, |
187 | _len: usize, |
188 | ) -> Result<Self::SerializeTupleVariant, Error> { |
189 | Err(self.sink.unsupported()) |
190 | } |
191 | |
192 | fn serialize_map( |
193 | self, |
194 | _len: Option<usize>, |
195 | ) -> Result<Self::SerializeMap, Error> { |
196 | Err(self.sink.unsupported()) |
197 | } |
198 | |
199 | fn serialize_struct( |
200 | self, |
201 | _name: &'static str, |
202 | _len: usize, |
203 | ) -> Result<Self::SerializeStruct, Error> { |
204 | Err(self.sink.unsupported()) |
205 | } |
206 | |
207 | fn serialize_struct_variant( |
208 | self, |
209 | _name: &'static str, |
210 | _variant_index: u32, |
211 | _variant: &'static str, |
212 | _len: usize, |
213 | ) -> Result<Self::SerializeStructVariant, Error> { |
214 | Err(self.sink.unsupported()) |
215 | } |
216 | } |
217 | |
218 | impl<S: Sink> PartSerializer<S> { |
219 | fn serialize_integer<I>(self, value: I) -> Result<S::Ok, Error> |
220 | where |
221 | I: itoa::Integer, |
222 | { |
223 | let mut buf: Buffer = itoa::Buffer::new(); |
224 | let part: &str = buf.format(value); |
225 | ser::Serializer::serialize_str(self, v:part) |
226 | } |
227 | |
228 | fn serialize_floating<F>(self, value: F) -> Result<S::Ok, Error> |
229 | where |
230 | F: ryu::Float, |
231 | { |
232 | let mut buf: Buffer = ryu::Buffer::new(); |
233 | let part: &str = buf.format(value); |
234 | ser::Serializer::serialize_str(self, v:part) |
235 | } |
236 | } |
237 | |