Crate rpc_router

source ·
Expand description

rpc::router module provides the type and implementation for json rpc routing.

It has the following constructs:

  • RpcRouter holds the HashMap of method_name: Box<dyn RpcHandlerWrapperTrait>.
  • RpcHandler trait is implemented for any async function that, with (S1, S2, ...[impl IntoParams]), returns web::Result<Serialize> where S1, S2, … are types that implement FromResources (see router/from_resources.rs and src/resources.rs).
  • IntoParams is the trait to implement to instruct how to go from Option<Value> json-rpc params to the handler’s param types.
  • IntoParams has a default into_params implementation that will return an error if the params are missing.
#[derive(Deserialize)]
pub struct ParamsIded {
  id: i64,
}

impl IntoParams for ParamsIded {}
  • For custom IntoParams behavior, implement the IntoParams::into_params function.
  • Implementing IntoDefaultParams on a type that implements Default will auto-implement IntoParams and call T::default() when the params Option<Value> is None.

Macros§

  • Macro generatring the Rpc Handler implementations for zero or more FromResources with the last argument being IntoParams and one with not last IntoParams argument.
  • A simple macro to create a new RpcRouterInner and add each rpc handler-compatible function along with their corresponding names.
  • A simple macro to create a new RouterBuider from a list of handlers and optionaly a list of resources

Structs§

Enums§

  • The RPC Request Parsing error is used when utilizing value.try_into()? or Request::from_value(value). The design intent is to validate and provide as much context as possible when a specific validation fails.

Traits§

  • The Handler trait that will be implemented by rpc handler functions.
  • Marker trait with a blanket implementation that return T::default if the params: Option<Value> is none.
  • A trait with a default implementation that converts any application error into a RpcHandlerError. This allows the application code to query and extract the specified application error.
  • IntoParams allows for converting an Option<Value> into the necessary type for RPC handler parameters. The default implementation below will result in failure if the value is None. For customized behavior, users can implement their own into_params method.

Type Aliases§

Derive Macros§

  • Will implement IntoHandlerError for this target type. The target type must implement std::error::Error
  • Will implement IntoParams for this target type. The target type must implement Deserialize
  • Will implement FromResources for this target type. The target type must implement Clone + Send + Sync