|
@@ -1,6 +1,7 @@
|
|
use std::fmt::{self, Display, Formatter};
|
|
use std::fmt::{self, Display, Formatter};
|
|
-use std::fs::OpenOptions;
|
|
+use std::fs::{self, OpenOptions};
|
|
use std::io::{self, Write};
|
|
use std::io::{self, Write};
|
|
|
|
+use std::path::Path;
|
|
|
|
|
|
const MAX_OUTPUT_FILES: u32 = 500;
|
|
const MAX_OUTPUT_FILES: u32 = 500;
|
|
|
|
|
|
@@ -67,24 +68,25 @@ impl SVG {
|
|
|
|
|
|
/// Print this SVG document to stdout
|
|
/// Print this SVG document to stdout
|
|
pub fn output(self, p: &str) -> io::Result<()> {
|
|
pub fn output(self, p: &str) -> io::Result<()> {
|
|
|
|
+ let output_dir = Path::new("output");
|
|
|
|
+ if !output_dir.is_dir() {
|
|
|
|
+ fs::create_dir(output_dir)?;
|
|
|
|
+ }
|
|
|
|
+
|
|
let mut file = {
|
|
let mut file = {
|
|
let mut n = 0;
|
|
let mut n = 0;
|
|
let mut path = format!("output/{}{:05}.svg", p, n);
|
|
let mut path = format!("output/{}{:05}.svg", p, n);
|
|
- let mut f = OpenOptions::new().write(true).create_new(true).open(&path);
|
|
+ while Path::new(&path).exists() {
|
|
- loop {
|
|
+ if n > MAX_OUTPUT_FILES {
|
|
- match f {
|
|
+ return Err(io::Error::new(
|
|
- Ok(_) => break,
|
|
+ io::ErrorKind::Other,
|
|
- Err(ref e) if e.kind() != io::ErrorKind::AlreadyExists =>
|
|
+ "gunpowder_treason: too many output files",
|
|
- return Err(io::Error::new(e.kind(), "failed to create file")),
|
|
+ ));
|
|
- _ if n > MAX_OUTPUT_FILES =>
|
|
|
|
- return Err(io::Error::new(io::ErrorKind::Other, "Too many output files already")),
|
|
|
|
- _ => (),
|
|
|
|
}
|
|
}
|
|
n += 1;
|
|
n += 1;
|
|
path = format!("output/{}{:05}.svg", p, n);
|
|
path = format!("output/{}{:05}.svg", p, n);
|
|
- f = OpenOptions::new().write(true).create_new(true).open(&path);
|
|
|
|
}
|
|
}
|
|
- f.unwrap()
|
|
+ OpenOptions::new().write(true).create_new(true).open(&path)?
|
|
};
|
|
};
|
|
self.write_svg(&mut file)
|
|
self.write_svg(&mut file)
|
|
}
|
|
}
|