use std::io;
use std::process::{Command, Output, Stdio};
fn launch_error(error: io::Error) -> io::Error {
if error.kind() == io::ErrorKind::NotFound {
io::Error::new(
io::ErrorKind::NotFound,
"close this screen and reopen swapdex; its executable is unavailable",
)
} else {
error
}
}
pub(crate) fn output(args: &[&str]) -> io::Result<Output> {
let executable = std::env::current_exe().map_err(launch_error)?;
Command::new(executable)
.args(args)
.stdin(Stdio::null())
.output()
.map_err(launch_error)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn unrelated_launch_errors_keep_their_cause() {
let error = launch_error(io::Error::new(io::ErrorKind::PermissionDenied, "denied"));
assert_eq!(error.kind(), io::ErrorKind::PermissionDenied);
assert_eq!(error.to_string(), "denied");
}
}