Skip to content

Commit 3662a61

Browse files
authored
OutputContext: Add new, into_output_and_command, and output_error (#28)
I learned it's actually impossible (!) for consumers to implement `CommandExt` because there are no public constructors for [`OutputContext`]. Similarly, the lack of a public `maybe_error_message` method requires a tedious workaround. This introduces several new methods to fix these deficiencies: - `OutputContext::new` lets you construct an `OutputContext`. - `OutputContext::into_output_and_command` lets you deconstruct an `OutputContext`. Previously, both these fields were accessible with `into_output` and `into_command`, but you could only call one of those methods, because they both take an owned `self` receiver. - `OutputContext::output_error` is like `OutputContext::error`, but it doesn't wrap the output in an `Error`, letting you use the `OutputError::with_message` method.
1 parent 4b60eae commit 3662a61

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

src/output_context.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ impl<O> OutputContext<O>
3434
where
3535
O: OutputLike + Send + Sync + 'static,
3636
{
37+
/// Construct a new [`OutputContext`].
38+
pub fn new(output: O, command: Box<dyn CommandDisplay + Send + Sync>) -> Self {
39+
Self { output, command }
40+
}
41+
3742
/// Get the [`OutputLike`] data contained in this context object.
3843
pub fn into_output(self) -> O {
3944
self.output
@@ -61,12 +66,31 @@ where
6166
self.command
6267
}
6368

69+
/// Get the output and command contained in this context object.
70+
///
71+
/// Unlike [`OutputContext::into_output`] and [`OutputContext::into_command`], this lets you
72+
/// extract both fields.
73+
pub fn into_output_and_command(self) -> (O, Box<dyn CommandDisplay>) {
74+
(self.output, self.command)
75+
}
76+
6477
/// Construct an error that indicates this command failed, containing information about the
6578
/// command and its output.
6679
///
6780
/// See [`CommandExt`] for examples of the error format.
81+
///
82+
/// This is a thin wrapper around [`OutputContext::output_error`].
6883
pub fn error(self) -> Error {
69-
Error::from(OutputError::new(self.command, Box::new(self.output)))
84+
Error::from(self.output_error())
85+
}
86+
87+
/// Construct an error that indicates this command failed, containing information about the
88+
/// command and its output.
89+
///
90+
/// This is like [`OutputContext::error`], but it returns the inner [`OutputError`] directly,
91+
/// rather than wrapping it in an [`Error`].
92+
pub fn output_error(self) -> OutputError {
93+
OutputError::new(self.command, Box::new(self.output))
7094
}
7195

7296
/// Construct an error that indicates this command failed, containing information about the
@@ -86,7 +110,7 @@ where
86110
where
87111
E: Debug + Display + Send + Sync + 'static,
88112
{
89-
let ret = OutputError::new(self.command, Box::new(self.output));
113+
let ret = self.output_error();
90114
Error::from(match message {
91115
Some(message) => ret.with_message(Box::new(message)),
92116
None => ret,

0 commit comments

Comments
 (0)