pub struct Declaration<'a> {
version: &'a str,
encoding: &'a str,
standalone: Option<bool>,
}
impl<'a> Declaration<'a> {
pub fn new(version: &'a str, encoding: &'a str) -> Self {
Declaration {
version,
encoding,
standalone: None,
}
}
pub fn with_standalone(mut self, standalone: bool) -> Self {
self.standalone = Some(standalone);
self
}
}
impl<'a> std::fmt::Display for Declaration<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
r#"<?xml version="{}" encoding="{}""#,
self.version, self.encoding
)?;
if let Some(standalone) = self.standalone {
let standalone_as_string = if standalone {
"yes"
}
else {
"no"
};
write!(f, r#" standalone="{}""#, standalone_as_string)?;
}
write!(f, "?>")?;
Ok(())
}
}