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