|
@@ -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
|
|
/// An SVG document
|
|
pub struct SVG {
|
|
pub struct SVG {
|
|
@@ -13,7 +12,7 @@ pub struct SVG {
|
|
pub struct Inches { amt: f64 }
|
|
pub struct Inches { amt: f64 }
|
|
|
|
|
|
impl Display for Inches {
|
|
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)?;
|
|
self.amt.fmt(f)?;
|
|
write!(f, "in")
|
|
write!(f, "in")
|
|
}
|
|
}
|
|
@@ -64,7 +63,30 @@ impl SVG {
|
|
Ok(buf)
|
|
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;
|
|
let (w, h) = self.size;
|
|
writeln!(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>")?;
|
|
writeln!(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>")?;
|
|
xml_open(
|
|
xml_open(
|