process/
output.rs

1//! # Output
2//!
3//! Module dedicated to command output. It only exposes an [`Output`]
4//! struct, a wrapper around raw `Vec<u8>` output.
5
6use std::ops::{Deref, DerefMut};
7
8use crate::{Error, Result};
9
10/// Wrapper around command output.
11///
12/// The only role of this struct is to provide convenient functions to
13/// export command output.
14#[derive(Clone, Debug, Default, Eq, PartialEq)]
15pub struct Output(Vec<u8>);
16
17impl Output {
18    pub fn new(output: impl IntoIterator<Item = u8>) -> Self {
19        Self::from(output.into_iter().collect::<Vec<_>>())
20    }
21
22    /// Reads the command output as string lossy.
23    pub fn to_string_lossy(&self) -> String {
24        String::from_utf8_lossy(self).to_string()
25    }
26}
27
28impl Deref for Output {
29    type Target = Vec<u8>;
30
31    fn deref(&self) -> &Self::Target {
32        &self.0
33    }
34}
35
36impl DerefMut for Output {
37    fn deref_mut(&mut self) -> &mut Self::Target {
38        &mut self.0
39    }
40}
41
42impl From<Vec<u8>> for Output {
43    fn from(output: Vec<u8>) -> Self {
44        Self(output)
45    }
46}
47
48impl From<Output> for Vec<u8> {
49    fn from(output: Output) -> Self {
50        output.0
51    }
52}
53
54impl TryFrom<Output> for String {
55    type Error = Error;
56
57    fn try_from(output: Output) -> Result<Self> {
58        String::from_utf8(output.into()).map_err(Error::ParseOutputAsUtf8StringError)
59    }
60}