1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
use std::{fs::File, io::{self, BufWriter, Stdout, Write, stdout}};
use crate::IoArg;
pub enum Output {
StdOut(Stdout),
File(File),
}
impl Output {
pub fn new(io_arg: IoArg) -> io::Result<Self> {
let ret = match io_arg {
IoArg::StdStream => Output::StdOut(stdout()),
IoArg::File(path) => Output::File(File::create(path)?),
};
Ok(ret)
}
pub fn write(&mut self) -> Box<dyn Write + '_> {
match self {
Output::StdOut(stream) => Box::new(stream.lock()),
Output::File(file) => Box::new(BufWriter::new(file)),
}
}
}