From 4bcaf67a0376fb8ab80a3fa287fbd0a040902528 Mon Sep 17 00:00:00 2001 From: Aiden McClelland Date: Tue, 2 Jan 2024 13:14:32 -0700 Subject: [PATCH] add no_display adapter --- rpc-toolkit/src/handler/adapters.rs | 113 +++++++++++++++++++++++++++- 1 file changed, 109 insertions(+), 4 deletions(-) diff --git a/rpc-toolkit/src/handler/adapters.rs b/rpc-toolkit/src/handler/adapters.rs index edd50fc..d92e4ad 100644 --- a/rpc-toolkit/src/handler/adapters.rs +++ b/rpc-toolkit/src/handler/adapters.rs @@ -13,12 +13,13 @@ use yajrc::RpcError; use crate::util::{internal_error, parse_error, Flat}; use crate::{ - iter_from_ctx_and_handler, AnyHandler, CallRemote, CliBindings, DynHandler, EitherContext, - HandleArgs, Handler, HandlerTypes, IntoContext, IntoHandlers, PrintCliResult, + iter_from_ctx_and_handler, AnyContext, AnyHandler, CallRemote, CliBindings, DynHandler, + EitherContext, HandleArgs, Handler, HandlerTypes, IntoContext, IntoHandlers, PrintCliResult, }; -pub trait HandlerExt: HandlerTypes + Sized { +pub trait HandlerExt: Handler + Sized { fn no_cli(self) -> NoCli; + fn no_display(self) -> NoDisplay; fn with_custom_display

(self, display: P) -> CustomDisplay where P: PrintCliResult< @@ -42,10 +43,13 @@ pub trait HandlerExt: HandlerTypes + Sized { fn with_remote_cli(self) -> RemoteCli; } -impl HandlerExt for T { +impl HandlerExt for T { fn no_cli(self) -> NoCli { NoCli(self) } + fn no_display(self) -> NoDisplay { + NoDisplay(self) + } fn with_custom_display

(self, display: P) -> CustomDisplay where P: PrintCliResult< @@ -118,6 +122,107 @@ where } } +#[derive(Debug, Clone)] +pub struct NoDisplay(pub H); +impl HandlerTypes for NoDisplay { + type Params = H::Params; + type InheritedParams = H::InheritedParams; + type Ok = H::Ok; + type Err = H::Err; +} +#[async_trait::async_trait] +impl Handler for NoDisplay +where + H: Handler, +{ + type Context = H::Context; + fn handle_sync( + &self, + HandleArgs { + context, + parent_method, + method, + params, + inherited_params, + raw_params, + }: HandleArgs, + ) -> Result { + self.0.handle_sync(HandleArgs { + context, + parent_method, + method, + params, + inherited_params, + raw_params, + }) + } + async fn handle_async( + &self, + HandleArgs { + context, + parent_method, + method, + params, + inherited_params, + raw_params, + }: HandleArgs, + ) -> Result { + self.0 + .handle_async(HandleArgs { + context, + parent_method, + method, + params, + inherited_params, + raw_params, + }) + .await + } + fn metadata( + &self, + method: VecDeque<&'static str>, + ctx_ty: TypeId, + ) -> OrdMap<&'static str, Value> { + self.0.metadata(method, ctx_ty) + } + fn contexts(&self) -> Option> { + self.0.contexts() + } + fn method_from_dots(&self, method: &str, ctx_ty: TypeId) -> Option> { + self.0.method_from_dots(method, ctx_ty) + } +} +impl CliBindings for NoDisplay +where + H: HandlerTypes, + H::Params: FromArgMatches + CommandFactory + Serialize, +{ + type Context = AnyContext; + fn cli_command(&self, _: TypeId) -> clap::Command { + H::Params::command() + } + fn cli_parse( + &self, + matches: &clap::ArgMatches, + _: TypeId, + ) -> Result<(VecDeque<&'static str>, Value), clap::Error> { + Self::Params::from_arg_matches(matches).and_then(|a| { + Ok(( + VecDeque::new(), + imbl_value::to_value(&a) + .map_err(|e| clap::Error::raw(clap::error::ErrorKind::ValueValidation, e))?, + )) + }) + } + fn cli_display( + &self, + _: HandleArgs, + _: Self::Ok, + ) -> Result<(), Self::Err> { + Ok(()) + } +} + #[derive(Clone, Debug)] pub struct CustomDisplay { print: P,