1use std::fmt::Write;
16use crate::*;
17
18impl Xml {
19 pub fn into_document(self) -> Option<Document> {
20 if self.0.len() != 1 { return None; }
21 let root = self.0.into_iter().next().unwrap();
22 if let Content::Tag(root) = root {
23 Some(Document {
24 root,
25 xml_decl_attrs: Vec::new(),
26 doctype: None,
27 })
28 } else {
29 None
30 }
31 }
32}
33
34impl Document {
35 pub fn with_xml_attr(mut self, name: &str, value: impl Into<String>) -> Self {
47 let value = value.into();
48 if let Some((_, v)) = self.xml_decl_attrs.iter_mut().find(|(n, _)| n == name) {
49 *v = value;
50 } else {
51 self.xml_decl_attrs.push((name.to_owned(), value));
52 }
53 self
54 }
55
56 pub fn with_xml_version(self, version: impl Into<String>) -> Self {
58 self.with_xml_attr("version", version)
59 }
60
61 pub fn with_xml_encoding(self, encoding: impl Into<String>) -> Self {
63 self.with_xml_attr("encoding", encoding)
64 }
65
66 pub fn with_doctype(mut self, doctype: impl Into<String>) -> Self {
68 self.doctype = Some(doctype.into());
69 self
70 }
71}
72
73fn escape(s: &str) -> String {
74 s.chars().fold(String::new(), |mut s, ch| match ch {
75 '<' => s + "<",
76 '>' => s + ">",
77 '&' => s + "&",
78 '\'' => s + "'",
79 '"' => s + """,
80 _ => { write!(&mut s, "{ch}").unwrap(); s },
81 })
82}
83
84impl ToXml for Xml {
85 fn to_xml(&self) -> Xml {
86 self.clone()
87 }
88}
89
90impl ToXml for Tag {
91 fn to_xml(&self) -> Xml {
92 Xml(vec![Content::Tag(self.clone())])
93 }
94}
95
96impl ToXml for &str {
97 fn to_xml(&self) -> Xml {
98 Xml(vec![Content::Word(escape(self))])
99 }
100}
101
102impl ToXml for String {
103 fn to_xml(&self) -> Xml {
104 self.as_str().to_xml()
105 }
106}
107
108impl ToXml for char {
109 fn to_xml(&self) -> Xml {
110 Xml(vec![Content::Word(escape(&self.to_string()))])
111 }
112}
113
114impl<T: ToXml> ToXml for Vec<T> {
115 fn to_xml(&self) -> Xml {
116 let contents = self.iter()
117 .map(|x| x.to_xml())
118 .map(|mut x| {
119 if x.0.len() == 1 {
120 x.0.drain(0..1).next().unwrap()
121 } else {
122 Content::Nested(x)
123 }
124 })
125 .collect();
126 Xml(contents)
127 }
128}
129
130impl<T: ToXml> ToXml for Option<T> {
131 fn to_xml(&self) -> Xml {
132 match self {
133 Some(x) => x.to_xml(),
134 None => Xml::default(),
135 }
136 }
137}
138
139impl<T: ToXml, E: ToXml> ToXml for Result<T, E> {
140 fn to_xml(&self) -> Xml {
141 match self {
142 Ok(x) => x.to_xml(),
143 Err(x) => x.to_xml(),
144 }
145 }
146}
147
148impl<T: ToXml> ToXml for &T {
149 fn to_xml(&self) -> Xml {
150 (*self).to_xml()
151 }
152}
153
154macro_rules! simple_impl {
155 ($t:ty) => {
156 impl ToXml for $t {
157 fn to_xml(&self) -> Xml {
158 Xml(vec![Content::Word(self.to_string())])
159 }
160 }
161 };
162}
163
164simple_impl!(u8);
166simple_impl!(u16);
167simple_impl!(u32);
168simple_impl!(u64);
169simple_impl!(u128);
170simple_impl!(usize);
171simple_impl!(i8);
172simple_impl!(i16);
173simple_impl!(i32);
174simple_impl!(i64);
175simple_impl!(i128);
176simple_impl!(isize);
177simple_impl!(bool);
178
179
180impl<T, I> CollectXml for I
181where
182 T: ToXml,
183 I: Iterator<Item = T>,
184{
185 fn collect_xml(self) -> Xml {
186 let inner = self.map(|x| x.to_xml().0).flatten().collect();
187 Xml(inner)
188 }
189}