瀏覽代碼

Add max output files for output func

Getty Ritter 5 年之前
父節點
當前提交
ab1ab67c9b
共有 1 個文件被更改,包括 5 次插入1 次删除
  1. 5 1
      src/lib.rs

+ 5 - 1
src/lib.rs

@@ -2,6 +2,8 @@ use std::fmt::{self, Display, Formatter};
 use std::fs::OpenOptions;
 use std::io::{self, Write};
 
+const MAX_OUTPUT_FILES: u32 = 500;
+
 /// An SVG document
 pub struct SVG {
     stuff: Vec<Box<AsSVG>>,
@@ -66,7 +68,7 @@ impl SVG {
     /// Print this SVG document to stdout
     pub fn output(self, p: &str) -> io::Result<()> {
         let mut file = {
-            let mut n = 0u32;
+            let mut n = 0;
             let mut path = format!("output/{}{:05}.svg", p, n);
             let mut f = OpenOptions::new().write(true).create_new(true).open(&path);
             loop {
@@ -74,6 +76,8 @@ impl SVG {
                     Ok(_) => break,
                     Err(ref e) if e.kind() != io::ErrorKind::AlreadyExists =>
                         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;