use std::convert::TryFrom;
use std::str::FromStr;
use crate::process::Status;
use crate::ParseStatusError;
impl TryFrom<char> for Status {
type Error = ParseStatusError;
fn try_from(value: char) -> Result<Self, Self::Error> {
match value {
'R' => Ok(Status::Running),
'S' => Ok(Status::Sleeping),
'D' => Ok(Status::DiskSleep),
'Z' => Ok(Status::Zombie),
'T' => Ok(Status::Stopped),
't' => Ok(Status::TracingStop),
'X' | 'x' => Ok(Status::Dead),
'K' => Ok(Status::WakeKill),
'W' => Ok(Status::Waking),
'P' => Ok(Status::Parked),
'I' => Ok(Status::Idle),
_ => Err(ParseStatusError::IncorrectChar {
contents: value.to_string(),
}),
}
}
}
impl FromStr for Status {
type Err = ParseStatusError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.len() != 1 {
return Err(ParseStatusError::IncorrectLength {
contents: s.to_string(),
});
}
Status::try_from(s.chars().next().unwrap())
}
}
impl std::fmt::Display for Status {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
match *self {
Status::Running => "R",
Status::Sleeping => "S",
Status::DiskSleep => "D",
Status::Zombie => "Z",
Status::Stopped => "T",
Status::TracingStop => "t",
Status::Dead => "X",
Status::WakeKill => "K",
Status::Waking => "W",
Status::Parked => "P",
Status::Idle => "I",
_ => "",
}
)
}
}