allow full args for handler fn

This commit is contained in:
Aiden McClelland
2024-01-24 12:26:32 -07:00
parent 6894fb5b07
commit 779b02d9c3
5 changed files with 204 additions and 96 deletions

View File

@@ -13,8 +13,8 @@ use yajrc::{Id, RpcError};
use crate::util::{internal_error, parse_error, Flat, PhantomData}; use crate::util::{internal_error, parse_error, Flat, PhantomData};
use crate::{ use crate::{
AnyHandler, CliBindings, CliBindingsAny, DynHandler, HandleAny, HandleAnyArgs, HandleArgs, AnyHandler, CliBindingsAny, DynHandler, HandleAny, HandleAnyArgs, Handler, HandlerArgs,
Handler, HandlerTypes, IntoContext, Name, ParentHandler, PrintCliResult, HandlerArgsFor, HandlerTypes, IntoContext, Name, ParentHandler, PrintCliResult,
}; };
type GenericRpcMethod<'a> = yajrc::GenericRpcMethod<&'a str, Value, Value>; type GenericRpcMethod<'a> = yajrc::GenericRpcMethod<&'a str, Value, Value>;
@@ -215,7 +215,7 @@ where
type Context = Context; type Context = Context;
async fn handle_async( async fn handle_async(
&self, &self,
handle_args: HandleArgs<Context, Self>, handle_args: HandlerArgsFor<Context, Self>,
) -> Result<Self::Ok, Self::Err> { ) -> Result<Self::Ok, Self::Err> {
let full_method = handle_args let full_method = handle_args
.parent_method .parent_method
@@ -250,18 +250,18 @@ where
type Context = Context; type Context = Context;
fn print( fn print(
&self, &self,
HandleArgs { HandlerArgs {
context, context,
parent_method, parent_method,
method, method,
params, params,
inherited_params, inherited_params,
raw_params, raw_params,
}: HandleArgs<Self::Context, Self>, }: HandlerArgsFor<Self::Context, Self>,
result: Self::Ok, result: Self::Ok,
) -> Result<(), Self::Err> { ) -> Result<(), Self::Err> {
self.handler.print( self.handler.print(
HandleArgs { HandlerArgs {
context, context,
parent_method, parent_method,
method, method,

View File

@@ -13,7 +13,8 @@ use yajrc::RpcError;
use crate::util::{internal_error, parse_error, Flat, PhantomData}; use crate::util::{internal_error, parse_error, Flat, PhantomData};
use crate::{ use crate::{
iter_from_ctx_and_handler, AnyContext, AnyHandler, CallRemote, CliBindings, DynHandler, 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 { pub trait HandlerExt: Handler + Sized {
@@ -32,7 +33,7 @@ pub trait HandlerExt: Handler + Sized {
display: F, display: F,
) -> CustomDisplayFn<F, Self, Context> ) -> CustomDisplayFn<F, Self, Context>
where 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>( fn with_inherited<Params, InheritedParams, F>(
self, self,
f: F, f: F,
@@ -68,7 +69,7 @@ impl<T: Handler + Sized> HandlerExt for T {
display: F, display: F,
) -> CustomDisplayFn<F, Self, Context> ) -> CustomDisplayFn<F, Self, Context>
where where
F: Fn(HandleArgs<Context, Self>, Self::Ok) -> Result<(), Self::Err>, F: Fn(HandlerArgsFor<Context, Self>, Self::Ok) -> Result<(), Self::Err>,
{ {
CustomDisplayFn { CustomDisplayFn {
_phantom: PhantomData::new(), _phantom: PhantomData::new(),
@@ -137,16 +138,16 @@ where
type Context = H::Context; type Context = H::Context;
fn handle_sync( fn handle_sync(
&self, &self,
HandleArgs { HandlerArgs {
context, context,
parent_method, parent_method,
method, method,
params, params,
inherited_params, inherited_params,
raw_params, raw_params,
}: HandleArgs<Self::Context, Self>, }: HandlerArgsFor<Self::Context, Self>,
) -> Result<Self::Ok, Self::Err> { ) -> Result<Self::Ok, Self::Err> {
self.0.handle_sync(HandleArgs { self.0.handle_sync(HandlerArgs {
context, context,
parent_method, parent_method,
method, method,
@@ -157,17 +158,17 @@ where
} }
async fn handle_async( async fn handle_async(
&self, &self,
HandleArgs { HandlerArgs {
context, context,
parent_method, parent_method,
method, method,
params, params,
inherited_params, inherited_params,
raw_params, raw_params,
}: HandleArgs<Self::Context, Self>, }: HandlerArgsFor<Self::Context, Self>,
) -> Result<Self::Ok, Self::Err> { ) -> Result<Self::Ok, Self::Err> {
self.0 self.0
.handle_async(HandleArgs { .handle_async(HandlerArgs {
context, context,
parent_method, parent_method,
method, method,
@@ -197,7 +198,7 @@ where
H::Params: FromArgMatches + CommandFactory + Serialize, H::Params: FromArgMatches + CommandFactory + Serialize,
{ {
type Context = AnyContext; 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(()) Ok(())
} }
} }
@@ -225,16 +226,16 @@ where
type Context = H::Context; type Context = H::Context;
fn handle_sync( fn handle_sync(
&self, &self,
HandleArgs { HandlerArgs {
context, context,
parent_method, parent_method,
method, method,
params, params,
inherited_params, inherited_params,
raw_params, raw_params,
}: HandleArgs<Self::Context, Self>, }: HandlerArgsFor<Self::Context, Self>,
) -> Result<Self::Ok, Self::Err> { ) -> Result<Self::Ok, Self::Err> {
self.handler.handle_sync(HandleArgs { self.handler.handle_sync(HandlerArgs {
context, context,
parent_method, parent_method,
method, method,
@@ -245,17 +246,17 @@ where
} }
async fn handle_async( async fn handle_async(
&self, &self,
HandleArgs { HandlerArgs {
context, context,
parent_method, parent_method,
method, method,
params, params,
inherited_params, inherited_params,
raw_params, raw_params,
}: HandleArgs<Self::Context, Self>, }: HandlerArgsFor<Self::Context, Self>,
) -> Result<Self::Ok, Self::Err> { ) -> Result<Self::Ok, Self::Err> {
self.handler self.handler
.handle_async(HandleArgs { .handle_async(HandlerArgs {
context, context,
parent_method, parent_method,
method, method,
@@ -295,18 +296,18 @@ where
type Context = P::Context; type Context = P::Context;
fn print( fn print(
&self, &self,
HandleArgs { HandlerArgs {
context, context,
parent_method, parent_method,
method, method,
params, params,
inherited_params, inherited_params,
raw_params, raw_params,
}: HandleArgs<Self::Context, Self>, }: HandlerArgsFor<Self::Context, Self>,
result: Self::Ok, result: Self::Ok,
) -> Result<(), Self::Err> { ) -> Result<(), Self::Err> {
self.print.print( self.print.print(
HandleArgs { HandlerArgs {
context, context,
parent_method, parent_method,
method, method,
@@ -360,16 +361,16 @@ where
type Context = H::Context; type Context = H::Context;
fn handle_sync( fn handle_sync(
&self, &self,
HandleArgs { HandlerArgs {
context, context,
parent_method, parent_method,
method, method,
params, params,
inherited_params, inherited_params,
raw_params, raw_params,
}: HandleArgs<Self::Context, Self>, }: HandlerArgsFor<Self::Context, Self>,
) -> Result<Self::Ok, Self::Err> { ) -> Result<Self::Ok, Self::Err> {
self.handler.handle_sync(HandleArgs { self.handler.handle_sync(HandlerArgs {
context, context,
parent_method, parent_method,
method, method,
@@ -380,17 +381,17 @@ where
} }
async fn handle_async( async fn handle_async(
&self, &self,
HandleArgs { HandlerArgs {
context, context,
parent_method, parent_method,
method, method,
params, params,
inherited_params, inherited_params,
raw_params, raw_params,
}: HandleArgs<Self::Context, Self>, }: HandlerArgsFor<Self::Context, Self>,
) -> Result<Self::Ok, Self::Err> { ) -> Result<Self::Ok, Self::Err> {
self.handler self.handler
.handle_async(HandleArgs { .handle_async(HandlerArgs {
context, context,
parent_method, parent_method,
method, method,
@@ -418,23 +419,23 @@ impl<F, H, Context> PrintCliResult for CustomDisplayFn<F, H, Context>
where where
Context: IntoContext, Context: IntoContext,
H: HandlerTypes, 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; type Context = Context;
fn print( fn print(
&self, &self,
HandleArgs { HandlerArgs {
context, context,
parent_method, parent_method,
method, method,
params, params,
inherited_params, inherited_params,
raw_params, raw_params,
}: HandleArgs<Context, Self>, }: HandlerArgsFor<Context, Self>,
result: Self::Ok, result: Self::Ok,
) -> Result<(), Self::Err> { ) -> Result<(), Self::Err> {
(self.print)( (self.print)(
HandleArgs { HandlerArgs {
context, context,
parent_method, parent_method,
method, method,
@@ -486,14 +487,14 @@ where
type Context = EitherContext<Context, H::Context>; type Context = EitherContext<Context, H::Context>;
async fn handle_async( async fn handle_async(
&self, &self,
HandleArgs { HandlerArgs {
context, context,
parent_method, parent_method,
method, method,
params, params,
inherited_params, inherited_params,
raw_params, raw_params,
}: HandleArgs<Self::Context, Self>, }: HandlerArgsFor<Self::Context, Self>,
) -> Result<Self::Ok, Self::Err> { ) -> Result<Self::Ok, Self::Err> {
match context { match context {
EitherContext::C1(context) => { EitherContext::C1(context) => {
@@ -514,7 +515,7 @@ where
} }
EitherContext::C2(context) => { EitherContext::C2(context) => {
self.handler self.handler
.handle_async(HandleArgs { .handle_async(HandlerArgs {
context, context,
parent_method, parent_method,
method, method,
@@ -548,18 +549,18 @@ where
type Context = H::Context; type Context = H::Context;
fn print( fn print(
&self, &self,
HandleArgs { HandlerArgs {
context, context,
parent_method, parent_method,
method, method,
params, params,
inherited_params, inherited_params,
raw_params, raw_params,
}: HandleArgs<Self::Context, Self>, }: HandlerArgsFor<Self::Context, Self>,
result: Self::Ok, result: Self::Ok,
) -> Result<(), Self::Err> { ) -> Result<(), Self::Err> {
self.handler.print( self.handler.print(
HandleArgs { HandlerArgs {
context, context,
parent_method, parent_method,
method, method,
@@ -620,16 +621,16 @@ where
type Context = H::Context; type Context = H::Context;
fn handle_sync( fn handle_sync(
&self, &self,
HandleArgs { HandlerArgs {
context, context,
parent_method, parent_method,
method, method,
params, params,
inherited_params, inherited_params,
raw_params, raw_params,
}: HandleArgs<Self::Context, Self>, }: HandlerArgsFor<Self::Context, Self>,
) -> Result<Self::Ok, Self::Err> { ) -> Result<Self::Ok, Self::Err> {
self.handler.handle_sync(HandleArgs { self.handler.handle_sync(HandlerArgs {
context, context,
parent_method, parent_method,
method, method,
@@ -640,16 +641,16 @@ where
} }
async fn handle_async( async fn handle_async(
&self, &self,
HandleArgs { HandlerArgs {
context, context,
parent_method, parent_method,
method, method,
params, params,
inherited_params, inherited_params,
raw_params, raw_params,
}: HandleArgs<Self::Context, Self>, }: HandlerArgsFor<Self::Context, Self>,
) -> Result<Self::Ok, Self::Err> { ) -> Result<Self::Ok, Self::Err> {
self.handler.handle_sync(HandleArgs { self.handler.handle_sync(HandlerArgs {
context, context,
parent_method, parent_method,
method, method,
@@ -694,18 +695,18 @@ where
} }
fn cli_display( fn cli_display(
&self, &self,
HandleArgs { HandlerArgs {
context, context,
parent_method, parent_method,
method, method,
params, params,
inherited_params, inherited_params,
raw_params, raw_params,
}: HandleArgs<Self::Context, Self>, }: HandlerArgsFor<Self::Context, Self>,
result: Self::Ok, result: Self::Ok,
) -> Result<(), Self::Err> { ) -> Result<(), Self::Err> {
self.handler.cli_display( self.handler.cli_display(
HandleArgs { HandlerArgs {
context, context,
parent_method, parent_method,
method, method,

View File

@@ -2,16 +2,15 @@ use std::any::TypeId;
use std::collections::VecDeque; use std::collections::VecDeque;
use std::fmt::Display; use std::fmt::Display;
use clap::{ArgMatches, Command, CommandFactory, FromArgMatches};
use futures::Future; use futures::Future;
use imbl_value::imbl::OrdMap; use imbl_value::imbl::OrdMap;
use imbl_value::Value; use imbl_value::Value;
use serde::de::DeserializeOwned; use serde::de::DeserializeOwned;
use serde::Serialize;
use crate::util::PhantomData; use crate::util::PhantomData;
use crate::{ 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> { pub struct FromFn<F, T, E, Args> {
@@ -49,7 +48,11 @@ where
<Self as HandlerTypes>::Ok: Display, <Self as HandlerTypes>::Ok: Display,
{ {
type Context = AnyContext; 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}")) Ok(println!("{result}"))
} }
} }
@@ -109,7 +112,11 @@ where
<Self as HandlerTypes>::Ok: Display, <Self as HandlerTypes>::Ok: Display,
{ {
type Context = AnyContext; 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}")) 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, ()> impl<F, T, E> HandlerTypes for FromFn<F, T, E, ()>
where where
F: Fn() -> Result<T, E> + Send + Sync + Clone + 'static, F: Fn() -> Result<T, E> + Send + Sync + Clone + 'static,
@@ -144,12 +246,12 @@ where
E: Send + Sync + 'static, E: Send + Sync + 'static,
{ {
type Context = AnyContext; 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)() (self.function)()
} }
async fn handle_async( async fn handle_async(
&self, &self,
handle_args: HandleArgs<Self::Context, Self>, handle_args: HandlerArgsFor<Self::Context, Self>,
) -> Result<Self::Ok, Self::Err> { ) -> Result<Self::Ok, Self::Err> {
if self.blocking { if self.blocking {
self.handle_async_with_sync_blocking(handle_args).await self.handle_async_with_sync_blocking(handle_args).await
@@ -184,7 +286,7 @@ where
type Context = AnyContext; type Context = AnyContext;
async fn handle_async( async fn handle_async(
&self, &self,
_: HandleArgs<Self::Context, Self>, _: HandlerArgsFor<Self::Context, Self>,
) -> Result<Self::Ok, Self::Err> { ) -> Result<Self::Ok, Self::Err> {
(self.function)().await (self.function)().await
} }
@@ -216,13 +318,13 @@ where
type Context = Context; type Context = Context;
fn handle_sync( fn handle_sync(
&self, &self,
handle_args: HandleArgs<Self::Context, Self>, handle_args: HandlerArgsFor<Self::Context, Self>,
) -> Result<Self::Ok, Self::Err> { ) -> Result<Self::Ok, Self::Err> {
(self.function)(handle_args.context) (self.function)(handle_args.context)
} }
async fn handle_async( async fn handle_async(
&self, &self,
handle_args: HandleArgs<Self::Context, Self>, handle_args: HandlerArgsFor<Self::Context, Self>,
) -> Result<Self::Ok, Self::Err> { ) -> Result<Self::Ok, Self::Err> {
if self.blocking { if self.blocking {
self.handle_async_with_sync_blocking(handle_args).await self.handle_async_with_sync_blocking(handle_args).await
@@ -259,7 +361,7 @@ where
type Context = Context; type Context = Context;
async fn handle_async( async fn handle_async(
&self, &self,
handle_args: HandleArgs<Self::Context, Self>, handle_args: HandlerArgsFor<Self::Context, Self>,
) -> Result<Self::Ok, Self::Err> { ) -> Result<Self::Ok, Self::Err> {
(self.function)(handle_args.context).await (self.function)(handle_args.context).await
} }
@@ -293,16 +395,16 @@ where
type Context = Context; type Context = Context;
fn handle_sync( fn handle_sync(
&self, &self,
handle_args: HandleArgs<Self::Context, Self>, handle_args: HandlerArgsFor<Self::Context, Self>,
) -> Result<Self::Ok, Self::Err> { ) -> Result<Self::Ok, Self::Err> {
let HandleArgs { let HandlerArgs {
context, params, .. context, params, ..
} = handle_args; } = handle_args;
(self.function)(context, params) (self.function)(context, params)
} }
async fn handle_async( async fn handle_async(
&self, &self,
handle_args: HandleArgs<Self::Context, Self>, handle_args: HandlerArgsFor<Self::Context, Self>,
) -> Result<Self::Ok, Self::Err> { ) -> Result<Self::Ok, Self::Err> {
if self.blocking { if self.blocking {
self.handle_async_with_sync_blocking(handle_args).await self.handle_async_with_sync_blocking(handle_args).await
@@ -341,9 +443,9 @@ where
type Context = Context; type Context = Context;
async fn handle_async( async fn handle_async(
&self, &self,
handle_args: HandleArgs<Self::Context, Self>, handle_args: HandlerArgsFor<Self::Context, Self>,
) -> Result<Self::Ok, Self::Err> { ) -> Result<Self::Ok, Self::Err> {
let HandleArgs { let HandlerArgs {
context, params, .. context, params, ..
} = handle_args; } = handle_args;
(self.function)(context, params).await (self.function)(context, params).await
@@ -382,9 +484,9 @@ where
type Context = Context; type Context = Context;
fn handle_sync( fn handle_sync(
&self, &self,
handle_args: HandleArgs<Self::Context, Self>, handle_args: HandlerArgsFor<Self::Context, Self>,
) -> Result<Self::Ok, Self::Err> { ) -> Result<Self::Ok, Self::Err> {
let HandleArgs { let HandlerArgs {
context, context,
params, params,
inherited_params, inherited_params,
@@ -394,7 +496,7 @@ where
} }
async fn handle_async( async fn handle_async(
&self, &self,
handle_args: HandleArgs<Self::Context, Self>, handle_args: HandlerArgsFor<Self::Context, Self>,
) -> Result<Self::Ok, Self::Err> { ) -> Result<Self::Ok, Self::Err> {
if self.blocking { if self.blocking {
self.handle_async_with_sync_blocking(handle_args).await self.handle_async_with_sync_blocking(handle_args).await
@@ -437,9 +539,9 @@ where
type Context = Context; type Context = Context;
async fn handle_async( async fn handle_async(
&self, &self,
handle_args: HandleArgs<Self::Context, Self>, handle_args: HandlerArgsFor<Self::Context, Self>,
) -> Result<Self::Ok, Self::Err> { ) -> Result<Self::Ok, Self::Err> {
let HandleArgs { let HandlerArgs {
context, context,
params, params,
inherited_params, inherited_params,

View File

@@ -28,7 +28,9 @@ pub(crate) struct HandleAnyArgs {
pub(crate) params: Value, pub(crate) params: Value,
} }
impl HandleAnyArgs { 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 where
H: HandlerTypes, H: HandlerTypes,
H::Params: DeserializeOwned, H::Params: DeserializeOwned,
@@ -40,7 +42,7 @@ impl HandleAnyArgs {
method, method,
params, params,
} = self; } = self;
Ok(HandleArgs { Ok(HandlerArgs {
context: Context::downcast(context).map_err(|_| imbl_value::Error { context: Context::downcast(context).map_err(|_| imbl_value::Error {
kind: imbl_value::ErrorKind::Deserialization, kind: imbl_value::ErrorKind::Deserialization,
source: serde::ser::Error::custom("context does not match expected"), 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>; ) -> Result<(VecDeque<&'static str>, Value), clap::Error>;
fn cli_display( fn cli_display(
&self, &self,
handle_args: HandleArgs<Self::Context, Self>, handle_args: HandlerArgsFor<Self::Context, Self>,
result: Self::Ok, result: Self::Ok,
) -> Result<(), Self::Err>; ) -> Result<(), Self::Err>;
} }
@@ -114,7 +116,7 @@ pub trait PrintCliResult: HandlerTypes {
type Context: IntoContext; type Context: IntoContext;
fn print( fn print(
&self, &self,
handle_args: HandleArgs<Self::Context, Self>, handle_args: HandlerArgsFor<Self::Context, Self>,
result: Self::Ok, result: Self::Ok,
) -> Result<(), Self::Err>; ) -> Result<(), Self::Err>;
} }
@@ -144,18 +146,18 @@ where
} }
fn cli_display( fn cli_display(
&self, &self,
HandleArgs { HandlerArgs {
context, context,
parent_method, parent_method,
method, method,
params, params,
inherited_params, inherited_params,
raw_params, raw_params,
}: HandleArgs<Self::Context, Self>, }: HandlerArgsFor<Self::Context, Self>,
result: Self::Ok, result: Self::Ok,
) -> Result<(), Self::Err> { ) -> Result<(), Self::Err> {
self.print( self.print(
HandleArgs { HandlerArgs {
context, context,
parent_method, parent_method,
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)] #[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 context: Context,
pub parent_method: VecDeque<&'static str>, pub parent_method: VecDeque<&'static str>,
pub method: VecDeque<&'static str>, pub method: VecDeque<&'static str>,
pub params: H::Params, pub params: Params,
pub inherited_params: H::InheritedParams, pub inherited_params: InheritedParams,
pub raw_params: Value, pub raw_params: Value,
} }
@@ -231,7 +236,7 @@ pub trait Handler: HandlerTypes + Clone + Send + Sync + 'static {
type Context: IntoContext; type Context: IntoContext;
fn handle_sync( fn handle_sync(
&self, &self,
handle_args: HandleArgs<Self::Context, Self>, handle_args: HandlerArgsFor<Self::Context, Self>,
) -> Result<Self::Ok, Self::Err> { ) -> Result<Self::Ok, Self::Err> {
handle_args handle_args
.context .context
@@ -240,17 +245,17 @@ pub trait Handler: HandlerTypes + Clone + Send + Sync + 'static {
} }
async fn handle_async( async fn handle_async(
&self, &self,
handle_args: HandleArgs<Self::Context, Self>, handle_args: HandlerArgsFor<Self::Context, Self>,
) -> Result<Self::Ok, Self::Err>; ) -> Result<Self::Ok, Self::Err>;
async fn handle_async_with_sync( async fn handle_async_with_sync(
&self, &self,
handle_args: HandleArgs<Self::Context, Self>, handle_args: HandlerArgsFor<Self::Context, Self>,
) -> Result<Self::Ok, Self::Err> { ) -> Result<Self::Ok, Self::Err> {
self.handle_sync(handle_args) self.handle_sync(handle_args)
} }
async fn handle_async_with_sync_blocking( async fn handle_async_with_sync_blocking(
&self, &self,
handle_args: HandleArgs<Self::Context, Self>, handle_args: HandlerArgsFor<Self::Context, Self>,
) -> Result<Self::Ok, Self::Err> { ) -> Result<Self::Ok, Self::Err> {
let s = self.clone(); let s = self.clone();
handle_args handle_args
@@ -305,7 +310,7 @@ where
imbl_value::to_value( imbl_value::to_value(
&self &self
.0 .0
.handle_sync(handle_args.downcast().map_err(invalid_params)?)?, .handle_sync(handle_args.downcast::<_, H>().map_err(invalid_params)?)?,
) )
.map_err(internal_error) .map_err(internal_error)
} }
@@ -313,7 +318,7 @@ where
imbl_value::to_value( imbl_value::to_value(
&self &self
.0 .0
.handle_async(handle_args.downcast().map_err(invalid_params)?) .handle_async(handle_args.downcast::<_, H>().map_err(invalid_params)?)
.await?, .await?,
) )
.map_err(internal_error) .map_err(internal_error)
@@ -351,7 +356,7 @@ where
fn cli_display(&self, handle_args: HandleAnyArgs, result: Value) -> Result<(), RpcError> { fn cli_display(&self, handle_args: HandleAnyArgs, result: Value) -> Result<(), RpcError> {
self.0 self.0
.cli_display( .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)?, imbl_value::from_value(result).map_err(internal_error)?,
) )
.map_err(RpcError::from) .map_err(RpcError::from)

View File

@@ -11,8 +11,8 @@ use yajrc::RpcError;
use crate::util::{combine, Flat, PhantomData}; use crate::util::{combine, Flat, PhantomData};
use crate::{ use crate::{
AnyContext, AnyHandler, CliBindings, DynHandler, Empty, HandleAny, HandleAnyArgs, HandleArgs, AnyContext, AnyHandler, CliBindings, DynHandler, Empty, HandleAny, HandleAnyArgs, Handler,
Handler, HandlerTypes, IntoContext, OrEmpty, HandlerArgs, HandlerArgsFor, HandlerTypes, IntoContext, OrEmpty,
}; };
pub trait IntoHandlers: HandlerTypes { pub trait IntoHandlers: HandlerTypes {
@@ -178,13 +178,13 @@ where
type Context = AnyContext; type Context = AnyContext;
fn handle_sync( fn handle_sync(
&self, &self,
HandleArgs { HandlerArgs {
context, context,
mut parent_method, mut parent_method,
mut method, mut method,
raw_params, raw_params,
.. ..
}: HandleArgs<AnyContext, Self>, }: HandlerArgsFor<AnyContext, Self>,
) -> Result<Self::Ok, Self::Err> { ) -> Result<Self::Ok, Self::Err> {
let cmd = method.pop_front(); let cmd = method.pop_front();
if let Some(cmd) = cmd { if let Some(cmd) = cmd {
@@ -203,13 +203,13 @@ where
} }
async fn handle_async( async fn handle_async(
&self, &self,
HandleArgs { HandlerArgs {
context, context,
mut parent_method, mut parent_method,
mut method, mut method,
raw_params, raw_params,
.. ..
}: HandleArgs<AnyContext, Self>, }: HandlerArgsFor<AnyContext, Self>,
) -> Result<Self::Ok, Self::Err> { ) -> Result<Self::Ok, Self::Err> {
let cmd = method.pop_front(); let cmd = method.pop_front();
if let Some(cmd) = cmd { if let Some(cmd) = cmd {
@@ -322,13 +322,13 @@ where
} }
fn cli_display( fn cli_display(
&self, &self,
HandleArgs { HandlerArgs {
context, context,
mut parent_method, mut parent_method,
mut method, mut method,
raw_params, raw_params,
.. ..
}: HandleArgs<AnyContext, Self>, }: HandlerArgsFor<AnyContext, Self>,
result: Self::Ok, result: Self::Ok,
) -> Result<(), Self::Err> { ) -> Result<(), Self::Err> {
let cmd = method.pop_front(); let cmd = method.pop_front();