2020-03-18 10:48:57 +01:00
|
|
|
use crate::common::*;
|
|
|
|
|
|
|
|
#[derive(PartialEq, Debug, Clone)]
|
|
|
|
pub(crate) enum InputTarget {
|
2020-04-03 05:04:12 +02:00
|
|
|
Path(PathBuf),
|
2020-03-18 10:48:57 +01:00
|
|
|
Stdin,
|
|
|
|
}
|
|
|
|
|
2020-04-03 05:04:12 +02:00
|
|
|
impl InputTarget {
|
|
|
|
pub(crate) fn resolve(&self, env: &Env) -> Self {
|
|
|
|
match self {
|
|
|
|
Self::Path(path) => Self::Path(env.resolve(path)),
|
|
|
|
Self::Stdin => Self::Stdin,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-18 10:48:57 +01:00
|
|
|
impl From<&OsStr> for InputTarget {
|
|
|
|
fn from(text: &OsStr) -> Self {
|
|
|
|
if text == OsStr::new("-") {
|
|
|
|
Self::Stdin
|
|
|
|
} else {
|
2020-04-03 05:04:12 +02:00
|
|
|
Self::Path(text.into())
|
2020-03-18 10:48:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for InputTarget {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
Self::Stdin => write!(f, "standard input"),
|
2020-04-03 05:04:12 +02:00
|
|
|
Self::Path(path) => write!(f, "`{}`", path.display()),
|
2020-03-18 10:48:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<P: AsRef<Path>> PartialEq<P> for InputTarget {
|
|
|
|
fn eq(&self, other: &P) -> bool {
|
|
|
|
match self {
|
2020-04-03 05:04:12 +02:00
|
|
|
Self::Path(path) => path == other.as_ref(),
|
2020-03-18 10:48:57 +01:00
|
|
|
Self::Stdin => Path::new("-") == other.as_ref(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn file() {
|
|
|
|
assert_eq!(
|
|
|
|
InputTarget::from(OsStr::new("foo")),
|
2020-04-03 05:04:12 +02:00
|
|
|
InputTarget::Path("foo".into())
|
2020-03-18 10:48:57 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn stdio() {
|
|
|
|
assert_eq!(InputTarget::from(OsStr::new("-")), InputTarget::Stdin);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn display_file() {
|
|
|
|
let path = PathBuf::from("./path");
|
2020-04-03 05:04:12 +02:00
|
|
|
let have = InputTarget::Path(path).to_string();
|
2020-03-18 10:48:57 +01:00
|
|
|
let want = "`./path`";
|
|
|
|
assert_eq!(have, want);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn display_stdio() {
|
|
|
|
let have = InputTarget::Stdin.to_string();
|
|
|
|
let want = "standard input";
|
|
|
|
assert_eq!(have, want);
|
|
|
|
}
|
|
|
|
}
|