Skip to main content

docx_rs/xml_builder/
declaration.rs

1use super::{XMLBuilder, XmlEvent};
2use std::io::Write;
3use xml::writer::Result;
4
5impl<W: Write> XMLBuilder<W> {
6    /// Build XML declaration
7    /// i.e. `<?xml version="1.0" encoding="UTF-8"?>`
8    pub(crate) fn declaration(self, standalone: Option<bool>) -> Result<Self> {
9        self.write(XmlEvent::StartDocument {
10            version: super::XmlVersion::Version10,
11            encoding: Some("UTF-8"),
12            standalone,
13        })
14    }
15}
16
17#[cfg(test)]
18mod tests {
19    use super::*;
20    use std::str;
21
22    #[test]
23    fn test_declaration() -> Result<()> {
24        let b = XMLBuilder::new(Vec::new());
25        let r = b.declaration(None)?.into_inner()?.into_inner();
26        assert_eq!(
27            str::from_utf8(&r).unwrap(),
28            r#"<?xml version="1.0" encoding="UTF-8"?>"#
29        );
30        Ok(())
31    }
32}