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