mirror of
https://github.com/Start9Labs/rpc-toolkit.git
synced 2026-03-26 02:11:56 +00:00
allow full args for handler fn
This commit is contained in:
@@ -13,8 +13,8 @@ use yajrc::{Id, RpcError};
|
||||
|
||||
use crate::util::{internal_error, parse_error, Flat, PhantomData};
|
||||
use crate::{
|
||||
AnyHandler, CliBindings, CliBindingsAny, DynHandler, HandleAny, HandleAnyArgs, HandleArgs,
|
||||
Handler, HandlerTypes, IntoContext, Name, ParentHandler, PrintCliResult,
|
||||
AnyHandler, CliBindingsAny, DynHandler, HandleAny, HandleAnyArgs, Handler, HandlerArgs,
|
||||
HandlerArgsFor, HandlerTypes, IntoContext, Name, ParentHandler, PrintCliResult,
|
||||
};
|
||||
|
||||
type GenericRpcMethod<'a> = yajrc::GenericRpcMethod<&'a str, Value, Value>;
|
||||
@@ -215,7 +215,7 @@ where
|
||||
type Context = Context;
|
||||
async fn handle_async(
|
||||
&self,
|
||||
handle_args: HandleArgs<Context, Self>,
|
||||
handle_args: HandlerArgsFor<Context, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
let full_method = handle_args
|
||||
.parent_method
|
||||
@@ -250,18 +250,18 @@ where
|
||||
type Context = Context;
|
||||
fn print(
|
||||
&self,
|
||||
HandleArgs {
|
||||
HandlerArgs {
|
||||
context,
|
||||
parent_method,
|
||||
method,
|
||||
params,
|
||||
inherited_params,
|
||||
raw_params,
|
||||
}: HandleArgs<Self::Context, Self>,
|
||||
}: HandlerArgsFor<Self::Context, Self>,
|
||||
result: Self::Ok,
|
||||
) -> Result<(), Self::Err> {
|
||||
self.handler.print(
|
||||
HandleArgs {
|
||||
HandlerArgs {
|
||||
context,
|
||||
parent_method,
|
||||
method,
|
||||
|
||||
@@ -13,7 +13,8 @@ use yajrc::RpcError;
|
||||
use crate::util::{internal_error, parse_error, Flat, PhantomData};
|
||||
use crate::{
|
||||
iter_from_ctx_and_handler, AnyContext, AnyHandler, CallRemote, CliBindings, DynHandler,
|
||||
EitherContext, HandleArgs, Handler, HandlerTypes, IntoContext, IntoHandlers, PrintCliResult,
|
||||
EitherContext, Handler, HandlerArgs, HandlerArgsFor, HandlerTypes, IntoContext, IntoHandlers,
|
||||
PrintCliResult,
|
||||
};
|
||||
|
||||
pub trait HandlerExt: Handler + Sized {
|
||||
@@ -32,7 +33,7 @@ pub trait HandlerExt: Handler + Sized {
|
||||
display: F,
|
||||
) -> CustomDisplayFn<F, Self, Context>
|
||||
where
|
||||
F: Fn(HandleArgs<Context, Self>, Self::Ok) -> Result<(), Self::Err>;
|
||||
F: Fn(HandlerArgsFor<Context, Self>, Self::Ok) -> Result<(), Self::Err>;
|
||||
fn with_inherited<Params, InheritedParams, F>(
|
||||
self,
|
||||
f: F,
|
||||
@@ -68,7 +69,7 @@ impl<T: Handler + Sized> HandlerExt for T {
|
||||
display: F,
|
||||
) -> CustomDisplayFn<F, Self, Context>
|
||||
where
|
||||
F: Fn(HandleArgs<Context, Self>, Self::Ok) -> Result<(), Self::Err>,
|
||||
F: Fn(HandlerArgsFor<Context, Self>, Self::Ok) -> Result<(), Self::Err>,
|
||||
{
|
||||
CustomDisplayFn {
|
||||
_phantom: PhantomData::new(),
|
||||
@@ -137,16 +138,16 @@ where
|
||||
type Context = H::Context;
|
||||
fn handle_sync(
|
||||
&self,
|
||||
HandleArgs {
|
||||
HandlerArgs {
|
||||
context,
|
||||
parent_method,
|
||||
method,
|
||||
params,
|
||||
inherited_params,
|
||||
raw_params,
|
||||
}: HandleArgs<Self::Context, Self>,
|
||||
}: HandlerArgsFor<Self::Context, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
self.0.handle_sync(HandleArgs {
|
||||
self.0.handle_sync(HandlerArgs {
|
||||
context,
|
||||
parent_method,
|
||||
method,
|
||||
@@ -157,17 +158,17 @@ where
|
||||
}
|
||||
async fn handle_async(
|
||||
&self,
|
||||
HandleArgs {
|
||||
HandlerArgs {
|
||||
context,
|
||||
parent_method,
|
||||
method,
|
||||
params,
|
||||
inherited_params,
|
||||
raw_params,
|
||||
}: HandleArgs<Self::Context, Self>,
|
||||
}: HandlerArgsFor<Self::Context, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
self.0
|
||||
.handle_async(HandleArgs {
|
||||
.handle_async(HandlerArgs {
|
||||
context,
|
||||
parent_method,
|
||||
method,
|
||||
@@ -197,7 +198,7 @@ where
|
||||
H::Params: FromArgMatches + CommandFactory + Serialize,
|
||||
{
|
||||
type Context = AnyContext;
|
||||
fn print(&self, _: HandleArgs<Self::Context, Self>, _: Self::Ok) -> Result<(), Self::Err> {
|
||||
fn print(&self, _: HandlerArgsFor<Self::Context, Self>, _: Self::Ok) -> Result<(), Self::Err> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -225,16 +226,16 @@ where
|
||||
type Context = H::Context;
|
||||
fn handle_sync(
|
||||
&self,
|
||||
HandleArgs {
|
||||
HandlerArgs {
|
||||
context,
|
||||
parent_method,
|
||||
method,
|
||||
params,
|
||||
inherited_params,
|
||||
raw_params,
|
||||
}: HandleArgs<Self::Context, Self>,
|
||||
}: HandlerArgsFor<Self::Context, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
self.handler.handle_sync(HandleArgs {
|
||||
self.handler.handle_sync(HandlerArgs {
|
||||
context,
|
||||
parent_method,
|
||||
method,
|
||||
@@ -245,17 +246,17 @@ where
|
||||
}
|
||||
async fn handle_async(
|
||||
&self,
|
||||
HandleArgs {
|
||||
HandlerArgs {
|
||||
context,
|
||||
parent_method,
|
||||
method,
|
||||
params,
|
||||
inherited_params,
|
||||
raw_params,
|
||||
}: HandleArgs<Self::Context, Self>,
|
||||
}: HandlerArgsFor<Self::Context, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
self.handler
|
||||
.handle_async(HandleArgs {
|
||||
.handle_async(HandlerArgs {
|
||||
context,
|
||||
parent_method,
|
||||
method,
|
||||
@@ -295,18 +296,18 @@ where
|
||||
type Context = P::Context;
|
||||
fn print(
|
||||
&self,
|
||||
HandleArgs {
|
||||
HandlerArgs {
|
||||
context,
|
||||
parent_method,
|
||||
method,
|
||||
params,
|
||||
inherited_params,
|
||||
raw_params,
|
||||
}: HandleArgs<Self::Context, Self>,
|
||||
}: HandlerArgsFor<Self::Context, Self>,
|
||||
result: Self::Ok,
|
||||
) -> Result<(), Self::Err> {
|
||||
self.print.print(
|
||||
HandleArgs {
|
||||
HandlerArgs {
|
||||
context,
|
||||
parent_method,
|
||||
method,
|
||||
@@ -360,16 +361,16 @@ where
|
||||
type Context = H::Context;
|
||||
fn handle_sync(
|
||||
&self,
|
||||
HandleArgs {
|
||||
HandlerArgs {
|
||||
context,
|
||||
parent_method,
|
||||
method,
|
||||
params,
|
||||
inherited_params,
|
||||
raw_params,
|
||||
}: HandleArgs<Self::Context, Self>,
|
||||
}: HandlerArgsFor<Self::Context, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
self.handler.handle_sync(HandleArgs {
|
||||
self.handler.handle_sync(HandlerArgs {
|
||||
context,
|
||||
parent_method,
|
||||
method,
|
||||
@@ -380,17 +381,17 @@ where
|
||||
}
|
||||
async fn handle_async(
|
||||
&self,
|
||||
HandleArgs {
|
||||
HandlerArgs {
|
||||
context,
|
||||
parent_method,
|
||||
method,
|
||||
params,
|
||||
inherited_params,
|
||||
raw_params,
|
||||
}: HandleArgs<Self::Context, Self>,
|
||||
}: HandlerArgsFor<Self::Context, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
self.handler
|
||||
.handle_async(HandleArgs {
|
||||
.handle_async(HandlerArgs {
|
||||
context,
|
||||
parent_method,
|
||||
method,
|
||||
@@ -418,23 +419,23 @@ impl<F, H, Context> PrintCliResult for CustomDisplayFn<F, H, Context>
|
||||
where
|
||||
Context: IntoContext,
|
||||
H: HandlerTypes,
|
||||
F: Fn(HandleArgs<Context, H>, H::Ok) -> Result<(), H::Err> + Send + Sync + Clone + 'static,
|
||||
F: Fn(HandlerArgsFor<Context, H>, H::Ok) -> Result<(), H::Err> + Send + Sync + Clone + 'static,
|
||||
{
|
||||
type Context = Context;
|
||||
fn print(
|
||||
&self,
|
||||
HandleArgs {
|
||||
HandlerArgs {
|
||||
context,
|
||||
parent_method,
|
||||
method,
|
||||
params,
|
||||
inherited_params,
|
||||
raw_params,
|
||||
}: HandleArgs<Context, Self>,
|
||||
}: HandlerArgsFor<Context, Self>,
|
||||
result: Self::Ok,
|
||||
) -> Result<(), Self::Err> {
|
||||
(self.print)(
|
||||
HandleArgs {
|
||||
HandlerArgs {
|
||||
context,
|
||||
parent_method,
|
||||
method,
|
||||
@@ -486,14 +487,14 @@ where
|
||||
type Context = EitherContext<Context, H::Context>;
|
||||
async fn handle_async(
|
||||
&self,
|
||||
HandleArgs {
|
||||
HandlerArgs {
|
||||
context,
|
||||
parent_method,
|
||||
method,
|
||||
params,
|
||||
inherited_params,
|
||||
raw_params,
|
||||
}: HandleArgs<Self::Context, Self>,
|
||||
}: HandlerArgsFor<Self::Context, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
match context {
|
||||
EitherContext::C1(context) => {
|
||||
@@ -514,7 +515,7 @@ where
|
||||
}
|
||||
EitherContext::C2(context) => {
|
||||
self.handler
|
||||
.handle_async(HandleArgs {
|
||||
.handle_async(HandlerArgs {
|
||||
context,
|
||||
parent_method,
|
||||
method,
|
||||
@@ -548,18 +549,18 @@ where
|
||||
type Context = H::Context;
|
||||
fn print(
|
||||
&self,
|
||||
HandleArgs {
|
||||
HandlerArgs {
|
||||
context,
|
||||
parent_method,
|
||||
method,
|
||||
params,
|
||||
inherited_params,
|
||||
raw_params,
|
||||
}: HandleArgs<Self::Context, Self>,
|
||||
}: HandlerArgsFor<Self::Context, Self>,
|
||||
result: Self::Ok,
|
||||
) -> Result<(), Self::Err> {
|
||||
self.handler.print(
|
||||
HandleArgs {
|
||||
HandlerArgs {
|
||||
context,
|
||||
parent_method,
|
||||
method,
|
||||
@@ -620,16 +621,16 @@ where
|
||||
type Context = H::Context;
|
||||
fn handle_sync(
|
||||
&self,
|
||||
HandleArgs {
|
||||
HandlerArgs {
|
||||
context,
|
||||
parent_method,
|
||||
method,
|
||||
params,
|
||||
inherited_params,
|
||||
raw_params,
|
||||
}: HandleArgs<Self::Context, Self>,
|
||||
}: HandlerArgsFor<Self::Context, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
self.handler.handle_sync(HandleArgs {
|
||||
self.handler.handle_sync(HandlerArgs {
|
||||
context,
|
||||
parent_method,
|
||||
method,
|
||||
@@ -640,16 +641,16 @@ where
|
||||
}
|
||||
async fn handle_async(
|
||||
&self,
|
||||
HandleArgs {
|
||||
HandlerArgs {
|
||||
context,
|
||||
parent_method,
|
||||
method,
|
||||
params,
|
||||
inherited_params,
|
||||
raw_params,
|
||||
}: HandleArgs<Self::Context, Self>,
|
||||
}: HandlerArgsFor<Self::Context, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
self.handler.handle_sync(HandleArgs {
|
||||
self.handler.handle_sync(HandlerArgs {
|
||||
context,
|
||||
parent_method,
|
||||
method,
|
||||
@@ -694,18 +695,18 @@ where
|
||||
}
|
||||
fn cli_display(
|
||||
&self,
|
||||
HandleArgs {
|
||||
HandlerArgs {
|
||||
context,
|
||||
parent_method,
|
||||
method,
|
||||
params,
|
||||
inherited_params,
|
||||
raw_params,
|
||||
}: HandleArgs<Self::Context, Self>,
|
||||
}: HandlerArgsFor<Self::Context, Self>,
|
||||
result: Self::Ok,
|
||||
) -> Result<(), Self::Err> {
|
||||
self.handler.cli_display(
|
||||
HandleArgs {
|
||||
HandlerArgs {
|
||||
context,
|
||||
parent_method,
|
||||
method,
|
||||
|
||||
@@ -2,16 +2,15 @@ use std::any::TypeId;
|
||||
use std::collections::VecDeque;
|
||||
use std::fmt::Display;
|
||||
|
||||
use clap::{ArgMatches, Command, CommandFactory, FromArgMatches};
|
||||
use futures::Future;
|
||||
use imbl_value::imbl::OrdMap;
|
||||
use imbl_value::Value;
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::util::PhantomData;
|
||||
use crate::{
|
||||
AnyContext, CliBindings, Empty, HandleArgs, Handler, HandlerTypes, IntoContext, PrintCliResult,
|
||||
AnyContext, Empty, Handler, HandlerArgs, HandlerArgsFor, HandlerTypes, IntoContext,
|
||||
PrintCliResult,
|
||||
};
|
||||
|
||||
pub struct FromFn<F, T, E, Args> {
|
||||
@@ -49,7 +48,11 @@ where
|
||||
<Self as HandlerTypes>::Ok: Display,
|
||||
{
|
||||
type Context = AnyContext;
|
||||
fn print(&self, _: HandleArgs<Self::Context, Self>, result: Self::Ok) -> Result<(), Self::Err> {
|
||||
fn print(
|
||||
&self,
|
||||
_: HandlerArgsFor<Self::Context, Self>,
|
||||
result: Self::Ok,
|
||||
) -> Result<(), Self::Err> {
|
||||
Ok(println!("{result}"))
|
||||
}
|
||||
}
|
||||
@@ -109,7 +112,11 @@ where
|
||||
<Self as HandlerTypes>::Ok: Display,
|
||||
{
|
||||
type Context = AnyContext;
|
||||
fn print(&self, _: HandleArgs<Self::Context, Self>, result: Self::Ok) -> Result<(), Self::Err> {
|
||||
fn print(
|
||||
&self,
|
||||
_: HandlerArgsFor<Self::Context, Self>,
|
||||
result: Self::Ok,
|
||||
) -> Result<(), Self::Err> {
|
||||
Ok(println!("{result}"))
|
||||
}
|
||||
}
|
||||
@@ -125,6 +132,101 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<F, T, E, Context, Params, InheritedParams> HandlerTypes
|
||||
for FromFn<F, T, E, HandlerArgs<Context, Params, InheritedParams>>
|
||||
where
|
||||
F: Fn(HandlerArgs<Context, Params, InheritedParams>) -> Result<T, E>
|
||||
+ Send
|
||||
+ Sync
|
||||
+ Clone
|
||||
+ 'static,
|
||||
T: Send + Sync + 'static,
|
||||
E: Send + Sync + 'static,
|
||||
Context: IntoContext,
|
||||
Params: Send + Sync,
|
||||
InheritedParams: Send + Sync,
|
||||
{
|
||||
type Params = Params;
|
||||
type InheritedParams = InheritedParams;
|
||||
type Ok = T;
|
||||
type Err = E;
|
||||
}
|
||||
#[async_trait::async_trait]
|
||||
impl<F, T, E, Context, Params, InheritedParams> Handler
|
||||
for FromFn<F, T, E, HandlerArgs<Context, Params, InheritedParams>>
|
||||
where
|
||||
F: Fn(HandlerArgs<Context, Params, InheritedParams>) -> Result<T, E>
|
||||
+ Send
|
||||
+ Sync
|
||||
+ Clone
|
||||
+ 'static,
|
||||
T: Send + Sync + 'static,
|
||||
E: Send + Sync + 'static,
|
||||
Context: IntoContext,
|
||||
Params: Send + Sync + 'static,
|
||||
InheritedParams: Send + Sync + 'static,
|
||||
{
|
||||
type Context = Context;
|
||||
fn handle_sync(
|
||||
&self,
|
||||
handle_args: HandlerArgsFor<Self::Context, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
(self.function)(handle_args)
|
||||
}
|
||||
async fn handle_async(
|
||||
&self,
|
||||
handle_args: HandlerArgsFor<Self::Context, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
if self.blocking {
|
||||
self.handle_async_with_sync_blocking(handle_args).await
|
||||
} else {
|
||||
self.handle_async_with_sync(handle_args).await
|
||||
}
|
||||
}
|
||||
fn metadata(&self, _: VecDeque<&'static str>, _: TypeId) -> OrdMap<&'static str, Value> {
|
||||
self.metadata.clone()
|
||||
}
|
||||
}
|
||||
impl<F, Fut, T, E, Context, Params, InheritedParams> HandlerTypes
|
||||
for FromFnAsync<F, Fut, T, E, HandlerArgs<Context, Params, InheritedParams>>
|
||||
where
|
||||
F: Fn(HandlerArgs<Context, Params, InheritedParams>) -> Fut + Send + Sync + Clone + 'static,
|
||||
Fut: Future<Output = Result<T, E>> + Send + 'static,
|
||||
T: Send + Sync + 'static,
|
||||
E: Send + Sync + 'static,
|
||||
Context: IntoContext,
|
||||
Params: Send + Sync,
|
||||
InheritedParams: Send + Sync,
|
||||
{
|
||||
type Params = Params;
|
||||
type InheritedParams = InheritedParams;
|
||||
type Ok = T;
|
||||
type Err = E;
|
||||
}
|
||||
#[async_trait::async_trait]
|
||||
impl<F, Fut, T, E, Context, Params, InheritedParams> Handler
|
||||
for FromFnAsync<F, Fut, T, E, HandlerArgs<Context, Params, InheritedParams>>
|
||||
where
|
||||
F: Fn(HandlerArgs<Context, Params, InheritedParams>) -> Fut + Send + Sync + Clone + 'static,
|
||||
Fut: Future<Output = Result<T, E>> + Send + 'static,
|
||||
T: Send + Sync + 'static,
|
||||
E: Send + Sync + 'static,
|
||||
Context: IntoContext,
|
||||
Params: Send + Sync + 'static,
|
||||
InheritedParams: Send + Sync + 'static,
|
||||
{
|
||||
type Context = Context;
|
||||
async fn handle_async(
|
||||
&self,
|
||||
handle_args: HandlerArgsFor<Self::Context, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
(self.function)(handle_args).await
|
||||
}
|
||||
fn metadata(&self, _: VecDeque<&'static str>, _: TypeId) -> OrdMap<&'static str, Value> {
|
||||
self.metadata.clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl<F, T, E> HandlerTypes for FromFn<F, T, E, ()>
|
||||
where
|
||||
F: Fn() -> Result<T, E> + Send + Sync + Clone + 'static,
|
||||
@@ -144,12 +246,12 @@ where
|
||||
E: Send + Sync + 'static,
|
||||
{
|
||||
type Context = AnyContext;
|
||||
fn handle_sync(&self, _: HandleArgs<Self::Context, Self>) -> Result<Self::Ok, Self::Err> {
|
||||
fn handle_sync(&self, _: HandlerArgsFor<Self::Context, Self>) -> Result<Self::Ok, Self::Err> {
|
||||
(self.function)()
|
||||
}
|
||||
async fn handle_async(
|
||||
&self,
|
||||
handle_args: HandleArgs<Self::Context, Self>,
|
||||
handle_args: HandlerArgsFor<Self::Context, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
if self.blocking {
|
||||
self.handle_async_with_sync_blocking(handle_args).await
|
||||
@@ -184,7 +286,7 @@ where
|
||||
type Context = AnyContext;
|
||||
async fn handle_async(
|
||||
&self,
|
||||
_: HandleArgs<Self::Context, Self>,
|
||||
_: HandlerArgsFor<Self::Context, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
(self.function)().await
|
||||
}
|
||||
@@ -216,13 +318,13 @@ where
|
||||
type Context = Context;
|
||||
fn handle_sync(
|
||||
&self,
|
||||
handle_args: HandleArgs<Self::Context, Self>,
|
||||
handle_args: HandlerArgsFor<Self::Context, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
(self.function)(handle_args.context)
|
||||
}
|
||||
async fn handle_async(
|
||||
&self,
|
||||
handle_args: HandleArgs<Self::Context, Self>,
|
||||
handle_args: HandlerArgsFor<Self::Context, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
if self.blocking {
|
||||
self.handle_async_with_sync_blocking(handle_args).await
|
||||
@@ -259,7 +361,7 @@ where
|
||||
type Context = Context;
|
||||
async fn handle_async(
|
||||
&self,
|
||||
handle_args: HandleArgs<Self::Context, Self>,
|
||||
handle_args: HandlerArgsFor<Self::Context, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
(self.function)(handle_args.context).await
|
||||
}
|
||||
@@ -293,16 +395,16 @@ where
|
||||
type Context = Context;
|
||||
fn handle_sync(
|
||||
&self,
|
||||
handle_args: HandleArgs<Self::Context, Self>,
|
||||
handle_args: HandlerArgsFor<Self::Context, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
let HandleArgs {
|
||||
let HandlerArgs {
|
||||
context, params, ..
|
||||
} = handle_args;
|
||||
(self.function)(context, params)
|
||||
}
|
||||
async fn handle_async(
|
||||
&self,
|
||||
handle_args: HandleArgs<Self::Context, Self>,
|
||||
handle_args: HandlerArgsFor<Self::Context, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
if self.blocking {
|
||||
self.handle_async_with_sync_blocking(handle_args).await
|
||||
@@ -341,9 +443,9 @@ where
|
||||
type Context = Context;
|
||||
async fn handle_async(
|
||||
&self,
|
||||
handle_args: HandleArgs<Self::Context, Self>,
|
||||
handle_args: HandlerArgsFor<Self::Context, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
let HandleArgs {
|
||||
let HandlerArgs {
|
||||
context, params, ..
|
||||
} = handle_args;
|
||||
(self.function)(context, params).await
|
||||
@@ -382,9 +484,9 @@ where
|
||||
type Context = Context;
|
||||
fn handle_sync(
|
||||
&self,
|
||||
handle_args: HandleArgs<Self::Context, Self>,
|
||||
handle_args: HandlerArgsFor<Self::Context, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
let HandleArgs {
|
||||
let HandlerArgs {
|
||||
context,
|
||||
params,
|
||||
inherited_params,
|
||||
@@ -394,7 +496,7 @@ where
|
||||
}
|
||||
async fn handle_async(
|
||||
&self,
|
||||
handle_args: HandleArgs<Self::Context, Self>,
|
||||
handle_args: HandlerArgsFor<Self::Context, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
if self.blocking {
|
||||
self.handle_async_with_sync_blocking(handle_args).await
|
||||
@@ -437,9 +539,9 @@ where
|
||||
type Context = Context;
|
||||
async fn handle_async(
|
||||
&self,
|
||||
handle_args: HandleArgs<Self::Context, Self>,
|
||||
handle_args: HandlerArgsFor<Self::Context, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
let HandleArgs {
|
||||
let HandlerArgs {
|
||||
context,
|
||||
params,
|
||||
inherited_params,
|
||||
|
||||
@@ -28,7 +28,9 @@ pub(crate) struct HandleAnyArgs {
|
||||
pub(crate) params: Value,
|
||||
}
|
||||
impl HandleAnyArgs {
|
||||
fn downcast<Context: IntoContext, H>(self) -> Result<HandleArgs<Context, H>, imbl_value::Error>
|
||||
fn downcast<Context: IntoContext, H>(
|
||||
self,
|
||||
) -> Result<HandlerArgsFor<Context, H>, imbl_value::Error>
|
||||
where
|
||||
H: HandlerTypes,
|
||||
H::Params: DeserializeOwned,
|
||||
@@ -40,7 +42,7 @@ impl HandleAnyArgs {
|
||||
method,
|
||||
params,
|
||||
} = self;
|
||||
Ok(HandleArgs {
|
||||
Ok(HandlerArgs {
|
||||
context: Context::downcast(context).map_err(|_| imbl_value::Error {
|
||||
kind: imbl_value::ErrorKind::Deserialization,
|
||||
source: serde::ser::Error::custom("context does not match expected"),
|
||||
@@ -105,7 +107,7 @@ pub trait CliBindings: HandlerTypes {
|
||||
) -> Result<(VecDeque<&'static str>, Value), clap::Error>;
|
||||
fn cli_display(
|
||||
&self,
|
||||
handle_args: HandleArgs<Self::Context, Self>,
|
||||
handle_args: HandlerArgsFor<Self::Context, Self>,
|
||||
result: Self::Ok,
|
||||
) -> Result<(), Self::Err>;
|
||||
}
|
||||
@@ -114,7 +116,7 @@ pub trait PrintCliResult: HandlerTypes {
|
||||
type Context: IntoContext;
|
||||
fn print(
|
||||
&self,
|
||||
handle_args: HandleArgs<Self::Context, Self>,
|
||||
handle_args: HandlerArgsFor<Self::Context, Self>,
|
||||
result: Self::Ok,
|
||||
) -> Result<(), Self::Err>;
|
||||
}
|
||||
@@ -144,18 +146,18 @@ where
|
||||
}
|
||||
fn cli_display(
|
||||
&self,
|
||||
HandleArgs {
|
||||
HandlerArgs {
|
||||
context,
|
||||
parent_method,
|
||||
method,
|
||||
params,
|
||||
inherited_params,
|
||||
raw_params,
|
||||
}: HandleArgs<Self::Context, Self>,
|
||||
}: HandlerArgsFor<Self::Context, Self>,
|
||||
result: Self::Ok,
|
||||
) -> Result<(), Self::Err> {
|
||||
self.print(
|
||||
HandleArgs {
|
||||
HandlerArgs {
|
||||
context,
|
||||
parent_method,
|
||||
method,
|
||||
@@ -209,13 +211,16 @@ impl HandleAny for DynHandler {
|
||||
}
|
||||
}
|
||||
|
||||
pub type HandlerArgsFor<Context: IntoContext, H: HandlerTypes + ?Sized> =
|
||||
HandlerArgs<Context, H::Params, H::InheritedParams>;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct HandleArgs<Context: IntoContext, H: HandlerTypes + ?Sized> {
|
||||
pub struct HandlerArgs<Context: IntoContext, Params: Send + Sync, InheritedParams: Send + Sync> {
|
||||
pub context: Context,
|
||||
pub parent_method: VecDeque<&'static str>,
|
||||
pub method: VecDeque<&'static str>,
|
||||
pub params: H::Params,
|
||||
pub inherited_params: H::InheritedParams,
|
||||
pub params: Params,
|
||||
pub inherited_params: InheritedParams,
|
||||
pub raw_params: Value,
|
||||
}
|
||||
|
||||
@@ -231,7 +236,7 @@ pub trait Handler: HandlerTypes + Clone + Send + Sync + 'static {
|
||||
type Context: IntoContext;
|
||||
fn handle_sync(
|
||||
&self,
|
||||
handle_args: HandleArgs<Self::Context, Self>,
|
||||
handle_args: HandlerArgsFor<Self::Context, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
handle_args
|
||||
.context
|
||||
@@ -240,17 +245,17 @@ pub trait Handler: HandlerTypes + Clone + Send + Sync + 'static {
|
||||
}
|
||||
async fn handle_async(
|
||||
&self,
|
||||
handle_args: HandleArgs<Self::Context, Self>,
|
||||
handle_args: HandlerArgsFor<Self::Context, Self>,
|
||||
) -> Result<Self::Ok, Self::Err>;
|
||||
async fn handle_async_with_sync(
|
||||
&self,
|
||||
handle_args: HandleArgs<Self::Context, Self>,
|
||||
handle_args: HandlerArgsFor<Self::Context, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
self.handle_sync(handle_args)
|
||||
}
|
||||
async fn handle_async_with_sync_blocking(
|
||||
&self,
|
||||
handle_args: HandleArgs<Self::Context, Self>,
|
||||
handle_args: HandlerArgsFor<Self::Context, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
let s = self.clone();
|
||||
handle_args
|
||||
@@ -305,7 +310,7 @@ where
|
||||
imbl_value::to_value(
|
||||
&self
|
||||
.0
|
||||
.handle_sync(handle_args.downcast().map_err(invalid_params)?)?,
|
||||
.handle_sync(handle_args.downcast::<_, H>().map_err(invalid_params)?)?,
|
||||
)
|
||||
.map_err(internal_error)
|
||||
}
|
||||
@@ -313,7 +318,7 @@ where
|
||||
imbl_value::to_value(
|
||||
&self
|
||||
.0
|
||||
.handle_async(handle_args.downcast().map_err(invalid_params)?)
|
||||
.handle_async(handle_args.downcast::<_, H>().map_err(invalid_params)?)
|
||||
.await?,
|
||||
)
|
||||
.map_err(internal_error)
|
||||
@@ -351,7 +356,7 @@ where
|
||||
fn cli_display(&self, handle_args: HandleAnyArgs, result: Value) -> Result<(), RpcError> {
|
||||
self.0
|
||||
.cli_display(
|
||||
handle_args.downcast().map_err(invalid_params)?,
|
||||
handle_args.downcast::<_, H>().map_err(invalid_params)?,
|
||||
imbl_value::from_value(result).map_err(internal_error)?,
|
||||
)
|
||||
.map_err(RpcError::from)
|
||||
|
||||
@@ -11,8 +11,8 @@ use yajrc::RpcError;
|
||||
|
||||
use crate::util::{combine, Flat, PhantomData};
|
||||
use crate::{
|
||||
AnyContext, AnyHandler, CliBindings, DynHandler, Empty, HandleAny, HandleAnyArgs, HandleArgs,
|
||||
Handler, HandlerTypes, IntoContext, OrEmpty,
|
||||
AnyContext, AnyHandler, CliBindings, DynHandler, Empty, HandleAny, HandleAnyArgs, Handler,
|
||||
HandlerArgs, HandlerArgsFor, HandlerTypes, IntoContext, OrEmpty,
|
||||
};
|
||||
|
||||
pub trait IntoHandlers: HandlerTypes {
|
||||
@@ -178,13 +178,13 @@ where
|
||||
type Context = AnyContext;
|
||||
fn handle_sync(
|
||||
&self,
|
||||
HandleArgs {
|
||||
HandlerArgs {
|
||||
context,
|
||||
mut parent_method,
|
||||
mut method,
|
||||
raw_params,
|
||||
..
|
||||
}: HandleArgs<AnyContext, Self>,
|
||||
}: HandlerArgsFor<AnyContext, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
let cmd = method.pop_front();
|
||||
if let Some(cmd) = cmd {
|
||||
@@ -203,13 +203,13 @@ where
|
||||
}
|
||||
async fn handle_async(
|
||||
&self,
|
||||
HandleArgs {
|
||||
HandlerArgs {
|
||||
context,
|
||||
mut parent_method,
|
||||
mut method,
|
||||
raw_params,
|
||||
..
|
||||
}: HandleArgs<AnyContext, Self>,
|
||||
}: HandlerArgsFor<AnyContext, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
let cmd = method.pop_front();
|
||||
if let Some(cmd) = cmd {
|
||||
@@ -322,13 +322,13 @@ where
|
||||
}
|
||||
fn cli_display(
|
||||
&self,
|
||||
HandleArgs {
|
||||
HandlerArgs {
|
||||
context,
|
||||
mut parent_method,
|
||||
mut method,
|
||||
raw_params,
|
||||
..
|
||||
}: HandleArgs<AnyContext, Self>,
|
||||
}: HandlerArgsFor<AnyContext, Self>,
|
||||
result: Self::Ok,
|
||||
) -> Result<(), Self::Err> {
|
||||
let cmd = method.pop_front();
|
||||
|
||||
Reference in New Issue
Block a user