serde_diff/
counting_serializer.rs

1use serde::{ser, Serialize};
2
3/// This is a serializer that counts the elements in a sequence
4pub(crate) struct CountingSerializer {
5    pub(crate) num_elements: usize,
6}
7
8/// This is a dummy error type for CountingSerializer. Currently we don't expect the serializer
9/// to fail, so it's empty for now
10#[derive(Debug)]
11pub(crate) struct CountingSerializerError;
12
13impl std::fmt::Display for CountingSerializerError {
14    fn fmt(&self, _f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
15        unimplemented!()
16    }
17}
18impl std::error::Error for CountingSerializerError {
19    fn description(&self) -> &str {
20        ""
21    }
22    fn cause(&self) -> Option<&dyn std::error::Error> {
23        None
24    }
25    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
26        None
27    }
28}
29impl ser::Error for CountingSerializerError {
30    fn custom<T>(_msg: T) -> Self
31    where
32        T: std::fmt::Display,
33    {
34        CountingSerializerError
35    }
36}
37
38impl<'a> ser::Serializer for &'a mut CountingSerializer {
39    type Ok = ();
40    type Error = CountingSerializerError;
41
42    type SerializeSeq = Self;
43    type SerializeTuple = ser::Impossible<(), Self::Error>;
44    type SerializeTupleStruct = ser::Impossible<(), Self::Error>;
45    type SerializeTupleVariant = ser::Impossible<(), Self::Error>;
46    type SerializeMap = ser::Impossible<(), Self::Error>;
47    type SerializeStruct = ser::Impossible<(), Self::Error>;
48    type SerializeStructVariant = ser::Impossible<(), Self::Error>;
49
50    fn serialize_bool(self, _v: bool) -> Result<(), Self::Error> {
51        unimplemented!()
52    }
53
54    fn serialize_i8(self, _v: i8) -> Result<(), Self::Error> {
55        unimplemented!()
56    }
57
58    fn serialize_i16(self, _v: i16) -> Result<(), Self::Error> {
59        unimplemented!()
60    }
61
62    fn serialize_i32(self, _v: i32) -> Result<(), Self::Error> {
63        unimplemented!()
64    }
65
66    fn serialize_i64(self, _v: i64) -> Result<(), Self::Error> {
67        unimplemented!()
68    }
69
70    fn serialize_u8(self, _v: u8) -> Result<(), Self::Error> {
71        unimplemented!()
72    }
73
74    fn serialize_u16(self, _v: u16) -> Result<(), Self::Error> {
75        unimplemented!()
76    }
77
78    fn serialize_u32(self, _v: u32) -> Result<(), Self::Error> {
79        unimplemented!()
80    }
81
82    fn serialize_u64(self, _v: u64) -> Result<(), Self::Error> {
83        unimplemented!()
84    }
85
86    fn serialize_f32(self, _v: f32) -> Result<(), Self::Error> {
87        unimplemented!()
88    }
89
90    fn serialize_f64(self, _v: f64) -> Result<(), Self::Error> {
91        unimplemented!()
92    }
93
94    fn serialize_char(self, _v: char) -> Result<(), Self::Error> {
95        unimplemented!()
96    }
97
98    fn serialize_str(self, _v: &str) -> Result<(), Self::Error> {
99        unimplemented!()
100    }
101
102    fn serialize_bytes(self, _v: &[u8]) -> Result<(), Self::Error> {
103        unimplemented!()
104    }
105
106    fn serialize_none(self) -> Result<(), Self::Error> {
107        unimplemented!()
108    }
109
110    fn serialize_some<T>(self, _value: &T) -> Result<(), Self::Error>
111    where
112        T: ?Sized + Serialize,
113    {
114        unimplemented!()
115    }
116
117    fn serialize_unit(self) -> Result<(), Self::Error> {
118        unimplemented!()
119    }
120
121    fn serialize_unit_struct(self, _name: &'static str) -> Result<(), Self::Error> {
122        unimplemented!()
123    }
124
125    fn serialize_unit_variant(
126        self,
127        _name: &'static str,
128        _variant_index: u32,
129        _variant: &'static str,
130    ) -> Result<(), Self::Error> {
131        unimplemented!()
132    }
133
134    fn serialize_newtype_struct<T>(self, _name: &'static str, _value: &T) -> Result<(), Self::Error>
135    where
136        T: ?Sized + Serialize,
137    {
138        unimplemented!()
139    }
140
141    fn serialize_newtype_variant<T>(
142        self,
143        _name: &'static str,
144        _variant_index: u32,
145        _variant: &'static str,
146        _value: &T,
147    ) -> Result<(), Self::Error>
148    where
149        T: ?Sized + Serialize,
150    {
151        unimplemented!()
152    }
153
154    fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
155        Ok(self)
156    }
157
158    fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, Self::Error> {
159        unimplemented!()
160    }
161
162    fn serialize_tuple_struct(
163        self,
164        _name: &'static str,
165        _len: usize,
166    ) -> Result<Self::SerializeTupleStruct, Self::Error> {
167        unimplemented!()
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, Self::Error> {
177        unimplemented!()
178    }
179
180    fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
181        unimplemented!()
182    }
183
184    fn serialize_struct(
185        self,
186        _name: &'static str,
187        _len: usize,
188    ) -> Result<Self::SerializeStruct, Self::Error> {
189        unimplemented!()
190    }
191
192    fn serialize_struct_variant(
193        self,
194        _name: &'static str,
195        _variant_index: u32,
196        _variant: &'static str,
197        _len: usize,
198    ) -> Result<Self::SerializeStructVariant, Self::Error> {
199        unimplemented!()
200    }
201}
202
203impl<'a> ser::SerializeSeq for &'a mut CountingSerializer {
204    type Ok = ();
205    type Error = CountingSerializerError;
206
207    fn serialize_element<T>(&mut self, _value: &T) -> Result<(), Self::Error>
208    where
209        T: ?Sized + Serialize,
210    {
211        self.num_elements += 1;
212        Ok(())
213    }
214
215    fn end(self) -> Result<(), Self::Error> {
216        Ok(())
217    }
218}