remove redundant error variant prefixes

This commit is contained in:
glyph 2021-11-01 14:10:16 +02:00
parent dc4fdb37e8
commit 59ad6f2523
2 changed files with 26 additions and 24 deletions

View File

@ -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<StatError> 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,

View File

@ -8,7 +8,7 @@ use crate::error::StatError;
use crate::structs::{CpuStat, CpuStatPercentages, DiskUsage, LoadAverage, MemStat};
pub fn cpu_stats() -> Result<String, StatError> {
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<String, StatError> {
}
pub fn cpu_stats_percent() -> Result<String, StatError> {
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<String, StatError> {
}
pub fn disk_usage() -> Result<String, StatError> {
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<String, StatError> {
}
pub fn load_average() -> Result<String, StatError> {
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<String, StatError> {
}
pub fn mem_stats() -> Result<String, StatError> {
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<String, StatError> {
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);