diff --git a/rpc-toolkit/src/cli.rs b/rpc-toolkit/src/cli.rs index 9067edb..fe59b82 100644 --- a/rpc-toolkit/src/cli.rs +++ b/rpc-toolkit/src/cli.rs @@ -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, + handle_args: HandlerArgsFor, ) -> Result { 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, + }: HandlerArgsFor, result: Self::Ok, ) -> Result<(), Self::Err> { self.handler.print( - HandleArgs { + HandlerArgs { context, parent_method, method, diff --git a/rpc-toolkit/src/handler/adapters.rs b/rpc-toolkit/src/handler/adapters.rs index 0c8beab..9fe71ed 100644 --- a/rpc-toolkit/src/handler/adapters.rs +++ b/rpc-toolkit/src/handler/adapters.rs @@ -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 where - F: Fn(HandleArgs, Self::Ok) -> Result<(), Self::Err>; + F: Fn(HandlerArgsFor, Self::Ok) -> Result<(), Self::Err>; fn with_inherited( self, f: F, @@ -68,7 +69,7 @@ impl HandlerExt for T { display: F, ) -> CustomDisplayFn where - F: Fn(HandleArgs, Self::Ok) -> Result<(), Self::Err>, + F: Fn(HandlerArgsFor, 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, + }: HandlerArgsFor, ) -> Result { - 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, + }: HandlerArgsFor, ) -> Result { 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::Ok) -> Result<(), Self::Err> { + fn print(&self, _: HandlerArgsFor, _: 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, + }: HandlerArgsFor, ) -> Result { - 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, + }: HandlerArgsFor, ) -> Result { 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, + }: HandlerArgsFor, 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, + }: HandlerArgsFor, ) -> Result { - 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, + }: HandlerArgsFor, ) -> Result { self.handler - .handle_async(HandleArgs { + .handle_async(HandlerArgs { context, parent_method, method, @@ -418,23 +419,23 @@ impl PrintCliResult for CustomDisplayFn where Context: IntoContext, H: HandlerTypes, - F: Fn(HandleArgs, H::Ok) -> Result<(), H::Err> + Send + Sync + Clone + 'static, + F: Fn(HandlerArgsFor, 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, + }: HandlerArgsFor, result: Self::Ok, ) -> Result<(), Self::Err> { (self.print)( - HandleArgs { + HandlerArgs { context, parent_method, method, @@ -486,14 +487,14 @@ where type Context = EitherContext; async fn handle_async( &self, - HandleArgs { + HandlerArgs { context, parent_method, method, params, inherited_params, raw_params, - }: HandleArgs, + }: HandlerArgsFor, ) -> Result { 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, + }: HandlerArgsFor, 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, + }: HandlerArgsFor, ) -> Result { - 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, + }: HandlerArgsFor, ) -> Result { - 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, + }: HandlerArgsFor, result: Self::Ok, ) -> Result<(), Self::Err> { self.handler.cli_display( - HandleArgs { + HandlerArgs { context, parent_method, method, diff --git a/rpc-toolkit/src/handler/from_fn.rs b/rpc-toolkit/src/handler/from_fn.rs index fc3d5c5..a8b6777 100644 --- a/rpc-toolkit/src/handler/from_fn.rs +++ b/rpc-toolkit/src/handler/from_fn.rs @@ -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 { @@ -49,7 +48,11 @@ where ::Ok: Display, { type Context = AnyContext; - fn print(&self, _: HandleArgs, result: Self::Ok) -> Result<(), Self::Err> { + fn print( + &self, + _: HandlerArgsFor, + result: Self::Ok, + ) -> Result<(), Self::Err> { Ok(println!("{result}")) } } @@ -109,7 +112,11 @@ where ::Ok: Display, { type Context = AnyContext; - fn print(&self, _: HandleArgs, result: Self::Ok) -> Result<(), Self::Err> { + fn print( + &self, + _: HandlerArgsFor, + result: Self::Ok, + ) -> Result<(), Self::Err> { Ok(println!("{result}")) } } @@ -125,6 +132,101 @@ where } } +impl HandlerTypes + for FromFn> +where + F: Fn(HandlerArgs) -> Result + + 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 Handler + for FromFn> +where + F: Fn(HandlerArgs) -> Result + + 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, + ) -> Result { + (self.function)(handle_args) + } + async fn handle_async( + &self, + handle_args: HandlerArgsFor, + ) -> Result { + 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 HandlerTypes + for FromFnAsync> +where + F: Fn(HandlerArgs) -> Fut + Send + Sync + Clone + 'static, + Fut: Future> + 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 Handler + for FromFnAsync> +where + F: Fn(HandlerArgs) -> Fut + Send + Sync + Clone + 'static, + Fut: Future> + 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, + ) -> Result { + (self.function)(handle_args).await + } + fn metadata(&self, _: VecDeque<&'static str>, _: TypeId) -> OrdMap<&'static str, Value> { + self.metadata.clone() + } +} + impl HandlerTypes for FromFn where F: Fn() -> Result + Send + Sync + Clone + 'static, @@ -144,12 +246,12 @@ where E: Send + Sync + 'static, { type Context = AnyContext; - fn handle_sync(&self, _: HandleArgs) -> Result { + fn handle_sync(&self, _: HandlerArgsFor) -> Result { (self.function)() } async fn handle_async( &self, - handle_args: HandleArgs, + handle_args: HandlerArgsFor, ) -> Result { 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, + _: HandlerArgsFor, ) -> Result { (self.function)().await } @@ -216,13 +318,13 @@ where type Context = Context; fn handle_sync( &self, - handle_args: HandleArgs, + handle_args: HandlerArgsFor, ) -> Result { (self.function)(handle_args.context) } async fn handle_async( &self, - handle_args: HandleArgs, + handle_args: HandlerArgsFor, ) -> Result { 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, + handle_args: HandlerArgsFor, ) -> Result { (self.function)(handle_args.context).await } @@ -293,16 +395,16 @@ where type Context = Context; fn handle_sync( &self, - handle_args: HandleArgs, + handle_args: HandlerArgsFor, ) -> Result { - let HandleArgs { + let HandlerArgs { context, params, .. } = handle_args; (self.function)(context, params) } async fn handle_async( &self, - handle_args: HandleArgs, + handle_args: HandlerArgsFor, ) -> Result { 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, + handle_args: HandlerArgsFor, ) -> Result { - 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, + handle_args: HandlerArgsFor, ) -> Result { - let HandleArgs { + let HandlerArgs { context, params, inherited_params, @@ -394,7 +496,7 @@ where } async fn handle_async( &self, - handle_args: HandleArgs, + handle_args: HandlerArgsFor, ) -> Result { 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, + handle_args: HandlerArgsFor, ) -> Result { - let HandleArgs { + let HandlerArgs { context, params, inherited_params, diff --git a/rpc-toolkit/src/handler/mod.rs b/rpc-toolkit/src/handler/mod.rs index 6ea8c73..741621f 100644 --- a/rpc-toolkit/src/handler/mod.rs +++ b/rpc-toolkit/src/handler/mod.rs @@ -28,7 +28,9 @@ pub(crate) struct HandleAnyArgs { pub(crate) params: Value, } impl HandleAnyArgs { - fn downcast(self) -> Result, imbl_value::Error> + fn downcast( + self, + ) -> Result, 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, + handle_args: HandlerArgsFor, result: Self::Ok, ) -> Result<(), Self::Err>; } @@ -114,7 +116,7 @@ pub trait PrintCliResult: HandlerTypes { type Context: IntoContext; fn print( &self, - handle_args: HandleArgs, + handle_args: HandlerArgsFor, 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, + }: HandlerArgsFor, 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 = + HandlerArgs; + #[derive(Debug, Clone)] -pub struct HandleArgs { +pub struct HandlerArgs { 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, + handle_args: HandlerArgsFor, ) -> Result { handle_args .context @@ -240,17 +245,17 @@ pub trait Handler: HandlerTypes + Clone + Send + Sync + 'static { } async fn handle_async( &self, - handle_args: HandleArgs, + handle_args: HandlerArgsFor, ) -> Result; async fn handle_async_with_sync( &self, - handle_args: HandleArgs, + handle_args: HandlerArgsFor, ) -> Result { self.handle_sync(handle_args) } async fn handle_async_with_sync_blocking( &self, - handle_args: HandleArgs, + handle_args: HandlerArgsFor, ) -> Result { 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) diff --git a/rpc-toolkit/src/handler/parent.rs b/rpc-toolkit/src/handler/parent.rs index e5b8bbb..381ca2d 100644 --- a/rpc-toolkit/src/handler/parent.rs +++ b/rpc-toolkit/src/handler/parent.rs @@ -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, + }: HandlerArgsFor, ) -> Result { 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, + }: HandlerArgsFor, ) -> Result { 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, + }: HandlerArgsFor, result: Self::Ok, ) -> Result<(), Self::Err> { let cmd = method.pop_front();