1mod child;
2mod map;
3mod plain;
4mod seq;
5mod tuple;
6mod writer;
7
8use self::{child::ChildSerializer, map::StructSerializer, tuple::TupleSerializer};
9use crate::{
10 config::{Namespaces, SerdeXml},
11 error::{Error, Result},
12};
13use log::trace;
14use serde::Serialize;
15use std::io::Write;
16use writer::Writer;
17use xml::EventWriter;
18
19pub fn to_string<S: Serialize>(value: &S) -> Result<String> {
40 let mut buffer = Vec::new();
41 to_writer(&mut buffer, value)?;
42 Ok(String::from_utf8(buffer)?)
43}
44
45pub fn to_writer<W: Write, S: Serialize>(writer: W, value: &S) -> Result<()> {
69 let mut serializer = Serializer::from_config(SerdeXml::default(), writer);
70 value.serialize(&mut serializer)
71}
72
73pub struct Serializer<W> {
75 writer: Writer<W>,
76}
77
78impl<W: Write> Serializer<W> {
79 pub fn new(writer: EventWriter<W>) -> Self {
80 Self {
81 writer: Writer::new(writer, Namespaces::default()),
82 }
83 }
84
85 pub fn new_from_writer(writer: W) -> Self {
86 Self::from_config(SerdeXml::default(), writer)
87 }
88
89 pub(crate) fn from_config(config: SerdeXml, sink: W) -> Self {
90 Self {
91 writer: Writer::new(config.emitter.create_writer(sink), config.namespaces),
92 }
93 }
94}
95
96impl<'a, W: Write> serde::ser::Serializer for &'a mut Serializer<W> {
97 type Ok = ();
98 type Error = Error;
99
100 type SerializeSeq = serde::ser::Impossible<Self::Ok, Self::Error>;
101 type SerializeTuple = serde::ser::Impossible<Self::Ok, Self::Error>;
102 type SerializeTupleStruct = TupleSerializer<'a, W>;
103 type SerializeTupleVariant = TupleSerializer<'a, W>;
104 type SerializeMap = serde::ser::Impossible<Self::Ok, Self::Error>;
105 type SerializeStruct = StructSerializer<'a, W>;
106 type SerializeStructVariant = StructSerializer<'a, W>;
107
108 fn serialize_bool(self, _v: bool) -> Result<Self::Ok> {
109 Err(Error::Unsupported("bool in document root"))
110 }
111
112 fn serialize_i8(self, _v: i8) -> Result<Self::Ok> {
113 Err(Error::Unsupported("integer in document root"))
114 }
115
116 fn serialize_i16(self, _v: i16) -> Result<Self::Ok> {
117 Err(Error::Unsupported("integer in document root"))
118 }
119
120 fn serialize_i32(self, _v: i32) -> Result<Self::Ok> {
121 Err(Error::Unsupported("integer in document root"))
122 }
123
124 fn serialize_i64(self, _v: i64) -> Result<Self::Ok> {
125 Err(Error::Unsupported("integer in document root"))
126 }
127
128 fn serialize_u8(self, _v: u8) -> Result<Self::Ok> {
129 Err(Error::Unsupported("integer in document root"))
130 }
131
132 fn serialize_u16(self, _v: u16) -> Result<Self::Ok> {
133 Err(Error::Unsupported("integer in document root"))
134 }
135
136 fn serialize_u32(self, _v: u32) -> Result<Self::Ok> {
137 Err(Error::Unsupported("integer in document root"))
138 }
139
140 fn serialize_u64(self, _v: u64) -> Result<Self::Ok> {
141 Err(Error::Unsupported("integer in document root"))
142 }
143
144 fn serialize_f32(self, _v: f32) -> Result<Self::Ok> {
145 Err(Error::Unsupported("float in document root"))
146 }
147
148 fn serialize_f64(self, _v: f64) -> Result<Self::Ok> {
149 Err(Error::Unsupported("float in document root"))
150 }
151
152 fn serialize_char(self, _v: char) -> Result<Self::Ok> {
153 Err(Error::Unsupported("char in document root"))
154 }
155
156 fn serialize_str(self, _v: &str) -> Result<Self::Ok> {
157 Err(Error::Unsupported("string in document root"))
158 }
159
160 fn serialize_bytes(self, _v: &[u8]) -> Result<Self::Ok> {
161 Err(Error::Unsupported("bytes"))
162 }
163
164 fn serialize_none(self) -> Result<Self::Ok> {
165 trace!("none");
166 Ok(())
167 }
168
169 fn serialize_some<T>(self, value: &T) -> Result<Self::Ok>
170 where
171 T: ?Sized + Serialize,
172 {
173 trace!("some");
174 value.serialize(self)
175 }
176
177 fn serialize_unit(self) -> Result<Self::Ok> {
178 Err(Error::Unsupported("unit in document root"))
179 }
180
181 fn serialize_unit_struct(self, name: &'static str) -> Result<Self::Ok> {
182 trace!("unit struct '{name}'");
183 self.writer.start_element(name)?;
184 self.writer.end_element()?;
185 Ok(())
186 }
187
188 fn serialize_unit_variant(
189 self,
190 name: &'static str,
191 _variant_index: u32,
192 variant: &'static str,
193 ) -> Result<Self::Ok> {
194 trace!("unit variant '{name}' '{variant}'");
195 self.writer.start_element(name)?;
196 self.writer.start_element(variant)?;
197 self.writer.end_element()?;
198 self.writer.end_element()?;
199 Ok(())
200 }
201
202 fn serialize_newtype_struct<T>(self, name: &'static str, value: &T) -> Result<Self::Ok>
203 where
204 T: ?Sized + Serialize,
205 {
206 trace!("newtype struct '{name}'");
207 value.serialize(ChildSerializer::for_newtype_struct(
208 &mut self.writer,
209 name.to_string(),
210 ))?;
211 Ok(())
212 }
213
214 fn serialize_newtype_variant<T>(
215 self,
216 name: &'static str,
217 _variant_index: u32,
218 variant: &'static str,
219 value: &T,
220 ) -> Result<Self::Ok>
221 where
222 T: ?Sized + Serialize,
223 {
224 trace!("newtype variant '{name}' '{variant}'");
225 self.writer.start_element(name)?;
226 value.serialize(ChildSerializer::new(
227 &mut self.writer,
228 Some(variant.to_string()),
229 ))?;
230 self.writer.end_element()?;
231 Ok(())
232 }
233
234 fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq> {
235 Err(Error::Unsupported("sequence in document root"))
236 }
237
238 fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple> {
239 Err(Error::Unsupported("tuple in document root"))
240 }
241
242 fn serialize_tuple_struct(
243 self,
244 name: &'static str,
245 _len: usize,
246 ) -> Result<Self::SerializeTupleStruct> {
247 trace!("tuple struct '{name}'");
248 self.writer.start_element(name)?;
249 Ok(TupleSerializer::new(&mut self.writer, true))
250 }
251
252 fn serialize_tuple_variant(
253 self,
254 name: &'static str,
255 _variant_index: u32,
256 variant: &'static str,
257 _len: usize,
258 ) -> Result<Self::SerializeTupleVariant> {
259 trace!("tuple variant '{name}' '{variant}'");
260 self.writer.start_element(name)?;
261 self.writer.start_element(variant)?;
262 Ok(TupleSerializer::new(&mut self.writer, true))
263 }
264
265 fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> {
266 Err(Error::Unsupported("map in document root"))
267 }
268
269 fn serialize_struct(self, name: &'static str, _len: usize) -> Result<Self::SerializeStruct> {
270 trace!("struct '{name}'");
271 Ok(StructSerializer::new(&mut self.writer, name.to_string()))
272 }
273
274 fn serialize_struct_variant(
275 self,
276 name: &'static str,
277 _variant_index: u32,
278 variant: &'static str,
279 _len: usize,
280 ) -> Result<Self::SerializeStructVariant> {
281 trace!("struct variant '{name}' '{variant}'");
282 Ok(StructSerializer::new_variant(
283 &mut self.writer,
284 Some(name.to_string()),
285 variant.to_string(),
286 ))
287 }
288}