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> {
output: O,
command: Box<dyn CommandDisplay + Send + Sync>,
}
impl<O> OutputContext<O>
where
O: OutputLike + Send + Sync + 'static,
{
pub fn new(output: O, command: Box<dyn CommandDisplay + Send + Sync>) -> Self {
Self { output, command }
}
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 into_output_and_command(self) -> (O, Box<dyn CommandDisplay>) {
(self.output, self.command)
}
pub fn error(self) -> Error {
Error::from(self.output_error())
}
pub fn output_error(self) -> OutputError {
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 = self.output_error();
Error::from(match message {
Some(message) => ret.with_message(Box::new(message)),
None => ret,
})
}
}