8e3f5516af
If a user passes `--input foo`, print "Searching `foo` for files…", instead of the resolved, absolute path to `foo`, since the former is what the user typed in. This was way harder, and had way more edge cases, than I thought it would be! One takaway, lexical path cleaning is excellent. type: changed fixes: - https://github.com/casey/intermodal/issues/252 - https://github.com/casey/intermodal/issues/332
86 lines
1.7 KiB
Rust
86 lines
1.7 KiB
Rust
use crate::common::*;
|
|
|
|
#[derive(PartialEq, Debug, Clone)]
|
|
pub(crate) enum InputTarget {
|
|
Path(PathBuf),
|
|
Stdin,
|
|
}
|
|
|
|
impl InputTarget {
|
|
pub(crate) fn try_from_os_str(text: &OsStr) -> Result<Self, OsString> {
|
|
text
|
|
.try_into()
|
|
.map_err(|err: Error| OsString::from(err.to_string()))
|
|
}
|
|
}
|
|
|
|
impl TryFrom<&OsStr> for InputTarget {
|
|
type Error = Error;
|
|
|
|
fn try_from(text: &OsStr) -> Result<Self, Self::Error> {
|
|
if text.is_empty() {
|
|
return Err(Error::InputTargetEmpty);
|
|
};
|
|
|
|
if text == OsStr::new("-") {
|
|
Ok(Self::Stdin)
|
|
} else {
|
|
Ok(Self::Path(text.into()))
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Display for InputTarget {
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
|
match self {
|
|
Self::Stdin => write!(f, "standard input"),
|
|
Self::Path(path) => write!(f, "`{}`", path.display()),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<P: AsRef<Path>> PartialEq<P> for InputTarget {
|
|
fn eq(&self, other: &P) -> bool {
|
|
match self {
|
|
Self::Path(path) => path == other.as_ref(),
|
|
Self::Stdin => Path::new("-") == other.as_ref(),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn file() {
|
|
assert_eq!(
|
|
InputTarget::try_from(OsStr::new("foo")).unwrap(),
|
|
InputTarget::Path("foo".into()),
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn stdio() {
|
|
assert_eq!(
|
|
InputTarget::try_from(OsStr::new("-")).unwrap(),
|
|
InputTarget::Stdin
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn display_file() {
|
|
let path = PathBuf::from("./path");
|
|
let have = InputTarget::Path(path).to_string();
|
|
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);
|
|
}
|
|
}
|