From 59ad6f2523dadf2a36dd7439b8af213f5d3ea8c6 Mon Sep 17 00:00:00 2001 From: glyph Date: Mon, 1 Nov 2021 14:10:16 +0200 Subject: [PATCH] remove redundant error variant prefixes --- peach-stats/src/error.rs | 38 ++++++++++++++++++++------------------ peach-stats/src/stats.rs | 12 ++++++------ 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/peach-stats/src/error.rs b/peach-stats/src/error.rs index fd80fda6..b5583642 100644 --- a/peach-stats/src/error.rs +++ b/peach-stats/src/error.rs @@ -1,33 +1,35 @@ -use std::io; +use std::{error, fmt, io}; use jsonrpc_core::{types::error::Error, ErrorCode}; use probes::ProbeError; #[derive(Debug)] pub enum StatError { - ReadCpuStat { source: ProbeError }, - ReadDiskUsage { source: ProbeError }, - ReadLoadAvg { source: ProbeError }, - ReadMemStat { source: ProbeError }, - ReadUptime { source: io::Error }, + CpuStat { source: ProbeError }, + DiskUsage { source: ProbeError }, + LoadAvg { source: ProbeError }, + MemStat { source: ProbeError }, + Uptime { source: io::Error }, } -impl std::fmt::Display for StatError { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { +impl error::Error for StatError {} + +impl fmt::Display for StatError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { - StatError::ReadCpuStat { ref source } => { + StatError::CpuStat { ref source } => { write!(f, "Failed to retrieve CPU statistics: {}", source) } - StatError::ReadDiskUsage { ref source } => { + StatError::DiskUsage { ref source } => { write!(f, "Failed to retrieve disk usage statistics: {}", source) } - StatError::ReadLoadAvg { ref source } => { + StatError::LoadAvg { ref source } => { write!(f, "Failed to retrieve load average statistics: {}", source) } - StatError::ReadMemStat { ref source } => { + StatError::MemStat { ref source } => { write!(f, "Failed to retrieve memory statistics: {}", source) } - StatError::ReadUptime { ref source } => { + StatError::Uptime { ref source } => { write!(f, "Failed to retrieve system uptime: {}", source) } } @@ -37,27 +39,27 @@ impl std::fmt::Display for StatError { impl From for Error { fn from(err: StatError) -> Self { match &err { - StatError::ReadCpuStat { source } => Error { + StatError::CpuStat { source } => Error { code: ErrorCode::ServerError(-32001), message: format!("Failed to retrieve CPU statistics: {}", source), data: None, }, - StatError::ReadDiskUsage { source } => Error { + StatError::DiskUsage { source } => Error { code: ErrorCode::ServerError(-32001), message: format!("Failed to retrieve disk usage statistics: {}", source), data: None, }, - StatError::ReadLoadAvg { source } => Error { + StatError::LoadAvg { source } => Error { code: ErrorCode::ServerError(-32001), message: format!("Failed to retrieve load average statistics: {}", source), data: None, }, - StatError::ReadMemStat { source } => Error { + StatError::MemStat { source } => Error { code: ErrorCode::ServerError(-32001), message: format!("Failed to retrieve memory statistics: {}", source), data: None, }, - StatError::ReadUptime { source } => Error { + StatError::Uptime { source } => Error { code: ErrorCode::ServerError(-32001), message: format!("Failed to retrieve system uptime: {}", source), data: None, diff --git a/peach-stats/src/stats.rs b/peach-stats/src/stats.rs index ffb884d6..15c90319 100644 --- a/peach-stats/src/stats.rs +++ b/peach-stats/src/stats.rs @@ -8,7 +8,7 @@ use crate::error::StatError; use crate::structs::{CpuStat, CpuStatPercentages, DiskUsage, LoadAverage, MemStat}; pub fn cpu_stats() -> Result { - let cpu_stats = cpu::proc::read().map_err(|source| StatError::ReadCpuStat { source })?; + let cpu_stats = cpu::proc::read().map_err(|source| StatError::CpuStat { source })?; let s = cpu_stats.stat; let cpu = CpuStat { user: s.user, @@ -22,7 +22,7 @@ pub fn cpu_stats() -> Result { } pub fn cpu_stats_percent() -> Result { - let cpu_stats = cpu::proc::read().map_err(|source| StatError::ReadCpuStat { source })?; + let cpu_stats = cpu::proc::read().map_err(|source| StatError::CpuStat { source })?; let s = cpu_stats.stat.in_percentages(); let cpu = CpuStatPercentages { user: s.user, @@ -36,7 +36,7 @@ pub fn cpu_stats_percent() -> Result { } pub fn disk_usage() -> Result { - let disks = disk_usage::read().map_err(|source| StatError::ReadDiskUsage { source })?; + let disks = disk_usage::read().map_err(|source| StatError::DiskUsage { source })?; let mut disk_usages = Vec::new(); for d in disks { let disk = DiskUsage { @@ -55,7 +55,7 @@ pub fn disk_usage() -> Result { } pub fn load_average() -> Result { - let l = load::read().map_err(|source| StatError::ReadLoadAvg { source })?; + let l = load::read().map_err(|source| StatError::LoadAvg { source })?; let load_avg = LoadAverage { one: l.one, five: l.five, @@ -67,7 +67,7 @@ pub fn load_average() -> Result { } pub fn mem_stats() -> Result { - let m = memory::read().map_err(|source| StatError::ReadMemStat { source })?; + let m = memory::read().map_err(|source| StatError::MemStat { source })?; let mem = MemStat { total: m.total(), free: m.free(), @@ -82,7 +82,7 @@ pub fn uptime() -> Result { let sys = System::new(); let uptime = sys .uptime() - .map_err(|source| StatError::ReadUptime { source })?; + .map_err(|source| StatError::Uptime { source })?; let uptime_secs = uptime.as_secs(); let json_uptime = json::to_string(&uptime_secs);