use std::io::Write;
use crate::{Result, XMLElement, XMLVersion};
pub struct XML {
version: XMLVersion,
encoding: String,
standalone: Option<bool>,
sort_attributes: bool,
indent: bool,
root: Option<XMLElement>,
}
impl XML {
pub(crate) fn new(
version: XMLVersion,
encoding: String,
standalone: Option<bool>,
indent: bool,
sort_attributes: bool,
) -> Self {
Self {
version,
encoding,
standalone,
indent,
sort_attributes,
root: None,
}
}
pub fn set_root_element(&mut self, element: XMLElement) {
self.root = Some(element);
}
pub fn generate<W: Write>(self, mut writer: W) -> Result<()> {
let standalone_attribute = match self.standalone {
Some(_) => r#" standalone="yes""#.to_string(),
None => String::default(),
};
writeln!(
writer,
r#"<?xml version="{}" encoding="{}"{}?>"#,
self.version.to_string(),
self.encoding,
standalone_attribute
)?;
if let Some(elem) = &self.root {
elem.render(&mut writer, self.sort_attributes, self.indent)?;
}
Ok(())
}
}