serde_wxf/ser/
mod.rs

1mod association;
2mod sequence;
3
4use serde::{ser, Serialize, Serializer};
5use wolfram_wxf::{ToWolfram, WolframValue};
6
7pub use self::{association::AssociationBuffer, sequence::SequenceBuffer};
8use crate::{Result, WXFError as Error};
9use std::collections::BTreeMap;
10
11impl ToWolfram for WXFSerializer {
12    fn to_wolfram(&self) -> WolframValue {
13        self.this.to_owned()
14    }
15}
16
17pub struct WXFSerializer {
18    this: WolframValue,
19    dict_buffer: BTreeMap<WolframValue, WolframValue>,
20}
21
22impl Default for WXFSerializer {
23    fn default() -> Self {
24        Self { this: WolframValue::Skip, dict_buffer: Default::default() }
25    }
26}
27
28impl<'a> Serializer for &'a mut WXFSerializer {
29    // The output type produced by this `Serializer` during successful
30    // serialization. Most serializers that produce text or binary output should
31    // set `Ok = ()` and serialize into an `io::Write` or buffer contained
32    // within the `Serializer` instance, as happens here. Serializers that build
33    // in-memory data structures may be simplified by using `Ok` to propagate
34    // the data structure around.
35    type Ok = ();
36
37    // The error type when some error occurs during serialization.
38    type Error = Error;
39
40    // Associated types for keeping track of additional state while serializing
41    // compound data structures like sequences and maps. In this case no
42    // additional state is required beyond what is already stored in the
43    // Serializer struct.
44    type SerializeSeq = SequenceBuffer<'a>;
45    type SerializeTuple = SequenceBuffer<'a>;
46    type SerializeTupleStruct = SequenceBuffer<'a>;
47    type SerializeTupleVariant = SequenceBuffer<'a>;
48    type SerializeMap = Self;
49    type SerializeStruct = Self;
50    type SerializeStructVariant = Self;
51
52    fn serialize_bool(self, v: bool) -> Result<()> {
53        Ok(self.this = v.to_wolfram())
54    }
55
56    fn serialize_i8(self, v: i8) -> Result<()> {
57        Ok(self.this = v.to_wolfram())
58    }
59
60    fn serialize_i16(self, v: i16) -> Result<()> {
61        Ok(self.this = v.to_wolfram())
62    }
63
64    fn serialize_i32(self, v: i32) -> Result<()> {
65        Ok(self.this = v.to_wolfram())
66    }
67
68    fn serialize_i64(self, v: i64) -> Result<()> {
69        Ok(self.this = v.to_wolfram())
70    }
71
72    fn serialize_u8(self, v: u8) -> Result<()> {
73        Ok(self.this = v.to_wolfram())
74    }
75
76    fn serialize_u16(self, v: u16) -> Result<()> {
77        Ok(self.this = v.to_wolfram())
78    }
79
80    fn serialize_u32(self, v: u32) -> Result<()> {
81        Ok(self.this = v.to_wolfram())
82    }
83
84    fn serialize_u64(self, v: u64) -> Result<()> {
85        Ok(self.this = v.to_wolfram())
86    }
87
88    fn serialize_f32(self, v: f32) -> Result<()> {
89        Ok(self.this = v.to_wolfram())
90    }
91
92    fn serialize_f64(self, v: f64) -> Result<()> {
93        Ok(self.this = v.to_wolfram())
94    }
95
96    fn serialize_char(self, v: char) -> Result<()> {
97        Ok(self.this = v.to_wolfram())
98    }
99
100    fn serialize_str(self, v: &str) -> Result<()> {
101        Ok(self.this = v.to_wolfram())
102    }
103
104    // Serialize a byte array as an array of bytes. Could also use a base64
105    // string here. Binary formats will typically represent byte arrays more
106    // compactly.
107    fn serialize_bytes(self, _v: &[u8]) -> Result<()> {
108        // WolframValue::PackedArray()
109        unimplemented!()
110    }
111
112    /// None
113    fn serialize_none(self) -> Result<()> {
114        Ok(self.this = WolframValue::symbol("None"))
115    }
116
117    fn serialize_some<T>(self, value: &T) -> Result<()>
118    where
119        T: ?Sized + Serialize,
120    {
121        value.serialize(self)
122    }
123
124    /// In Serde, unit means an anonymous value containing no data.
125    /// Nothing to output, aka Null in wolfram language
126    fn serialize_unit(self) -> Result<()> {
127        Ok(self.this = WolframValue::symbol("Null"))
128    }
129
130    // Unit struct means a named value containing no data. Again, since there is
131    // no data, map this to JSON as `null`. There is no need to serialize the
132    // name in most formats.
133    fn serialize_unit_struct(self, _name: &'static str) -> Result<()> {
134        unimplemented!()
135    }
136
137    // When serializing a unit variant (or any other kind of variant), formats
138    // can choose whether to keep track of it by index or by name. Binary
139    // formats typically use the index of the variant and human-readable formats
140    // typically use the name.
141    fn serialize_unit_variant(self, _name: &'static str, _variant_index: u32, _variant: &'static str) -> Result<()> {
142        unimplemented!()
143    }
144
145    // As is done here, serializers are encouraged to treat newtype structs as
146    // insignificant wrappers around the data they contain.
147    fn serialize_newtype_struct<T>(self, _name: &'static str, _value: &T) -> Result<()>
148    where
149        T: ?Sized + Serialize,
150    {
151        unimplemented!()
152    }
153
154    // Note that newtype variant (and all of the other variant serialization
155    // methods) refer exclusively to the "externally tagged" enum
156    // representation.
157    //
158    // Serialize this to JSON in externally tagged form as `{ NAME: VALUE }`.
159    fn serialize_newtype_variant<T>(self, _name: &'static str, _variant_index: u32, _variant: &'static str, _value: &T) -> Result<()>
160    where
161        T: ?Sized + Serialize,
162    {
163        unimplemented!()
164    }
165
166    // Now we get to the serialization of compound types.
167    //
168    // The start of the sequence, each value, and the end are three separate
169    // method calls. This one is responsible only for serializing the start,
170    // which in JSON is `[`.
171    //
172    // The length of the sequence may or may not be known ahead of time. This
173    // doesn't make a difference in JSON because the length is not represented
174    // explicitly in the serialized form. Some serializers may only be able to
175    // support sequences for which the length is known up front.
176    fn serialize_seq(self, length: Option<usize>) -> Result<Self::SerializeSeq> {
177        Ok(SequenceBuffer::new(self, None, length.unwrap_or_default()))
178    }
179
180    // Tuples look just like sequences in JSON. Some formats may be able to
181    // represent tuples more efficiently by omitting the length, since tuple
182    // means that the corresponding `Deserialize implementation will know the
183    // length without needing to look at the serialized data.
184    fn serialize_tuple(self, length: usize) -> Result<Self::SerializeTuple> {
185        Ok(SequenceBuffer::new(self, None, length))
186    }
187
188    // Tuple structs look just like sequences in JSON.
189    fn serialize_tuple_struct(self, name: &'static str, length: usize) -> Result<Self::SerializeTupleStruct> {
190        Ok(SequenceBuffer::new(self, Some(name), length))
191    }
192
193    // Tuple variants are represented in JSON as `{ NAME: [DATA...] }`. Again
194    // this method is only responsible for the externally tagged representation.
195    fn serialize_tuple_variant(
196        self,
197        name: &'static str,
198        _variant_index: u32,
199        _variant: &'static str,
200        length: usize,
201    ) -> Result<Self::SerializeTupleVariant> {
202        Ok(SequenceBuffer::new(self, Some(name), length))
203    }
204
205    // Maps are represented in JSON as `{ K: V, K: V, ... }`.
206    fn serialize_map(self, _: Option<usize>) -> Result<Self::SerializeMap> {
207        Ok(self)
208    }
209
210    // Structs look just like maps in JSON. In particular, JSON requires that we
211    // serialize the field names of the struct. Other formats may be able to
212    // omit the field names when serializing structs because the corresponding
213    // Deserialize implementation is required to know what the keys are without
214    // looking at the serialized data.
215    fn serialize_struct(self, _name: &'static str, len: usize) -> Result<Self::SerializeStruct> {
216        self.serialize_map(Some(len))
217    }
218
219    // Struct variants are represented in JSON as `{ NAME: { K: V, ... } }`.
220    // This is the externally tagged representation.
221    fn serialize_struct_variant(
222        self,
223        _name: &'static str,
224        _variant_index: u32,
225        _variant: &'static str,
226        _len: usize,
227    ) -> Result<Self::SerializeStructVariant> {
228        // self.inner += "{";
229        // variant.serialize(&mut *self)?;
230        // self.inner += ":{";
231        // Ok(self)
232        unimplemented!()
233    }
234}