forked from PeachCloud/peach-workspace
remove redundant error variant prefixes
This commit is contained in:
parent
dc4fdb37e8
commit
59ad6f2523
|
@ -1,33 +1,35 @@
|
||||||
use std::io;
|
use std::{error, fmt, io};
|
||||||
|
|
||||||
use jsonrpc_core::{types::error::Error, ErrorCode};
|
use jsonrpc_core::{types::error::Error, ErrorCode};
|
||||||
use probes::ProbeError;
|
use probes::ProbeError;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum StatError {
|
pub enum StatError {
|
||||||
ReadCpuStat { source: ProbeError },
|
CpuStat { source: ProbeError },
|
||||||
ReadDiskUsage { source: ProbeError },
|
DiskUsage { source: ProbeError },
|
||||||
ReadLoadAvg { source: ProbeError },
|
LoadAvg { source: ProbeError },
|
||||||
ReadMemStat { source: ProbeError },
|
MemStat { source: ProbeError },
|
||||||
ReadUptime { source: io::Error },
|
Uptime { source: io::Error },
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::fmt::Display for StatError {
|
impl error::Error for StatError {}
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
||||||
|
impl fmt::Display for StatError {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
match *self {
|
match *self {
|
||||||
StatError::ReadCpuStat { ref source } => {
|
StatError::CpuStat { ref source } => {
|
||||||
write!(f, "Failed to retrieve CPU statistics: {}", 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)
|
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)
|
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)
|
write!(f, "Failed to retrieve memory statistics: {}", source)
|
||||||
}
|
}
|
||||||
StatError::ReadUptime { ref source } => {
|
StatError::Uptime { ref source } => {
|
||||||
write!(f, "Failed to retrieve system uptime: {}", source)
|
write!(f, "Failed to retrieve system uptime: {}", source)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -37,27 +39,27 @@ impl std::fmt::Display for StatError {
|
||||||
impl From<StatError> for Error {
|
impl From<StatError> for Error {
|
||||||
fn from(err: StatError) -> Self {
|
fn from(err: StatError) -> Self {
|
||||||
match &err {
|
match &err {
|
||||||
StatError::ReadCpuStat { source } => Error {
|
StatError::CpuStat { source } => Error {
|
||||||
code: ErrorCode::ServerError(-32001),
|
code: ErrorCode::ServerError(-32001),
|
||||||
message: format!("Failed to retrieve CPU statistics: {}", source),
|
message: format!("Failed to retrieve CPU statistics: {}", source),
|
||||||
data: None,
|
data: None,
|
||||||
},
|
},
|
||||||
StatError::ReadDiskUsage { source } => Error {
|
StatError::DiskUsage { source } => Error {
|
||||||
code: ErrorCode::ServerError(-32001),
|
code: ErrorCode::ServerError(-32001),
|
||||||
message: format!("Failed to retrieve disk usage statistics: {}", source),
|
message: format!("Failed to retrieve disk usage statistics: {}", source),
|
||||||
data: None,
|
data: None,
|
||||||
},
|
},
|
||||||
StatError::ReadLoadAvg { source } => Error {
|
StatError::LoadAvg { source } => Error {
|
||||||
code: ErrorCode::ServerError(-32001),
|
code: ErrorCode::ServerError(-32001),
|
||||||
message: format!("Failed to retrieve load average statistics: {}", source),
|
message: format!("Failed to retrieve load average statistics: {}", source),
|
||||||
data: None,
|
data: None,
|
||||||
},
|
},
|
||||||
StatError::ReadMemStat { source } => Error {
|
StatError::MemStat { source } => Error {
|
||||||
code: ErrorCode::ServerError(-32001),
|
code: ErrorCode::ServerError(-32001),
|
||||||
message: format!("Failed to retrieve memory statistics: {}", source),
|
message: format!("Failed to retrieve memory statistics: {}", source),
|
||||||
data: None,
|
data: None,
|
||||||
},
|
},
|
||||||
StatError::ReadUptime { source } => Error {
|
StatError::Uptime { source } => Error {
|
||||||
code: ErrorCode::ServerError(-32001),
|
code: ErrorCode::ServerError(-32001),
|
||||||
message: format!("Failed to retrieve system uptime: {}", source),
|
message: format!("Failed to retrieve system uptime: {}", source),
|
||||||
data: None,
|
data: None,
|
||||||
|
|
|
@ -8,7 +8,7 @@ use crate::error::StatError;
|
||||||
use crate::structs::{CpuStat, CpuStatPercentages, DiskUsage, LoadAverage, MemStat};
|
use crate::structs::{CpuStat, CpuStatPercentages, DiskUsage, LoadAverage, MemStat};
|
||||||
|
|
||||||
pub fn cpu_stats() -> Result<String, StatError> {
|
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 s = cpu_stats.stat;
|
||||||
let cpu = CpuStat {
|
let cpu = CpuStat {
|
||||||
user: s.user,
|
user: s.user,
|
||||||
|
@ -22,7 +22,7 @@ pub fn cpu_stats() -> Result<String, StatError> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn cpu_stats_percent() -> 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 s = cpu_stats.stat.in_percentages();
|
||||||
let cpu = CpuStatPercentages {
|
let cpu = CpuStatPercentages {
|
||||||
user: s.user,
|
user: s.user,
|
||||||
|
@ -36,7 +36,7 @@ pub fn cpu_stats_percent() -> Result<String, StatError> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn disk_usage() -> 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();
|
let mut disk_usages = Vec::new();
|
||||||
for d in disks {
|
for d in disks {
|
||||||
let disk = DiskUsage {
|
let disk = DiskUsage {
|
||||||
|
@ -55,7 +55,7 @@ pub fn disk_usage() -> Result<String, StatError> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn load_average() -> 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 {
|
let load_avg = LoadAverage {
|
||||||
one: l.one,
|
one: l.one,
|
||||||
five: l.five,
|
five: l.five,
|
||||||
|
@ -67,7 +67,7 @@ pub fn load_average() -> Result<String, StatError> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn mem_stats() -> 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 {
|
let mem = MemStat {
|
||||||
total: m.total(),
|
total: m.total(),
|
||||||
free: m.free(),
|
free: m.free(),
|
||||||
|
@ -82,7 +82,7 @@ pub fn uptime() -> Result<String, StatError> {
|
||||||
let sys = System::new();
|
let sys = System::new();
|
||||||
let uptime = sys
|
let uptime = sys
|
||||||
.uptime()
|
.uptime()
|
||||||
.map_err(|source| StatError::ReadUptime { source })?;
|
.map_err(|source| StatError::Uptime { source })?;
|
||||||
let uptime_secs = uptime.as_secs();
|
let uptime_secs = uptime.as_secs();
|
||||||
let json_uptime = json::to_string(&uptime_secs);
|
let json_uptime = json::to_string(&uptime_secs);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue