use crate::error::{Error, NeverError};
use crate::utils::{spawn, PANIC_MSG};
use std::ffi::OsStr;
use std::process::{Child, Output};
pub fn execute_void_nopanic<TArg, TEnvKey, TEnvVal>(
cmd: impl AsRef<OsStr>,
args: impl IntoIterator<Item = TArg>,
envs: impl IntoIterator<Item = (TEnvKey, TEnvVal)>,
) where
TArg: AsRef<OsStr>,
TEnvKey: AsRef<OsStr>,
TEnvVal: AsRef<OsStr>,
{
let _ = spawn(cmd, args, envs).and_then(Child::wait_with_output);
}
pub fn execute_void_panic<TArg, TEnvKey, TEnvVal>(
cmd: impl AsRef<OsStr>,
args: impl IntoIterator<Item = TArg>,
envs: impl IntoIterator<Item = (TEnvKey, TEnvVal)>,
) where
TArg: AsRef<OsStr>,
TEnvKey: AsRef<OsStr>,
TEnvVal: AsRef<OsStr>,
{
let output = spawn(cmd, args, envs)
.and_then(Child::wait_with_output)
.expect(PANIC_MSG);
if !output.status.success() {
panic!("{}", PANIC_MSG)
}
}
pub fn execute_void_result<TArg, TEnvKey, TEnvVal, TError>(
cmd: impl AsRef<OsStr>,
args: impl IntoIterator<Item = TArg>,
envs: impl IntoIterator<Item = (TEnvKey, TEnvVal)>,
) -> Result<(), TError>
where
TArg: AsRef<OsStr>,
TEnvKey: AsRef<OsStr>,
TEnvVal: AsRef<OsStr>,
TError: From<Error<NeverError>>,
{
let mut process = spawn(cmd, args, envs).map_err(Error::ProcessNotSpawned)?;
let status = process.wait().map_err(Error::WaitFailed)?;
if !status.success() {
Err(Error::ProcessFailed(Output {
status,
stdout: Vec::new(),
stderr: Vec::new(),
})
.into())
} else {
Ok(())
}
}