inline-xml 0.3.2

Embed XML data directly in your Rust code
Documentation
// Copyright (C) 2023 Benjamin Stürz
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
use std::fmt::Write;
use crate::*;

impl Xml {
    pub fn into_document(self) -> Option<Document> {
        if self.0.len() != 1 { return None; }
        let root = self.0.into_iter().next().unwrap();
        if let Content::Tag(root) = root {
            Some(Document {
                root,
                xml_decl_attrs: Vec::new(),
                doctype: None,
            })
        } else {
            None
        }
    }
}

impl Document {
    /// Pass an XML attribute.
    ///
    /// # Example
    /// ``` rust
    /// use inline_xml::*;
    ///
    /// let doc = xml! { <root /> }
    ///     .into_document()
    ///     .expect("Failed to create XML document.")
    ///     .with_xml_attr("standalone", "yes");
    /// ```
    pub fn with_xml_attr(mut self, name: &str, value: impl Into<String>) -> Self {
        let value = value.into();
        if let Some((_, v)) = self.xml_decl_attrs.iter_mut().find(|(n, _)| n == name) {
            *v = value;
        } else {
            self.xml_decl_attrs.push((name.to_owned(), value));
        }
        self
    }

    /// Specify the XML version of this document.
    pub fn with_xml_version(self, version: impl Into<String>) -> Self {
        self.with_xml_attr("version", version)
    }

    /// Specify the XML encoding of this document (probably "UTF-8").
    pub fn with_xml_encoding(self, encoding: impl Into<String>) -> Self {
        self.with_xml_attr("encoding", encoding)
    }

    /// Specify the document type (eg. "html").
    pub fn with_doctype(mut self, doctype: impl Into<String>) -> Self {
        self.doctype = Some(doctype.into());
        self
    }
}

fn escape(s: &str) -> String {
    s.chars().fold(String::new(), |mut s, ch| match ch {
        '<'     => s + "&lt;",
        '>'     => s + "&gt;",
        '&'     => s + "&amp;",
        '\''    => s + "&apos;",
        '"'     => s + "&quot;",
        _       => { write!(&mut s, "{ch}").unwrap(); s },
    })
}

impl ToXml for Xml {
    fn to_xml(&self) -> Xml {
        self.clone()
    }
}

impl ToXml for Tag {
    fn to_xml(&self) -> Xml {
        Xml(vec![Content::Tag(self.clone())])
    }
}

impl ToXml for &str {
    fn to_xml(&self) -> Xml {
        Xml(vec![Content::Word(escape(self))])
    }
}

impl ToXml for String {
    fn to_xml(&self) -> Xml {
        self.as_str().to_xml()
    }
}

impl ToXml for char {
    fn to_xml(&self) -> Xml {
        Xml(vec![Content::Word(escape(&self.to_string()))])
    }
}

impl<T: ToXml> ToXml for Vec<T> {
    fn to_xml(&self) -> Xml {
        let contents = self.iter()
            .map(|x| x.to_xml())
            .map(|mut x| {
                if x.0.len() == 1 {
                    x.0.drain(0..1).next().unwrap()
                } else {
                    Content::Nested(x)
                }
            })
            .collect();
        Xml(contents)
    }
}

impl<T: ToXml> ToXml for Option<T> {
    fn to_xml(&self) -> Xml {
        match self {
            Some(x) => x.to_xml(),
            None    => Xml::default(),
        }
    }
}

impl<T: ToXml, E: ToXml> ToXml for Result<T, E> {
    fn to_xml(&self) -> Xml {
        match self {
            Ok(x)   => x.to_xml(),
            Err(x)  => x.to_xml(),
        }
    }
}

impl<T: ToXml> ToXml for &T {
    fn to_xml(&self) -> Xml {
        (*self).to_xml()
    }
}

macro_rules! simple_impl {
    ($t:ty) => {
        impl ToXml for $t {
            fn to_xml(&self) -> Xml {
                Xml(vec![Content::Word(self.to_string())])
            }
        }
    };
}

// Implement ToXml for types that produce a single word, for which escaping is not needed.
simple_impl!(u8);
simple_impl!(u16);
simple_impl!(u32);
simple_impl!(u64);
simple_impl!(u128);
simple_impl!(usize);
simple_impl!(i8);
simple_impl!(i16);
simple_impl!(i32);
simple_impl!(i64);
simple_impl!(i128);
simple_impl!(isize);
simple_impl!(bool);


impl<T, I> CollectXml for I
where
    T: ToXml,
    I: Iterator<Item = T>,
{
    fn collect_xml(self) -> Xml {
        let inner = self.map(|x| x.to_xml().0).flatten().collect();
        Xml(inner)
    }
}