From 9bc9cdbbeef79745ef134dccff9a1fc1e632fe1a Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Mon, 23 Jan 2023 15:27:21 +0100 Subject: [PATCH] feat(rpc): add impl_to_rpc_result macro (#986) --- crates/net/rpc/src/result.rs | 164 +++++++++++++---------------------- 1 file changed, 58 insertions(+), 106 deletions(-) diff --git a/crates/net/rpc/src/result.rs b/crates/net/rpc/src/result.rs index b331bf3580..2ca2091969 100644 --- a/crates/net/rpc/src/result.rs +++ b/crates/net/rpc/src/result.rs @@ -1,7 +1,6 @@ //! Additional helpers for converting errors. use jsonrpsee::core::{Error as RpcError, RpcResult}; -use reth_network_api::NetworkError; /// Helper trait to easily convert various `Result` types into [`RpcResult`] pub(crate) trait ToRpcResult { @@ -31,115 +30,68 @@ pub(crate) trait ToRpcResult { fn with_message(self, msg: &str) -> RpcResult; } -impl ToRpcResult for reth_interfaces::Result { - #[inline] - fn map_rpc_err<'a, F, M>(self, op: F) -> RpcResult - where - F: FnOnce(reth_interfaces::Error) -> (i32, M, Option<&'a [u8]>), - M: Into, - { - match self { - Ok(t) => Ok(t), - Err(err) => { - let (code, msg, data) = op(err); - Err(rpc_err(code, msg, data)) +/// A macro that implements the `ToRpcResult` for a specific error type +macro_rules! impl_to_rpc_result { + ($err:ty) => { + impl ToRpcResult for Result { + #[inline] + fn map_rpc_err<'a, F, M>(self, op: F) -> RpcResult + where + F: FnOnce($err) -> (i32, M, Option<&'a [u8]>), + M: Into, + { + match self { + Ok(t) => Ok(t), + Err(err) => { + let (code, msg, data) = op(err); + Err(rpc_err(code, msg, data)) + } + } + } + + #[inline] + fn map_internal_err<'a, F, M>(self, op: F) -> RpcResult + where + F: FnOnce($err) -> M, + M: Into, + { + match self { + Ok(t) => Ok(t), + Err(err) => Err(internal_rpc_err(op(err))), + } + } + + #[inline] + fn map_internal_err_with_data<'a, F, M>(self, op: F) -> RpcResult + where + F: FnOnce($err) -> (M, &'a [u8]), + M: Into, + { + match self { + Ok(t) => Ok(t), + Err(err) => { + let (msg, data) = op(err); + Err(internal_rpc_err_with_data(msg, data)) + } + } + } + + #[inline] + fn with_message(self, msg: &str) -> RpcResult { + match self { + Ok(t) => Ok(t), + Err(err) => { + let msg = format!("{msg}: {err:?}"); + Err(internal_rpc_err(msg)) + } + } } } - } - - #[inline] - fn map_internal_err<'a, F, M>(self, op: F) -> RpcResult - where - F: FnOnce(reth_interfaces::Error) -> M, - M: Into, - { - match self { - Ok(t) => Ok(t), - Err(err) => Err(internal_rpc_err(op(err))), - } - } - - #[inline] - fn map_internal_err_with_data<'a, F, M>(self, op: F) -> RpcResult - where - F: FnOnce(reth_interfaces::Error) -> (M, &'a [u8]), - M: Into, - { - match self { - Ok(t) => Ok(t), - Err(err) => { - let (msg, data) = op(err); - Err(internal_rpc_err_with_data(msg, data)) - } - } - } - - #[inline] - fn with_message(self, msg: &str) -> RpcResult { - match self { - Ok(t) => Ok(t), - Err(err) => { - let msg = format!("{msg}: {err:?}"); - Err(internal_rpc_err(msg)) - } - } - } + }; } -impl ToRpcResult for Result { - #[inline] - fn map_rpc_err<'a, F, M>(self, op: F) -> RpcResult - where - F: FnOnce(NetworkError) -> (i32, M, Option<&'a [u8]>), - M: Into, - { - match self { - Ok(t) => Ok(t), - Err(err) => { - let (code, msg, data) = op(err); - Err(rpc_err(code, msg, data)) - } - } - } - - #[inline] - fn map_internal_err<'a, F, M>(self, op: F) -> RpcResult - where - F: FnOnce(NetworkError) -> M, - M: Into, - { - match self { - Ok(t) => Ok(t), - Err(err) => Err(internal_rpc_err(op(err))), - } - } - - #[inline] - fn map_internal_err_with_data<'a, F, M>(self, op: F) -> RpcResult - where - F: FnOnce(NetworkError) -> (M, &'a [u8]), - M: Into, - { - match self { - Ok(t) => Ok(t), - Err(err) => { - let (msg, data) = op(err); - Err(internal_rpc_err_with_data(msg, data)) - } - } - } - - #[inline] - fn with_message(self, msg: &str) -> RpcResult { - match self { - Ok(t) => Ok(t), - Err(err) => { - let msg = format!("{msg}: {err:?}"); - Err(internal_rpc_err(msg)) - } - } - } -} +impl_to_rpc_result!(reth_interfaces::Error); +impl_to_rpc_result!(reth_network_api::NetworkError); /// Constructs an internal JSON-RPC error. pub(crate) fn internal_rpc_err(msg: impl Into) -> jsonrpsee::core::Error {