Skip to content

Commit

Permalink
feat: improved error messages that will list the invoked command.
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Mar 6, 2023
1 parent 245c95e commit a501d65
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 35 deletions.
9 changes: 5 additions & 4 deletions src/haiku.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ pub fn commands<T: AsRef<OsStr>>(path: T) -> Vec<Command> {
}

pub fn that<T: AsRef<OsStr>>(path: T) -> io::Result<()> {
commands(path)[0].status_without_output().into_result()
let cmd = &mut commands(path)[0];
cmd.status_without_output().into_result(cmd)
}

pub fn with<T: AsRef<OsStr>>(path: T, app: impl Into<String>) -> io::Result<()> {
Command::new(app.into())
.arg(path.as_ref())
let mut cmd = Command::new(app.into());
cmd.arg(path.as_ref())
.status_without_output()
.into_result()
.into_result(&cmd)
}
9 changes: 5 additions & 4 deletions src/ios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ pub fn commands<T: AsRef<OsStr>>(path: T) -> Vec<Command> {
}

pub fn that<T: AsRef<OsStr>>(path: T) -> io::Result<()> {
commands(path)[0].status_without_output().into_result()
let cmd = &mut commands(path)[0];
cmd.status_without_output().into_result(cmd)
}

pub fn with<T: AsRef<OsStr>>(path: T, app: impl Into<String>) -> io::Result<()> {
Command::new("uiopen")
.arg("--url")
let mut cmd = Command::new("uiopen");
cmd.arg("--url")
.arg(path.as_ref())
.arg("--bundleid")
.arg(app.into())
.status_without_output()
.into_result()
.into_result(&cmd)
}
16 changes: 3 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,32 +182,22 @@ pub fn with_in_background<T: AsRef<OsStr>>(
}

trait IntoResult<T> {
fn into_result(self) -> T;
fn into_result(self, cmd: &Command) -> T;
}

impl IntoResult<io::Result<()>> for io::Result<std::process::ExitStatus> {
fn into_result(self) -> io::Result<()> {
fn into_result(self, cmd: &Command) -> io::Result<()> {
match self {
Ok(status) if status.success() => Ok(()),
Ok(status) => Err(io::Error::new(
io::ErrorKind::Other,
format!("Launcher failed with {:?}", status),
format!("Launcher {cmd:?} failed with {:?}", status),
)),
Err(err) => Err(err),
}
}
}

#[cfg(windows)]
impl IntoResult<io::Result<()>> for std::os::raw::c_int {
fn into_result(self) -> io::Result<()> {
match self {
i if i > 32 => Ok(()),
_ => Err(io::Error::last_os_error()),
}
}
}

trait CommandExt {
fn status_without_output(&mut self) -> io::Result<std::process::ExitStatus>;
}
Expand Down
9 changes: 5 additions & 4 deletions src/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ pub fn commands<T: AsRef<OsStr>>(path: T) -> Vec<Command> {
}

pub fn that<T: AsRef<OsStr>>(path: T) -> io::Result<()> {
commands(path)[0].status_without_output().into_result()
let cmd = &mut commands(path)[0];
cmd.status_without_output().into_result(cmd)
}

pub fn with<T: AsRef<OsStr>>(path: T, app: impl Into<String>) -> io::Result<()> {
Command::new("/usr/bin/open")
.arg(path.as_ref())
let mut cmd = Command::new("/usr/bin/open");
cmd.arg(path.as_ref())
.arg("-a")
.arg(app.into())
.status_without_output()
.into_result()
.into_result(&cmd)
}
12 changes: 6 additions & 6 deletions src/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ pub fn commands<T: AsRef<OsStr>>(path: T) -> Vec<Command> {

pub fn that<T: AsRef<OsStr>>(path: T) -> io::Result<()> {
let mut last_err = None;
for mut command in commands(path) {
match command.status_without_output() {
for mut cmd in commands(path) {
match cmd.status_without_output() {
Ok(status) => {
return Ok(status).into_result();
return Ok(status).into_result(&cmd);
}
Err(err) => last_err = Some(err),
}
Expand All @@ -40,10 +40,10 @@ pub fn that<T: AsRef<OsStr>>(path: T) -> io::Result<()> {
}

pub fn with<T: AsRef<OsStr>>(path: T, app: impl Into<String>) -> io::Result<()> {
Command::new(app.into())
.arg(path.as_ref())
let mut cmd = Command::new(app.into());
cmd.arg(path.as_ref())
.status_without_output()
.into_result()
.into_result(&cmd)
}

// Polyfill to workaround absolute path bug in wslu(wslview). In versions before
Expand Down
9 changes: 5 additions & 4 deletions src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ pub fn commands<T: AsRef<OsStr>>(path: T) -> Vec<Command> {
}

pub fn that<T: AsRef<OsStr>>(path: T) -> io::Result<()> {
commands(path)[0].status_without_output().into_result()
let cmd = &mut commands(path)[0];
cmd.status_without_output().into_result(cmd)
}

pub fn with<T: AsRef<OsStr>>(path: T, app: impl Into<String>) -> io::Result<()> {
Command::new("cmd")
.arg("/c")
let mut cmd = Command::new("cmd");
cmd.arg("/c")
.arg(app.into())
.arg(path.as_ref())
.status_without_output()
.into_result()
.into_result(&cmd)
}

0 comments on commit a501d65

Please sign in to comment.