#[cfg(unix)]
use std::os::unix::process::ExitStatusExt;
use std::{
borrow::Cow,
process::ExitStatus,
str,
time::Duration,
};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CommandOutput {
status: ExitStatus,
stdout: Vec<u8>,
stderr: Vec<u8>,
stdout_truncated: bool,
stderr_truncated: bool,
elapsed: Duration,
}
impl CommandOutput {
#[inline]
pub(crate) fn new(
status: ExitStatus,
stdout: Vec<u8>,
stderr: Vec<u8>,
stdout_truncated: bool,
stderr_truncated: bool,
elapsed: Duration,
) -> Self {
Self {
status,
stdout,
stderr,
stdout_truncated,
stderr_truncated,
elapsed,
}
}
#[inline]
pub fn exit_code(&self) -> Option<i32> {
self.status.code()
}
#[inline]
pub const fn exit_status(&self) -> &ExitStatus {
&self.status
}
#[cfg(unix)]
#[inline]
pub fn termination_signal(&self) -> Option<i32> {
self.status.signal()
}
#[inline]
pub fn stdout(&self) -> &[u8] {
&self.stdout
}
#[inline]
pub fn stderr(&self) -> &[u8] {
&self.stderr
}
#[inline]
pub fn stdout_text(&self) -> Result<&str, str::Utf8Error> {
str::from_utf8(&self.stdout)
}
#[inline]
pub fn stderr_text(&self) -> Result<&str, str::Utf8Error> {
str::from_utf8(&self.stderr)
}
#[inline]
pub fn stdout_lossy_text(&self) -> Cow<'_, str> {
String::from_utf8_lossy(&self.stdout)
}
#[inline]
pub fn stderr_lossy_text(&self) -> Cow<'_, str> {
String::from_utf8_lossy(&self.stderr)
}
#[inline]
pub const fn elapsed(&self) -> Duration {
self.elapsed
}
#[inline]
pub const fn stdout_truncated(&self) -> bool {
self.stdout_truncated
}
#[inline]
pub const fn stderr_truncated(&self) -> bool {
self.stderr_truncated
}
}