command_error/
output_like.rs

1use std::borrow::Cow;
2use std::process::ExitStatus;
3use std::process::Output;
4
5use utf8_command::Utf8Output;
6
7/// A command output type.
8pub trait OutputLike {
9    /// The command's exit status.
10    fn status(&self) -> ExitStatus;
11
12    /// The command's stdout, decoded to UTF-8 on a best-effort basis.
13    fn stdout(&self) -> Cow<'_, str>;
14
15    /// The command's stderr, decoded to UTF-8 on a best-effort basis.
16    fn stderr(&self) -> Cow<'_, str>;
17}
18
19/// A trivial implementation with empty output.
20impl OutputLike for ExitStatus {
21    fn status(&self) -> ExitStatus {
22        *self
23    }
24
25    fn stdout(&self) -> Cow<'_, str> {
26        Cow::Borrowed("")
27    }
28
29    fn stderr(&self) -> Cow<'_, str> {
30        Cow::Borrowed("")
31    }
32}
33
34impl OutputLike for Output {
35    fn status(&self) -> ExitStatus {
36        self.status
37    }
38
39    fn stdout(&self) -> Cow<'_, str> {
40        String::from_utf8_lossy(&self.stdout)
41    }
42
43    fn stderr(&self) -> Cow<'_, str> {
44        String::from_utf8_lossy(&self.stderr)
45    }
46}
47
48impl OutputLike for Utf8Output {
49    fn status(&self) -> ExitStatus {
50        self.status
51    }
52
53    fn stdout(&self) -> Cow<'_, str> {
54        Cow::Borrowed(&self.stdout)
55    }
56
57    fn stderr(&self) -> Cow<'_, str> {
58        Cow::Borrowed(&self.stderr)
59    }
60}