parco_xml/xml.rs
1use std::fmt::Formatter;
2
3/// A trait the allows you to serialize data to xml
4pub trait Xml: Sized {
5 /// serialize xml to a string
6 fn xml(&self) -> String {
7 self.display().to_string()
8 }
9
10 /// serialize xml to a formatter
11 fn serialize_xml(&self, fmt: &mut Formatter<'_>) -> std::fmt::Result;
12
13 /// serialize xml via [`std::fmt::Display`] trait
14 fn display<'a>(&'a self) -> Display<'a, Self> {
15 Display(self)
16 }
17}
18
19pub struct Display<'a, T>(&'a T);
20
21impl<'a, T: Xml> std::fmt::Display for Display<'a, T> {
22 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
23 T::serialize_xml(self.0, f)
24 }
25}