Browse Source

Let XMLWriter add initial XML header too

Getty Ritter 5 years ago
parent
commit
84babcfef3
1 changed files with 6 additions and 2 deletions
  1. 6 2
      src/lib.rs

+ 6 - 2
src/lib.rs

@@ -29,6 +29,11 @@ pub struct XMLWriter<'a>{
 }
 
 impl<'a> XMLWriter<'a> {
+    pub fn start(buf: &'a mut Write) -> io::Result<XMLWriter<'a>> {
+        writeln!(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>")?;
+        Ok(XMLWriter { writer: buf })
+    }
+
     /// create an entire closed tag with the given attributes
     pub fn tag(&mut self, name: &str, attrs: &[(&str, &Display)]) -> io::Result<()> {
         write!(self.writer, "<{}", name)?;
@@ -119,8 +124,7 @@ impl SVG {
 
    pub fn write_svg<W: Write>(self, buf: &mut W) -> io::Result<()> {
        let (w, h) = self.size;
-       writeln!(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>")?;
-       let mut xml = XMLWriter { writer: buf };
+       let mut xml = XMLWriter::start(buf)?;
        xml.block(
            "svg",
            &[("xmlns", &"http://www.w3.org/2000/svg"),