use crate::CmdError;
use std::process::{ExitStatus, Output};
#[cfg(doc)]
use crate::ExitStatusFromCode;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NamedOutput {
name: String,
output: Output,
}
impl NamedOutput {
pub fn nonzero_captured(self) -> Result<NamedOutput, CmdError> {
crate::nonzero_captured(self.name, self.output)
}
pub fn nonzero_streamed(self) -> Result<NamedOutput, CmdError> {
crate::nonzero_streamed(self.name, self.output)
}
#[must_use]
pub fn status(&self) -> &ExitStatus {
&self.output.status
}
#[must_use]
pub fn stdout(&self) -> &Vec<u8> {
&self.output.stdout
}
#[must_use]
pub fn stderr(&self) -> &Vec<u8> {
&self.output.stderr
}
#[must_use]
pub fn stdout_lossy(&self) -> String {
String::from_utf8_lossy(&self.output.stdout).to_string()
}
#[must_use]
pub fn stderr_lossy(&self) -> String {
String::from_utf8_lossy(&self.output.stderr).to_string()
}
#[must_use]
pub fn name(&self) -> String {
self.name.clone()
}
#[must_use]
pub fn output(&self) -> &Output {
&self.output
}
}
impl AsRef<Output> for NamedOutput {
fn as_ref(&self) -> &Output {
&self.output
}
}
impl<'a> From<&'a NamedOutput> for &'a Output {
fn from(value: &'a NamedOutput) -> Self {
&value.output
}
}
impl From<NamedOutput> for Output {
fn from(value: NamedOutput) -> Self {
value.output
}
}
pub trait OutputWithName {
#[must_use]
fn named(self, s: impl AsRef<str>) -> NamedOutput;
}
impl OutputWithName for Output {
fn named(self, s: impl AsRef<str>) -> NamedOutput {
NamedOutput {
name: s.as_ref().to_string(),
output: self,
}
}
}