use std::borrow::Borrow;
use std::fmt::Debug;
use std::fmt::Display;
use std::process::ExitStatus;
#[cfg(doc)]
use std::process::Command;
#[cfg(doc)]
use std::process::Output;
#[cfg(doc)]
use crate::CommandExt;
use crate::CommandDisplay;
use crate::Error;
use crate::OutputError;
use crate::OutputLike;
pub struct OutputContext<O> {
pub(crate) output: O,
pub(crate) command: Box<dyn CommandDisplay + Send + Sync>,
}
impl<O> OutputContext<O>
where
O: OutputLike + Send + Sync + 'static,
{
pub fn into_output(self) -> O {
self.output
}
pub fn output(&self) -> &O {
&self.output
}
pub fn status(&self) -> ExitStatus {
self.output.status()
}
pub fn command(&self) -> &(dyn CommandDisplay + Send + Sync) {
self.command.borrow()
}
pub fn into_command(self) -> Box<dyn CommandDisplay> {
self.command
}
pub fn error(self) -> Error {
Error::from(OutputError::new(self.command, Box::new(self.output)))
}
pub fn error_msg<E>(self, message: E) -> Error
where
E: Debug + Display + Send + Sync + 'static,
{
Error::from(
OutputError::new(self.command, Box::new(self.output)).with_message(Box::new(message)),
)
}
pub(crate) fn maybe_error_msg<E>(self, message: Option<E>) -> Error
where
E: Debug + Display + Send + Sync + 'static,
{
let ret = OutputError::new(self.command, Box::new(self.output));
Error::from(match message {
Some(message) => ret.with_message(Box::new(message)),
None => ret,
})
}
}