inline_xml/
impls.rs

1// Copyright (C) 2023 Benjamin Stürz
2//
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, either version 3 of the License, or
6// (at your option) any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with this program.  If not, see <http://www.gnu.org/licenses/>.
15use 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    /// Pass an XML attribute.
36    ///
37    /// # Example
38    /// ``` rust
39    /// use inline_xml::*;
40    ///
41    /// let doc = xml! { <root /> }
42    ///     .into_document()
43    ///     .expect("Failed to create XML document.")
44    ///     .with_xml_attr("standalone", "yes");
45    /// ```
46    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    /// Specify the XML version of this document.
57    pub fn with_xml_version(self, version: impl Into<String>) -> Self {
58        self.with_xml_attr("version", version)
59    }
60
61    /// Specify the XML encoding of this document (probably "UTF-8").
62    pub fn with_xml_encoding(self, encoding: impl Into<String>) -> Self {
63        self.with_xml_attr("encoding", encoding)
64    }
65
66    /// Specify the document type (eg. "html").
67    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 + "&lt;",
76        '>'     => s + "&gt;",
77        '&'     => s + "&amp;",
78        '\''    => s + "&apos;",
79        '"'     => s + "&quot;",
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
164// Implement ToXml for types that produce a single word, for which escaping is not needed.
165simple_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}