humane_commands/
output.rs

1use std::{process::Output, string::FromUtf8Error};
2
3pub trait OutputExt {
4    /// Tries to parse stdout into a valid [String] without trailing newlines
5    fn stdout(&self) -> Result<String, FromUtf8Error>;
6    /// Tries to parse stderr into a valid [String] without trailing newlines
7    fn stderr(&self) -> Result<String, FromUtf8Error>;
8    /// Parses stdout lossily into a valid [String] without trailing newlines
9    fn stdout_lossy(&self) -> String;
10    /// Parses stderr lossily into a valid [String] without trailing newlines
11    fn stderr_lossy(&self) -> String;
12}
13
14impl OutputExt for Output {
15    fn stdout(&self) -> Result<String, FromUtf8Error> {
16        String::from_utf8(self.stdout.clone()).map(|x| x.trim_end_matches('\n').to_owned())
17    }
18
19    fn stderr(&self) -> Result<String, FromUtf8Error> {
20        String::from_utf8(self.stderr.clone()).map(|x| x.trim_end_matches('\n').to_owned())
21    }
22
23    fn stdout_lossy(&self) -> String {
24        String::from_utf8_lossy(&self.stdout)
25            .trim_end_matches('\n')
26            .to_owned()
27    }
28
29    fn stderr_lossy(&self) -> String {
30        String::from_utf8_lossy(&self.stderr)
31            .trim_end_matches('\n')
32            .to_owned()
33    }
34}