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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use std::{
    io::{self, BufWriter},
    path::PathBuf,
    str::FromStr,
};

/// Possible choices for output streams.
/// Used by the `-o` option to the compiler.
#[derive(Default, Debug)]
pub enum OutputFile {
    #[default]
    Stdout,
    File(PathBuf),
}

impl OutputFile {
    pub fn as_path_string(&self) -> String {
        match self {
            OutputFile::Stdout => "<stdout>".to_string(),
            OutputFile::File(path) => path.to_string_lossy().to_string(),
        }
    }
}

impl FromStr for OutputFile {
    type Err = String;
    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
        match s {
            "-" => Ok(OutputFile::Stdout),
            _ => Ok(OutputFile::File(PathBuf::from(s))),
        }
    }
}

impl ToString for OutputFile {
    fn to_string(&self) -> String {
        match self {
            OutputFile::Stdout => "-".to_string(),
            OutputFile::File(p) => p.to_str().unwrap().to_string(),
        }
    }
}

impl OutputFile {
    pub fn isatty(&self) -> bool {
        match self {
            OutputFile::Stdout => atty::is(atty::Stream::Stdout),
            OutputFile::File(_) => false,
        }
    }

    pub fn get_write(&self) -> Box<dyn io::Write> {
        match self {
            OutputFile::Stdout => Box::new(BufWriter::new(std::io::stdout())),
            OutputFile::File(path) => {
                Box::new(BufWriter::new(std::fs::File::create(path).unwrap()))
            }
        }
    }
}