Quellcode durchsuchen

Merge branch 'master' of rosencrantz:/srv/git/gunpowder_treason

Getty Ritter vor 5 Jahren
Ursprung
Commit
ed0f021314
1 geänderte Dateien mit 28 neuen und 6 gelöschten Zeilen
  1. 28 6
      src/lib.rs

+ 28 - 6
src/lib.rs

@@ -1,7 +1,6 @@
-use std::fmt::Display;
-use std::fmt::{Formatter, Error};
-use std::io;
-use std::io::Write;
+use std::fmt::{self, Display, Formatter};
+use std::fs::OpenOptions;
+use std::io::{self, Write};
 
 /// An SVG document
 pub struct SVG {
@@ -13,7 +12,7 @@ pub struct SVG {
 pub struct Inches { amt: f64 }
 
 impl Display for Inches {
-    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
+    fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
         self.amt.fmt(f)?;
         write!(f, "in")
     }
@@ -64,7 +63,30 @@ impl SVG {
         Ok(buf)
     }
 
-    pub fn write_svg<W: Write>(self, buf: &mut W) -> io::Result<()> {
+    /// Print this SVG document to stdout
+    pub fn output(self, p: &str) -> io::Result<()> {
+        let mut file = {
+            let mut n = 0u32;
+            let mut path = format!("output/{}{:05}.svg", p, n);
+            let mut f = OpenOptions::new().write(true).create_new(true).open(&path);
+            loop {
+                match f {
+                    Ok(_) => break,
+                    Err(ref e) if e.kind() != io::ErrorKind::AlreadyExists =>
+                        return Err(io::Error::new(e.kind(), "failed to create file")),
+                    _ => (),
+                }
+                n += 1;
+                path = format!("output/{}{:05}.svg", p, n);
+                f = OpenOptions::new().write(true).create_new(true).open(&path);
+            }
+            f.unwrap()
+        };
+        self.write_svg(&mut file)
+    }
+
+
+   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\"?>")?;
         xml_open(