xml_data/serializer/
core.rs

1use crate::{
2	Result,
3};
4use std::borrow::Cow;
5
6/// Element that can be serialized.
7pub trait Element {
8	/// Tag for XML element
9	fn tag(&self) -> Cow<'_, str>;
10
11	/// Called by serializer to let an element serialize its attributes and inner data (text and
12	/// further elements).
13	fn serialize<S: Serializer>(&self, serializer: S) -> Result<()>;
14}
15
16/// Interface to serialize an element.
17///
18/// An element needs to serialize attributes first, then inner text and elements.
19pub trait Serializer {
20	/// Add an attribute to the serialized element
21	fn serialize_attribute(&mut self, key: &str, value: Cow<'_, str>) -> Result<()>;
22
23	/// Add inner text to the element.
24	///
25	/// Must be escaped automatically by the serializer.
26	fn serialize_text(&mut self, text: Cow<'_, str>) -> Result<()>;
27
28	/// Add an inner element
29	///
30	/// The serializer will need to determine the `Element::tag` of the element and call its
31	/// `Element::serialize` function.
32	fn serialize_element<E: Element>(&mut self, element: &E) -> Result<()>;
33}