2020-02-05 06:29:53 +01:00
|
|
|
use crate::common::*;
|
|
|
|
|
|
|
|
#[derive(PartialEq, Debug)]
|
2020-03-12 06:44:14 +01:00
|
|
|
pub(crate) enum OutputTarget {
|
2020-02-05 06:29:53 +01:00
|
|
|
File(PathBuf),
|
2020-03-12 06:44:14 +01:00
|
|
|
Stdout,
|
2020-02-05 06:29:53 +01:00
|
|
|
}
|
|
|
|
|
2020-03-12 06:44:14 +01:00
|
|
|
impl OutputTarget {
|
2020-02-05 06:29:53 +01:00
|
|
|
pub(crate) fn resolve(&self, env: &Env) -> Self {
|
|
|
|
match self {
|
|
|
|
Self::File(path) => Self::File(env.resolve(path)),
|
2020-03-12 06:44:14 +01:00
|
|
|
Self::Stdout => Self::Stdout,
|
2020-02-05 06:29:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-12 06:44:14 +01:00
|
|
|
impl From<&OsStr> for OutputTarget {
|
2020-02-05 06:29:53 +01:00
|
|
|
fn from(text: &OsStr) -> Self {
|
|
|
|
if text == OsStr::new("-") {
|
2020-03-12 06:44:14 +01:00
|
|
|
Self::Stdout
|
2020-02-05 06:29:53 +01:00
|
|
|
} else {
|
|
|
|
Self::File(text.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-12 06:44:14 +01:00
|
|
|
impl Display for OutputTarget {
|
2020-03-12 00:04:22 +01:00
|
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
|
|
|
match self {
|
2020-03-12 06:44:14 +01:00
|
|
|
Self::Stdout => write!(f, "standard output"),
|
2020-03-12 00:04:22 +01:00
|
|
|
Self::File(path) => write!(f, "`{}`", path.display()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-05 06:29:53 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn file() {
|
2020-03-12 06:44:14 +01:00
|
|
|
assert_eq!(
|
|
|
|
OutputTarget::from(OsStr::new("foo")),
|
|
|
|
OutputTarget::File("foo".into())
|
|
|
|
);
|
2020-02-05 06:29:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn stdio() {
|
2020-03-12 06:44:14 +01:00
|
|
|
assert_eq!(OutputTarget::from(OsStr::new("-")), OutputTarget::Stdout);
|
2020-02-05 06:29:53 +01:00
|
|
|
}
|
2020-03-12 00:04:22 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn display_file() {
|
|
|
|
let path = PathBuf::from("./path");
|
2020-03-12 06:44:14 +01:00
|
|
|
let have = OutputTarget::File(path).to_string();
|
2020-03-12 00:04:22 +01:00
|
|
|
let want = "`./path`";
|
|
|
|
assert_eq!(have, want);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn display_stdio() {
|
2020-03-12 06:44:14 +01:00
|
|
|
let have = OutputTarget::Stdout.to_string();
|
|
|
|
let want = "standard output";
|
2020-03-12 00:04:22 +01:00
|
|
|
assert_eq!(have, want);
|
|
|
|
}
|
2020-02-05 06:29:53 +01:00
|
|
|
}
|