apache_avro/serde/
util.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use crate::{Error, error::Details};
19use serde::{
20    Serialize, Serializer,
21    ser::{
22        SerializeMap, SerializeSeq, SerializeStruct, SerializeStructVariant, SerializeTuple,
23        SerializeTupleStruct, SerializeTupleVariant,
24    },
25};
26
27/// Serialize a `T: Serialize` as a `String`.
28///
29/// An error will be returned if any other function than [`Serializer::serialize_str`] is called.
30pub struct StringSerializer;
31
32impl Serializer for StringSerializer {
33    type Ok = String;
34    type Error = Error;
35    type SerializeSeq = Self;
36    type SerializeTuple = Self;
37    type SerializeTupleStruct = Self;
38    type SerializeTupleVariant = Self;
39    type SerializeMap = Self;
40    type SerializeStruct = Self;
41    type SerializeStructVariant = Self;
42
43    fn serialize_bool(self, _v: bool) -> Result<Self::Ok, Self::Error> {
44        Err(Details::MapFieldExpectedString.into())
45    }
46
47    fn serialize_i8(self, _v: i8) -> Result<Self::Ok, Self::Error> {
48        Err(Details::MapFieldExpectedString.into())
49    }
50
51    fn serialize_i16(self, _v: i16) -> Result<Self::Ok, Self::Error> {
52        Err(Details::MapFieldExpectedString.into())
53    }
54
55    fn serialize_i32(self, _v: i32) -> Result<Self::Ok, Self::Error> {
56        Err(Details::MapFieldExpectedString.into())
57    }
58
59    fn serialize_i64(self, _v: i64) -> Result<Self::Ok, Self::Error> {
60        Err(Details::MapFieldExpectedString.into())
61    }
62
63    fn serialize_u8(self, _v: u8) -> Result<Self::Ok, Self::Error> {
64        Err(Details::MapFieldExpectedString.into())
65    }
66
67    fn serialize_u16(self, _v: u16) -> Result<Self::Ok, Self::Error> {
68        Err(Details::MapFieldExpectedString.into())
69    }
70
71    fn serialize_u32(self, _v: u32) -> Result<Self::Ok, Self::Error> {
72        Err(Details::MapFieldExpectedString.into())
73    }
74
75    fn serialize_u64(self, _v: u64) -> Result<Self::Ok, Self::Error> {
76        Err(Details::MapFieldExpectedString.into())
77    }
78
79    fn serialize_f32(self, _v: f32) -> Result<Self::Ok, Self::Error> {
80        Err(Details::MapFieldExpectedString.into())
81    }
82
83    fn serialize_f64(self, _v: f64) -> Result<Self::Ok, Self::Error> {
84        Err(Details::MapFieldExpectedString.into())
85    }
86
87    fn serialize_char(self, _v: char) -> Result<Self::Ok, Self::Error> {
88        Err(Details::MapFieldExpectedString.into())
89    }
90
91    fn serialize_str(self, v: &str) -> Result<Self::Ok, Self::Error> {
92        Ok(v.to_string())
93    }
94
95    fn serialize_bytes(self, _v: &[u8]) -> Result<Self::Ok, Self::Error> {
96        Err(Details::MapFieldExpectedString.into())
97    }
98
99    fn serialize_none(self) -> Result<Self::Ok, Self::Error> {
100        Err(Details::MapFieldExpectedString.into())
101    }
102
103    fn serialize_some<T>(self, _value: &T) -> Result<Self::Ok, Self::Error>
104    where
105        T: ?Sized + Serialize,
106    {
107        Err(Details::MapFieldExpectedString.into())
108    }
109
110    fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
111        Err(Details::MapFieldExpectedString.into())
112    }
113
114    fn serialize_unit_struct(self, _name: &'static str) -> Result<Self::Ok, Self::Error> {
115        Err(Details::MapFieldExpectedString.into())
116    }
117
118    fn serialize_unit_variant(
119        self,
120        _name: &'static str,
121        _variant_index: u32,
122        _variant: &'static str,
123    ) -> Result<Self::Ok, Self::Error> {
124        Err(Details::MapFieldExpectedString.into())
125    }
126
127    fn serialize_newtype_struct<T>(
128        self,
129        _name: &'static str,
130        _value: &T,
131    ) -> Result<Self::Ok, Self::Error>
132    where
133        T: ?Sized + Serialize,
134    {
135        Err(Details::MapFieldExpectedString.into())
136    }
137
138    fn serialize_newtype_variant<T>(
139        self,
140        _name: &'static str,
141        _variant_index: u32,
142        _variant: &'static str,
143        _value: &T,
144    ) -> Result<Self::Ok, Self::Error>
145    where
146        T: ?Sized + Serialize,
147    {
148        Err(Details::MapFieldExpectedString.into())
149    }
150
151    fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
152        Err(Details::MapFieldExpectedString.into())
153    }
154
155    fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, Self::Error> {
156        Err(Details::MapFieldExpectedString.into())
157    }
158
159    fn serialize_tuple_struct(
160        self,
161        _name: &'static str,
162        _len: usize,
163    ) -> Result<Self::SerializeTupleStruct, Self::Error> {
164        Err(Details::MapFieldExpectedString.into())
165    }
166
167    fn serialize_tuple_variant(
168        self,
169        _name: &'static str,
170        _variant_index: u32,
171        _variant: &'static str,
172        _len: usize,
173    ) -> Result<Self::SerializeTupleVariant, Self::Error> {
174        Err(Details::MapFieldExpectedString.into())
175    }
176
177    fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
178        Err(Details::MapFieldExpectedString.into())
179    }
180
181    fn serialize_struct(
182        self,
183        _name: &'static str,
184        _len: usize,
185    ) -> Result<Self::SerializeStruct, Self::Error> {
186        Err(Details::MapFieldExpectedString.into())
187    }
188
189    fn serialize_struct_variant(
190        self,
191        _name: &'static str,
192        _variant_index: u32,
193        _variant: &'static str,
194        _len: usize,
195    ) -> Result<Self::SerializeStructVariant, Self::Error> {
196        Err(Details::MapFieldExpectedString.into())
197    }
198}
199
200impl SerializeSeq for StringSerializer {
201    type Ok = String;
202    type Error = Error;
203
204    fn serialize_element<T>(&mut self, _value: &T) -> Result<(), Self::Error>
205    where
206        T: ?Sized + Serialize,
207    {
208        Err(Details::MapFieldExpectedString.into())
209    }
210
211    fn end(self) -> Result<Self::Ok, Self::Error> {
212        Err(Details::MapFieldExpectedString.into())
213    }
214}
215
216impl SerializeTuple for StringSerializer {
217    type Ok = String;
218    type Error = Error;
219
220    fn serialize_element<T>(&mut self, _value: &T) -> Result<(), Self::Error>
221    where
222        T: ?Sized + Serialize,
223    {
224        Err(Details::MapFieldExpectedString.into())
225    }
226
227    fn end(self) -> Result<Self::Ok, Self::Error> {
228        Err(Details::MapFieldExpectedString.into())
229    }
230}
231
232impl SerializeTupleStruct for StringSerializer {
233    type Ok = String;
234    type Error = Error;
235
236    fn serialize_field<T>(&mut self, _value: &T) -> Result<(), Self::Error>
237    where
238        T: ?Sized + Serialize,
239    {
240        Err(Details::MapFieldExpectedString.into())
241    }
242
243    fn end(self) -> Result<Self::Ok, Self::Error> {
244        Err(Details::MapFieldExpectedString.into())
245    }
246}
247
248impl SerializeTupleVariant for StringSerializer {
249    type Ok = String;
250    type Error = Error;
251
252    fn serialize_field<T>(&mut self, _value: &T) -> Result<(), Self::Error>
253    where
254        T: ?Sized + Serialize,
255    {
256        Err(Details::MapFieldExpectedString.into())
257    }
258
259    fn end(self) -> Result<Self::Ok, Self::Error> {
260        Err(Details::MapFieldExpectedString.into())
261    }
262}
263
264impl SerializeMap for StringSerializer {
265    type Ok = String;
266    type Error = Error;
267
268    fn serialize_key<T>(&mut self, _key: &T) -> Result<(), Self::Error>
269    where
270        T: ?Sized + Serialize,
271    {
272        Err(Details::MapFieldExpectedString.into())
273    }
274
275    fn serialize_value<T>(&mut self, _value: &T) -> Result<(), Self::Error>
276    where
277        T: ?Sized + Serialize,
278    {
279        Err(Details::MapFieldExpectedString.into())
280    }
281
282    fn end(self) -> Result<Self::Ok, Self::Error> {
283        Err(Details::MapFieldExpectedString.into())
284    }
285}
286
287impl SerializeStruct for StringSerializer {
288    type Ok = String;
289    type Error = Error;
290
291    fn serialize_field<T>(&mut self, _key: &'static str, _value: &T) -> Result<(), Self::Error>
292    where
293        T: ?Sized + Serialize,
294    {
295        Err(Details::MapFieldExpectedString.into())
296    }
297
298    fn end(self) -> Result<Self::Ok, Self::Error> {
299        Err(Details::MapFieldExpectedString.into())
300    }
301}
302
303impl SerializeStructVariant for StringSerializer {
304    type Ok = String;
305    type Error = Error;
306
307    fn serialize_field<T>(&mut self, _key: &'static str, _value: &T) -> Result<(), Self::Error>
308    where
309        T: ?Sized + Serialize,
310    {
311        Err(Details::MapFieldExpectedString.into())
312    }
313
314    fn end(self) -> Result<Self::Ok, Self::Error> {
315        Err(Details::MapFieldExpectedString.into())
316    }
317}