2021-08-31 12:45:48 +00:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::{self, stdout, Write};
|
|
|
|
use std::path::Path;
|
2021-09-29 10:39:51 +00:00
|
|
|
use std::panic::{RefUnwindSafe, UnwindSafe};
|
2021-08-31 12:45:48 +00:00
|
|
|
|
|
|
|
/// Returns either a new file, if a path is given, or stdout, if no path is given.
|
2021-09-29 10:39:51 +00:00
|
|
|
pub fn outfile_or_stdout<P: AsRef<Path>>(
|
|
|
|
path: Option<P>,
|
|
|
|
) -> io::Result<Box<dyn Write + Send + Sync + Unpin + RefUnwindSafe + UnwindSafe>> {
|
2021-08-31 12:45:48 +00:00
|
|
|
if let Some(path) = path {
|
|
|
|
let f = File::create(path)?;
|
2021-09-29 10:39:51 +00:00
|
|
|
Ok(Box::new(f) as Box<_>)
|
2021-08-31 12:45:48 +00:00
|
|
|
} else {
|
2021-09-29 10:39:51 +00:00
|
|
|
Ok(Box::new(stdout()) as Box<_>)
|
2021-08-31 12:45:48 +00:00
|
|
|
}
|
|
|
|
}
|