Skip to content

Commit

Permalink
feat(rpc): add default to_rpc_result function (paradigmxyz#993)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored and literallymarvellous committed Feb 6, 2023
1 parent f876903 commit 010a4fd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion crates/net/rpc/src/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl AdminApiServer for AdminApi {

async fn node_info(&self) -> RpcResult<NodeInfo> {
let enr = self.network.local_node_record();
let status = self.network.network_status().await.map_internal_err(|e| e.to_string())?;
let status = self.network.network_status().await.to_rpc_result()?;

Ok(NodeInfo::new(enr, status))
}
Expand Down
10 changes: 10 additions & 0 deletions crates/net/rpc/src/result.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
//! Additional helpers for converting errors.

use jsonrpsee::core::{Error as RpcError, RpcResult};
use std::fmt::Display;

/// Helper trait to easily convert various `Result` types into [`RpcResult`]
pub(crate) trait ToRpcResult<Ok, Err> {
/// Converts the error of the [Result] to an [RpcResult] via the `Err` [Display] impl.
fn to_rpc_result(self) -> RpcResult<Ok>
where
Err: Display,
Self: Sized,
{
self.map_internal_err(|err| err.to_string())
}

/// Converts this type into an [`RpcResult`]
fn map_rpc_err<'a, F, M>(self, op: F) -> RpcResult<Ok>
where
Expand Down

0 comments on commit 010a4fd

Please sign in to comment.