bare-script 0.1.1

The type-safe scripting authority for Rust. A framework for building robust shell commands and automation with 'Parse, don't validate' philosophy.
Documentation
//! Command output types.

use std::fmt;
use std::process::ExitStatus;

/// Represents the output of an executed command.
#[derive(Debug, Clone)]
pub struct Output {
    stdout: Vec<u8>,
    stderr: Vec<u8>,
    status: ExitStatus,
}

impl Output {
    /// Creates a new `Output` from raw components.
    pub fn new(stdout: Vec<u8>, stderr: Vec<u8>, status: ExitStatus) -> Self {
        Self {
            stdout,
            stderr,
            status,
        }
    }

    /// Returns the standard output as bytes.
    #[inline]
    pub fn stdout(&self) -> &[u8] {
        &self.stdout
    }

    /// Returns the standard error as bytes.
    #[inline]
    pub fn stderr(&self) -> &[u8] {
        &self.stderr
    }

    /// Returns the exit status of the command.
    #[inline]
    pub fn status(&self) -> ExitStatus {
        self.status
    }

    /// Returns the stdout output as a string, lossily converting UTF-8 bytes.
    #[inline]
    pub fn stdout_str(&self) -> String {
        String::from_utf8_lossy(&self.stdout).into_owned()
    }

    /// Returns the stderr output as a string, lossily converting UTF-8 bytes.
    #[inline]
    pub fn stderr_str(&self) -> String {
        String::from_utf8_lossy(&self.stderr).into_owned()
    }

    /// Returns `true` if the command exited successfully (code 0).
    #[inline]
    pub fn success(&self) -> bool {
        self.status.success()
    }

    /// Returns the exit code of the command, if available.
    #[inline]
    pub fn code(&self) -> Option<i32> {
        self.status.code()
    }
}

impl fmt::Display for Output {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.stdout_str())
    }
}