intermodal/src/output_target.rs
Casey Rodarmor b25b389ae6
Rename Target to OutputTarget
Making it specific to output allows us to be more specific in the
display message, and possibly add output-specific code in the future.

type: reform
2020-04-07 19:01:16 -07:00

69 lines
1.3 KiB
Rust

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