Add max output files for output func
Getty Ritter
6 years ago
1 | 1 | use std::fmt::{self, Display, Formatter}; |
2 | 2 | use std::fs::OpenOptions; |
3 | 3 | use std::io::{self, Write}; |
4 | ||
5 | const MAX_OUTPUT_FILES: u32 = 500; | |
4 | 6 | |
5 | 7 | /// An SVG document |
6 | 8 | pub struct SVG { |
66 | 68 | /// Print this SVG document to stdout |
67 | 69 | pub fn output(self, p: &str) -> io::Result<()> { |
68 | 70 | let mut file = { |
69 |
let mut n = 0 |
|
71 | let mut n = 0; | |
70 | 72 | let mut path = format!("output/{}{:05}.svg", p, n); |
71 | 73 | let mut f = OpenOptions::new().write(true).create_new(true).open(&path); |
72 | 74 | loop { |
74 | 76 | Ok(_) => break, |
75 | 77 | Err(ref e) if e.kind() != io::ErrorKind::AlreadyExists => |
76 | 78 | return Err(io::Error::new(e.kind(), "failed to create file")), |
79 | _ if n > MAX_OUTPUT_FILES => | |
80 | return Err(io::Error::new(io::ErrorKind::Other, "Too many output files already")), | |
77 | 81 | _ => (), |
78 | 82 | } |
79 | 83 | n += 1; |