serde_wxf/ser/association.rs
1use super::*;
2
3#[allow(dead_code)]
4pub struct AssociationBuffer<'s> {
5 ptr: &'s mut WXFSerializer,
6 name: Option<&'static str>,
7 buffer: BTreeMap<WolframValue, WolframValue>,
8}
9
10// Some `Serialize` types are not able to hold a key and value in memory at the
11// same time so `SerializeMap` implementations are required to support
12// `serialize_key` and `serialize_value` individually.
13//
14// There is a third optional method on the `SerializeMap` trait. The
15// `serialize_entry` method allows serializers to optimize for the case where
16// key and value are both available simultaneously. In JSON it doesn't make a
17// difference so the default behavior for `serialize_entry` is fine.
18impl<'a> ser::SerializeMap for &'a mut WXFSerializer {
19 type Ok = ();
20 type Error = Error;
21
22 // The Serde data model allows map keys to be any serializable type. JSON
23 // only allows string keys so the implementation below will produce invalid
24 // JSON if the key serializes as something other than a string.
25 //
26 // A real JSON serializer would need to validate that map keys are strings.
27 // This can be done by using a different Serializer to serialize the key
28 // (instead of `&mut **self`) and having that other serializer only
29 // implement `serialize_str` and return an error on any other data type.
30 fn serialize_key<T>(&mut self, _key: &T) -> Result<()>
31 where
32 T: ?Sized + Serialize,
33 {
34 // if !self.inner.ends_with('{') {
35 // self.inner += ",";
36 // }
37 // key.serialize(&mut **self)
38 unimplemented!()
39 }
40
41 // It doesn't make a difference whether the colon is printed at the end of
42 // `serialize_key` or at the beginning of `serialize_value`. In this case
43 // the code is a bit simpler having it here.
44 fn serialize_value<T>(&mut self, _value: &T) -> Result<()>
45 where
46 T: ?Sized + Serialize,
47 {
48 // self.inner += ":";
49 // value.serialize(&mut **self)
50 unimplemented!()
51 }
52
53 fn end(self) -> Result<()> {
54 Ok(())
55 }
56}
57
58// Structs are like maps in which the keys are constrained to be compile-time
59// constant strings.
60// Name[a -> b, c -> d]
61impl<'a> ser::SerializeStruct for &'a mut WXFSerializer {
62 type Ok = ();
63 type Error = Error;
64
65 fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()>
66 where
67 T: ?Sized + Serialize,
68 {
69 value.serialize(&mut **self)?;
70 self.dict_buffer.insert(key.to_wolfram(), self.this.to_wolfram());
71 Ok(())
72 }
73
74 fn end(self) -> Result<()> {
75 self.this = self.dict_buffer.to_wolfram();
76 self.dict_buffer.clear();
77 Ok(())
78 }
79}
80
81// Similar to `SerializeTupleVariant`, here the `end` method is responsible for
82// closing both of the curly braces opened by `serialize_struct_variant`.
83impl<'a> ser::SerializeStructVariant for &'a mut WXFSerializer {
84 type Ok = ();
85 type Error = Error;
86
87 fn serialize_field<T>(&mut self, _key: &'static str, _value: &T) -> Result<()>
88 where
89 T: ?Sized + Serialize,
90 {
91 // if !self.inner.ends_with('{') {
92 // self.inner += ",";
93 // }
94 // key.serialize(&mut **self)?;
95 // self.inner += ":";
96 // value.serialize(&mut **self)
97 unimplemented!()
98 }
99
100 fn end(self) -> Result<()> {
101 Ok(())
102 }
103}