• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use super::{plain::to_plain_string, Serializer};
2 use crate::error::{Error, Result};
3 use log::debug;
4 use serde::ser::Serialize;
5 use std::io::Write;
6 
7 pub struct MapSerializer<'ser, W: 'ser + Write> {
8     ser: &'ser mut Serializer<W>,
9     must_close_tag: bool,
10 }
11 
12 impl<'ser, W: 'ser + Write> MapSerializer<'ser, W> {
new(ser: &'ser mut Serializer<W>, must_close_tag: bool) -> Self13     pub fn new(ser: &'ser mut Serializer<W>, must_close_tag: bool) -> Self {
14         MapSerializer {
15             ser,
16             must_close_tag,
17         }
18     }
19 }
20 
21 impl<'ser, W: Write> serde::ser::SerializeMap for MapSerializer<'ser, W> {
22     type Ok = ();
23     type Error = Error;
24 
serialize_key<T>(&mut self, key: &T) -> Result<()> where T: ?Sized + Serialize,25     fn serialize_key<T>(&mut self, key: &T) -> Result<()>
26     where
27         T: ?Sized + Serialize,
28     {
29         self.ser.open_tag(&to_plain_string(key)?)?;
30         Ok(())
31     }
32 
serialize_value<T>(&mut self, value: &T) -> Result<()> where T: ?Sized + Serialize,33     fn serialize_value<T>(&mut self, value: &T) -> Result<()>
34     where
35         T: ?Sized + Serialize,
36     {
37         value.serialize(&mut *self.ser)?;
38         Ok(())
39     }
40 
end(self) -> Result<()>41     fn end(self) -> Result<()> {
42         if self.must_close_tag {
43             self.ser.end_tag()?;
44         }
45         Ok(())
46     }
47 }
48 
49 pub struct StructSerializer<'ser, W: 'ser + Write> {
50     ser: &'ser mut Serializer<W>,
51     must_close_tag: bool,
52 }
53 
54 impl<'ser, W: 'ser + Write> StructSerializer<'ser, W> {
new(ser: &'ser mut Serializer<W>, must_close_tag: bool) -> Self55     pub fn new(ser: &'ser mut Serializer<W>, must_close_tag: bool) -> Self {
56         StructSerializer {
57             ser,
58             must_close_tag,
59         }
60     }
61 
serialize_struct_field<T>(&mut self, key: &'static str, value: &T) -> Result<()> where T: ?Sized + Serialize,62     fn serialize_struct_field<T>(&mut self, key: &'static str, value: &T) -> Result<()>
63     where
64         T: ?Sized + Serialize,
65     {
66         if key.starts_with("@") {
67             debug!("attribute {}", key);
68             self.ser.add_attr(&key[1..], to_plain_string(value)?)
69         } else if key == "$value" {
70             self.ser.build_start_tag()?;
71             debug!("body");
72             value.serialize(&mut *self.ser)?;
73             Ok(())
74         } else {
75             self.ser.build_start_tag()?;
76             self.ser.open_tag(key)?;
77             debug!("field {}", key);
78             value.serialize(&mut *self.ser)?;
79             debug!("end field");
80             Ok(())
81         }
82     }
83 
after_fields(self) -> Result<()>84     fn after_fields(self) -> Result<()> {
85         self.ser.build_start_tag()?;
86         self.ser.end_tag()?;
87         if self.must_close_tag {
88             self.ser.end_tag()?;
89         }
90         Ok(())
91     }
92 }
93 
94 impl<'ser, W: 'ser + Write> serde::ser::SerializeStruct for StructSerializer<'ser, W> {
95     type Ok = ();
96     type Error = Error;
97 
serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()> where T: ?Sized + Serialize,98     fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()>
99     where
100         T: ?Sized + Serialize,
101     {
102         self.serialize_struct_field(key, value)
103     }
104 
end(self) -> Result<()>105     fn end(self) -> Result<()> {
106         self.after_fields()
107     }
108 }
109 
110 impl<'ser, W: 'ser + Write> serde::ser::SerializeStructVariant for StructSerializer<'ser, W> {
111     type Ok = ();
112     type Error = Error;
113 
serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()> where T: ?Sized + Serialize,114     fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()>
115     where
116         T: ?Sized + Serialize,
117     {
118         self.serialize_struct_field(key, value)
119     }
120 
end(self) -> Result<()>121     fn end(self) -> Result<()> {
122         self.after_fields()
123     }
124 }
125