use std::{fmt::Display, io};
#[derive(Debug)]
pub enum ProcessItem {
Output(String),
Error(String),
Exit(i32),
}
impl ProcessItem {
#[must_use]
pub fn is_output(&self) -> bool {
matches!(self, Self::Output(..))
}
#[must_use]
pub fn is_error(&self) -> bool {
matches!(self, Self::Error(..))
}
#[must_use]
pub fn is_exit(&self) -> bool {
matches!(self, Self::Exit(..))
}
pub fn as_exit(&self) -> Option<&i32> {
if let Self::Exit(v) = self {
Some(v)
} else {
None
}
}
pub fn as_error(&self) -> Option<&String> {
if let Self::Error(v) = self {
Some(v)
} else {
None
}
}
pub fn as_output(&self) -> Option<&String> {
if let Self::Output(v) = self {
Some(v)
} else {
None
}
}
}
impl From<(bool, io::Result<String>)> for ProcessItem {
fn from(v: (bool, io::Result<String>)) -> Self {
match v.1 {
Ok(line) if v.0 => Self::Output(line),
Ok(line) => Self::Error(line),
Err(e) => Self::Error(e.to_string()),
}
}
}
impl Display for ProcessItem {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ProcessItem::Output(line) => line.fmt(f),
ProcessItem::Error(line) => line.fmt(f),
ProcessItem::Exit(code) => write!(f, "[Exit] {code}"),
}
}
}