//! Custom error type for `peach-stats`. use probes::ProbeError; use std::{error, fmt, io::Error as IoError, str::Utf8Error}; /// Custom error type encapsulating all possible errors when retrieving system /// statistics. #[derive(Debug)] pub enum StatsError { /// Failed to retrieve CPU statistics. CpuStat(ProbeError), /// Failed to retrieve disk usage statistics. DiskUsage(ProbeError), /// Failed to retrieve load average statistics. LoadAvg(ProbeError), /// Failed to retrieve memory usage statistics. MemStat(ProbeError), /// Failed to retrieve system uptime. Uptime(IoError), /// Systemctl command returned an error. Systemctl(IoError), /// Failed to interpret sequence of `u8` as a string. Utf8String(Utf8Error), } impl error::Error for StatsError {} impl fmt::Display for StatsError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { StatsError::CpuStat(ref source) => { write!(f, "Failed to retrieve CPU statistics: {}", source) } StatsError::DiskUsage(ref source) => { write!(f, "Failed to retrieve disk usage statistics: {}", source) } StatsError::LoadAvg(ref source) => { write!(f, "Failed to retrieve load average statistics: {}", source) } StatsError::MemStat(ref source) => { write!(f, "Failed to retrieve memory statistics: {}", source) } StatsError::Uptime(ref source) => { write!(f, "Failed to retrieve system uptime: {}", source) } StatsError::Systemctl(ref source) => { write!(f, "Systemctl command returned an error: {}", source) } StatsError::Utf8String(ref source) => { write!(f, "Failed to convert stdout to string: {}", source) } } } }