Replace miniserde_support with serde_support for peach-jsonrpc-server #64
|
@ -18,8 +18,8 @@ env_logger = "0.9"
|
|||
jsonrpc-core = "18"
|
||||
jsonrpc-http-server = "18"
|
||||
log = "0.4"
|
||||
miniserde = "0.1.15"
|
||||
peach-stats = { path = "../peach-stats", features = ["miniserde_support"] }
|
||||
peach-stats = { path = "../peach-stats", features = ["serde_support"] }
|
||||
serde_json = "1.0.74"
|
||||
|
||||
[dev-dependencies]
|
||||
jsonrpc-test = "18"
|
||||
|
|
|
@ -1,12 +1,16 @@
|
|||
use std::fmt;
|
||||
|
||||
use jsonrpc_core::{Error as JsonRpcError, ErrorCode};
|
||||
use serde_json::error::Error as SerdeJsonError;
|
||||
|
||||
use peach_stats::StatsError;
|
||||
|
||||
/// Custom error type encapsulating all possible errors for a JSON-RPC server
|
||||
/// and associated methods.
|
||||
#[derive(Debug)]
|
||||
pub enum JsonRpcServerError {
|
||||
/// Failed to serialize a string from a data structure.
|
||||
Serde(SerdeJsonError),
|
||||
/// An error returned from the `peach-stats` library.
|
||||
Stats(StatsError),
|
||||
/// An expected JSON-RPC method parameter was not provided.
|
||||
|
@ -24,6 +28,9 @@ impl fmt::Display for JsonRpcServerError {
|
|||
JsonRpcServerError::MissingParameter(ref source) => {
|
||||
write!(f, "Missing expected parameter: {}", source)
|
||||
}
|
||||
JsonRpcServerError::Serde(ref source) => {
|
||||
write!(f, "{}", source)
|
||||
}
|
||||
JsonRpcServerError::Stats(ref source) => {
|
||||
write!(f, "{}", source)
|
||||
}
|
||||
|
@ -34,6 +41,11 @@ impl fmt::Display for JsonRpcServerError {
|
|||
impl From<JsonRpcServerError> for JsonRpcError {
|
||||
fn from(err: JsonRpcServerError) -> Self {
|
||||
match &err {
|
||||
JsonRpcServerError::Serde(source) => JsonRpcError {
|
||||
code: ErrorCode::ServerError(-32002),
|
||||
message: format!("{}", source),
|
||||
data: None,
|
||||
},
|
||||
JsonRpcServerError::Stats(source) => JsonRpcError {
|
||||
code: ErrorCode::ServerError(-32001),
|
||||
message: format!("{}", source),
|
||||
|
|
|
@ -8,7 +8,6 @@ use std::result::Result;
|
|||
use jsonrpc_core::{IoHandler, Value};
|
||||
use jsonrpc_http_server::{AccessControlAllowOrigin, DomainsValidation, ServerBuilder};
|
||||
use log::info;
|
||||
use miniserde::json;
|
||||
use peach_stats::stats;
|
||||
|
||||
mod error;
|
||||
|
@ -30,7 +29,7 @@ pub fn run() -> Result<(), JsonRpcServerError> {
|
|||
io.add_sync_method("cpu_stats", move |_| {
|
||||
info!("Fetching CPU statistics.");
|
||||
let cpu = stats::cpu_stats().map_err(JsonRpcServerError::Stats)?;
|
||||
let json_cpu = json::to_string(&cpu);
|
||||
let json_cpu = serde_json::to_string(&cpu).map_err(JsonRpcServerError::Serde)?;
|
||||
|
||||
Ok(Value::String(json_cpu))
|
||||
});
|
||||
|
@ -38,7 +37,7 @@ pub fn run() -> Result<(), JsonRpcServerError> {
|
|||
io.add_sync_method("cpu_stats_percent", move |_| {
|
||||
info!("Fetching CPU statistics as percentages.");
|
||||
let cpu = stats::cpu_stats_percent().map_err(JsonRpcServerError::Stats)?;
|
||||
let json_cpu = json::to_string(&cpu);
|
||||
let json_cpu = serde_json::to_string(&cpu).map_err(JsonRpcServerError::Serde)?;
|
||||
|
||||
Ok(Value::String(json_cpu))
|
||||
});
|
||||
|
@ -46,7 +45,7 @@ pub fn run() -> Result<(), JsonRpcServerError> {
|
|||
io.add_sync_method("disk_usage", move |_| {
|
||||
info!("Fetching disk usage statistics.");
|
||||
let disks = stats::disk_usage().map_err(JsonRpcServerError::Stats)?;
|
||||
let json_disks = json::to_string(&disks);
|
||||
let json_disks = serde_json::to_string(&disks).map_err(JsonRpcServerError::Serde)?;
|
||||
|
||||
Ok(Value::String(json_disks))
|
||||
});
|
||||
|
@ -54,7 +53,7 @@ pub fn run() -> Result<(), JsonRpcServerError> {
|
|||
io.add_sync_method("load_average", move |_| {
|
||||
info!("Fetching system load average statistics.");
|
||||
let avg = stats::load_average().map_err(JsonRpcServerError::Stats)?;
|
||||
let json_avg = json::to_string(&avg);
|
||||
let json_avg = serde_json::to_string(&avg).map_err(JsonRpcServerError::Serde)?;
|
||||
|
||||
Ok(Value::String(json_avg))
|
||||
});
|
||||
|
@ -62,7 +61,7 @@ pub fn run() -> Result<(), JsonRpcServerError> {
|
|||
io.add_sync_method("mem_stats", move |_| {
|
||||
info!("Fetching current memory statistics.");
|
||||
let mem = stats::mem_stats().map_err(JsonRpcServerError::Stats)?;
|
||||
let json_mem = json::to_string(&mem);
|
||||
let json_mem = serde_json::to_string(&mem).map_err(JsonRpcServerError::Serde)?;
|
||||
|
||||
Ok(Value::String(json_mem))
|
||||
});
|
||||
|
@ -70,7 +69,7 @@ pub fn run() -> Result<(), JsonRpcServerError> {
|
|||
io.add_sync_method("uptime", move |_| {
|
||||
info!("Fetching system uptime.");
|
||||
let uptime = stats::uptime().map_err(JsonRpcServerError::Stats)?;
|
||||
let json_uptime = json::to_string(&uptime);
|
||||
let json_uptime = serde_json::to_string(&uptime).map_err(JsonRpcServerError::Serde)?;
|
||||
|
||||
Ok(Value::String(json_uptime))
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue