Skip to main content

keyvalues_serde/
ser.rs

1//! Serialize Rust types to VDF text
2
3use serde_core::{ser, Serialize};
4
5use std::io::Write;
6
7use crate::{
8    error::{Error, Result},
9    tokens::{naive::vdf_from_naive_tokens, NaiveToken},
10};
11
12/// The struct for serializing Rust values into VDF text
13///
14/// This typically doesn't need to be invoked directly when [`to_writer()`] and
15/// [`to_writer_with_key()`] can be used instead
16#[derive(Default)]
17pub struct Serializer {
18    tokens: Vec<NaiveToken>,
19}
20
21impl Serializer {
22    /// Creates a new VDF serializer
23    pub fn new() -> Self {
24        Self::default()
25    }
26}
27
28/// Serialize the `value` into an IO stream of VDF text
29///
30/// # Errors
31///
32/// This will return an error if the input can't be represented with valid VDF
33pub fn to_writer<W, T>(writer: &mut W, value: &T) -> Result<()>
34where
35    W: Write,
36    T: Serialize,
37{
38    _to_writer(writer, value, None)
39}
40
41/// Serialize the `value` into an IO stream of VDF text with a custom top level VDF key
42///
43/// # Errors
44///
45/// This will return an error if the input can't be represented with valid VDF
46pub fn to_writer_with_key<W, T>(writer: &mut W, value: &T, key: &str) -> Result<()>
47where
48    W: Write,
49    T: Serialize,
50{
51    _to_writer(writer, value, Some(key))
52}
53
54// Serialization process goes as follows:
55// value: &T
56// -> NaiveTokenStream
57// -> Vdf (fails on invalid VDF structure like nested sequences)
58// -> Formatted
59// Which is a bit of a long-winded process just to serialize some text, but it comes with
60// validation (NaiveTokenStream -> Vdf) and reuses portions from the parser (Vdf -> Formatted)
61fn _to_writer<W, T>(writer: &mut W, value: &T, maybe_key: Option<&str>) -> Result<()>
62where
63    W: Write,
64    T: Serialize,
65{
66    let mut serializer = Serializer::new();
67    value.serialize(&mut serializer)?;
68
69    if let Some(key) = maybe_key {
70        match serializer.tokens.first() {
71            // Replace the old key
72            Some(NaiveToken::Str(_old_key)) => {
73                serializer.tokens[0] = NaiveToken::Str(key.to_owned());
74            }
75            // Push on the key
76            Some(_) => serializer.tokens.insert(0, NaiveToken::Str(key.to_owned())),
77            None => {}
78        }
79    }
80
81    let vdf = vdf_from_naive_tokens(&serializer.tokens)?;
82    write!(writer, "{vdf}")?;
83
84    Ok(())
85}
86
87/// Attempts to serialize some input to VDF text
88///
89/// # Errors
90///
91/// This will return an error if the input can't be represented with valid VDF
92pub fn to_string<T>(value: &T) -> Result<String>
93where
94    T: Serialize,
95{
96    let mut buffer = Vec::new();
97    to_writer(&mut buffer, value)?;
98    let s = String::from_utf8(buffer).expect("Input was all valid UTF-8");
99
100    Ok(s)
101}
102
103/// Attempts to serialize some input to VDF text with a custom top level VDF key
104///
105/// # Errors
106///
107/// This will return an error if the input can't be represented with valid VDF
108pub fn to_string_with_key<T>(value: &T, key: &str) -> Result<String>
109where
110    T: Serialize,
111{
112    let mut buffer = Vec::new();
113    to_writer_with_key(&mut buffer, value, key)?;
114    let s = String::from_utf8(buffer).expect("Input was all valid UTF-8");
115
116    Ok(s)
117}
118
119macro_rules! forward_serialize_as_str {
120    ( $( ( $method:ident, $ty:ty ) ),* $(,)? ) => {
121        $(
122            fn $method(self, v: $ty) -> Result<()> {
123                self.serialize_str(&v.to_string())
124            }
125        )*
126    }
127}
128
129impl ser::Serializer for &mut Serializer {
130    type Ok = ();
131
132    type Error = Error;
133
134    type SerializeSeq = Self;
135    type SerializeTuple = Self;
136    type SerializeTupleStruct = Self;
137    type SerializeTupleVariant = Self;
138    type SerializeMap = Self;
139    type SerializeStruct = Self;
140    type SerializeStructVariant = Self;
141
142    forward_serialize_as_str!(
143        (serialize_i8, i8),
144        (serialize_i16, i16),
145        (serialize_i32, i32),
146        (serialize_i64, i64),
147        (serialize_i128, i128),
148        (serialize_u8, u8),
149        (serialize_u16, u16),
150        (serialize_u32, u32),
151        (serialize_u64, u64),
152        (serialize_u128, u128),
153        (serialize_char, char),
154    );
155
156    fn serialize_str(self, v: &str) -> Result<()> {
157        self.tokens.push(NaiveToken::str(v));
158        Ok(())
159    }
160
161    fn serialize_bool(self, v: bool) -> Result<()> {
162        self.serialize_i8(v as i8)
163    }
164
165    fn serialize_f32(self, v: f32) -> Result<()> {
166        if v.is_finite() {
167            self.serialize_str(&v.to_string())
168        } else {
169            Err(Error::NonFiniteFloat(v))
170        }
171    }
172
173    fn serialize_f64(self, v: f64) -> Result<()> {
174        // TODO: include this and empty vecs and nested Option<Vec> in potential pitfalls
175        // TODO: look into this more, might be the other way around if the wiki is wrong
176        // Note: I believe floats in VDF are considered f32 so even when you use an f64 it will get
177        // converted to an f32 when serialized
178        self.serialize_f32(v as f32)
179    }
180
181    fn serialize_bytes(self, _v: &[u8]) -> Result<()> {
182        Err(Error::Unsupported("Bytes"))
183    }
184
185    fn serialize_none(self) -> Result<()> {
186        self.tokens.push(NaiveToken::Null);
187        Ok(())
188    }
189
190    fn serialize_some<T>(self, value: &T) -> Result<()>
191    where
192        T: ?Sized + Serialize,
193    {
194        // Just serializes the contained value
195        value.serialize(self)
196    }
197
198    fn serialize_unit(self) -> Result<()> {
199        Err(Error::Unsupported("Unit Type"))
200    }
201
202    fn serialize_unit_struct(self, _name: &'static str) -> Result<()> {
203        Err(Error::Unsupported("Unit Struct"))
204    }
205
206    fn serialize_unit_variant(
207        self,
208        _name: &'static str,
209        _variant_index: u32,
210        variant: &'static str,
211    ) -> Result<()> {
212        // Just pass the variant name for unit variant enums
213        self.serialize_str(variant)
214    }
215
216    fn serialize_newtype_struct<T>(self, _name: &'static str, value: &T) -> Result<()>
217    where
218        T: ?Sized + Serialize,
219    {
220        // Just a wrapper over the contained value
221        value.serialize(self)
222    }
223
224    fn serialize_newtype_variant<T>(
225        self,
226        _name: &'static str,
227        _variant_index: u32,
228        _variant: &'static str,
229        _value: &T,
230    ) -> Result<()>
231    where
232        T: ?Sized + Serialize,
233    {
234        Err(Error::Unsupported("Enum Newtype Variant"))
235    }
236
237    fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq> {
238        self.tokens.push(NaiveToken::SeqBegin);
239        Ok(self)
240    }
241
242    fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple> {
243        self.serialize_seq(Some(len))
244    }
245
246    fn serialize_tuple_struct(
247        self,
248        _name: &'static str,
249        len: usize,
250    ) -> Result<Self::SerializeTupleStruct> {
251        self.serialize_tuple(len)
252    }
253
254    fn serialize_tuple_variant(
255        self,
256        _name: &'static str,
257        _variant_index: u32,
258        _variant: &'static str,
259        _len: usize,
260    ) -> Result<Self::SerializeTupleVariant> {
261        Ok(self)
262    }
263
264    fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> {
265        self.tokens.push(NaiveToken::ObjBegin);
266        Ok(self)
267    }
268
269    fn serialize_struct(self, name: &'static str, len: usize) -> Result<Self::SerializeStruct> {
270        // The top level key is the name of the struct
271        if self.tokens.is_empty() {
272            self.serialize_str(name)?;
273        }
274
275        self.serialize_map(Some(len))
276    }
277
278    fn serialize_struct_variant(
279        self,
280        _name: &'static str,
281        _variant_index: u32,
282        _variant: &'static str,
283        _len: usize,
284    ) -> Result<Self::SerializeStructVariant> {
285        Ok(self)
286    }
287}
288
289impl ser::SerializeSeq for &mut Serializer {
290    type Ok = ();
291    type Error = Error;
292
293    fn serialize_element<T>(&mut self, value: &T) -> Result<()>
294    where
295        T: ?Sized + Serialize,
296    {
297        value.serialize(&mut **self)
298    }
299
300    fn end(self) -> Result<()> {
301        self.tokens.push(NaiveToken::SeqEnd);
302        Ok(())
303    }
304}
305
306impl ser::SerializeTuple for &mut Serializer {
307    type Ok = ();
308    type Error = Error;
309
310    fn serialize_element<T>(&mut self, value: &T) -> Result<()>
311    where
312        T: ?Sized + Serialize,
313    {
314        value.serialize(&mut **self)
315    }
316
317    fn end(self) -> Result<()> {
318        self.tokens.push(NaiveToken::SeqEnd);
319        Ok(())
320    }
321}
322
323impl ser::SerializeTupleStruct for &mut Serializer {
324    type Ok = ();
325    type Error = Error;
326
327    fn serialize_field<T>(&mut self, value: &T) -> Result<()>
328    where
329        T: ?Sized + Serialize,
330    {
331        value.serialize(&mut **self)
332    }
333
334    fn end(self) -> Result<()> {
335        self.tokens.push(NaiveToken::SeqEnd);
336        Ok(())
337    }
338}
339
340impl ser::SerializeTupleVariant for &mut Serializer {
341    type Ok = ();
342    type Error = Error;
343
344    fn serialize_field<T>(&mut self, _value: &T) -> Result<()>
345    where
346        T: ?Sized + Serialize,
347    {
348        Err(Error::Unsupported("Enum Tuple Variant"))
349    }
350
351    fn end(self) -> Result<()> {
352        Err(Error::Unsupported("Enum Tuple Variant"))
353    }
354}
355
356impl ser::SerializeMap for &mut Serializer {
357    type Ok = ();
358    type Error = Error;
359
360    fn serialize_key<T>(&mut self, key: &T) -> Result<()>
361    where
362        T: ?Sized + Serialize,
363    {
364        key.serialize(&mut **self)
365    }
366
367    fn serialize_value<T>(&mut self, value: &T) -> Result<()>
368    where
369        T: ?Sized + Serialize,
370    {
371        value.serialize(&mut **self)
372    }
373
374    fn end(self) -> Result<()> {
375        self.tokens.push(NaiveToken::ObjEnd);
376        Ok(())
377    }
378}
379
380impl ser::SerializeStruct for &mut Serializer {
381    type Ok = ();
382    type Error = Error;
383
384    fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()>
385    where
386        T: ?Sized + Serialize,
387    {
388        key.serialize(&mut **self)?;
389        value.serialize(&mut **self)
390    }
391
392    fn end(self) -> Result<()> {
393        self.tokens.push(NaiveToken::ObjEnd);
394        Ok(())
395    }
396}
397
398impl ser::SerializeStructVariant for &mut Serializer {
399    type Ok = ();
400    type Error = Error;
401
402    fn serialize_field<T>(&mut self, _key: &'static str, _value: &T) -> Result<()>
403    where
404        T: ?Sized + Serialize,
405    {
406        Err(Error::Unsupported("Enum Struct Variant"))
407    }
408
409    fn end(self) -> Result<()> {
410        Err(Error::Unsupported("Enum Struct Variant"))
411    }
412}