diff --git a/rpc-toolkit/src/cli.rs b/rpc-toolkit/src/cli.rs index bb84a25..9067edb 100644 --- a/rpc-toolkit/src/cli.rs +++ b/rpc-toolkit/src/cli.rs @@ -14,7 +14,7 @@ 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, + Handler, HandlerTypes, IntoContext, Name, ParentHandler, PrintCliResult, }; type GenericRpcMethod<'a> = yajrc::GenericRpcMethod<&'a str, Value, Value>; @@ -238,26 +238,17 @@ where } } } -impl CliBindings for CallRemoteHandler +impl PrintCliResult + for CallRemoteHandler where - RemoteHandler: Handler + CliBindings, + RemoteHandler: PrintCliResult, RemoteHandler::Params: Serialize, RemoteHandler::InheritedParams: Serialize, RemoteHandler::Ok: DeserializeOwned, RemoteHandler::Err: From, { type Context = Context; - fn cli_command(&self, ctx_ty: TypeId) -> clap::Command { - self.handler.cli_command(ctx_ty) - } - fn cli_parse( - &self, - matches: &clap::ArgMatches, - ctx_ty: TypeId, - ) -> Result<(std::collections::VecDeque<&'static str>, Value), clap::Error> { - self.handler.cli_parse(matches, ctx_ty) - } - fn cli_display( + fn print( &self, HandleArgs { context, @@ -266,10 +257,10 @@ where params, inherited_params, raw_params, - }: HandleArgs, + }: HandleArgs, result: Self::Ok, ) -> Result<(), Self::Err> { - self.handler.cli_display( + self.handler.print( HandleArgs { context, parent_method, diff --git a/rpc-toolkit/src/handler/adapters.rs b/rpc-toolkit/src/handler/adapters.rs index a5659c5..6d711a1 100644 --- a/rpc-toolkit/src/handler/adapters.rs +++ b/rpc-toolkit/src/handler/adapters.rs @@ -191,33 +191,13 @@ where self.0.method_from_dots(method, ctx_ty) } } -impl CliBindings for NoDisplay +impl PrintCliResult 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> { + fn print(&self, _: HandleArgs, _: Self::Ok) -> Result<(), Self::Err> { Ok(()) } } @@ -299,10 +279,9 @@ where self.handler.method_from_dots(method, ctx_ty) } } -impl CliBindings for CustomDisplay +impl PrintCliResult for CustomDisplay where H: HandlerTypes, - H::Params: FromArgMatches + CommandFactory + Serialize, P: PrintCliResult< Params = H::Params, InheritedParams = H::InheritedParams, @@ -314,23 +293,7 @@ where + 'static, { type Context = P::Context; - 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( + fn print( &self, HandleArgs { context, @@ -451,31 +414,14 @@ where self.handler.method_from_dots(method, ctx_ty) } } -impl CliBindings for CustomDisplayFn +impl PrintCliResult for CustomDisplayFn where Context: IntoContext, H: HandlerTypes, - H::Params: FromArgMatches + CommandFactory + Serialize, F: Fn(HandleArgs, H::Ok) -> Result<(), H::Err> + Send + Sync + Clone + 'static, { type Context = Context; - 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( + fn print( &self, HandleArgs { context, @@ -594,22 +540,13 @@ where self.handler.method_from_dots(method, ctx_ty) } } -impl CliBindings for RemoteCli +impl PrintCliResult for RemoteCli where - H: CliBindings, + Context: IntoContext, + H: PrintCliResult, { type Context = H::Context; - fn cli_command(&self, ctx_ty: TypeId) -> clap::Command { - self.handler.cli_command(ctx_ty) - } - fn cli_parse( - &self, - matches: &clap::ArgMatches, - ctx_ty: TypeId, - ) -> Result<(VecDeque<&'static str>, Value), clap::Error> { - self.handler.cli_parse(matches, ctx_ty) - } - fn cli_display( + fn print( &self, HandleArgs { context, @@ -621,7 +558,7 @@ where }: HandleArgs, result: Self::Ok, ) -> Result<(), Self::Err> { - self.handler.cli_display( + self.handler.print( HandleArgs { context, parent_method, diff --git a/rpc-toolkit/src/handler/from_fn.rs b/rpc-toolkit/src/handler/from_fn.rs index b648dcf..fc3d5c5 100644 --- a/rpc-toolkit/src/handler/from_fn.rs +++ b/rpc-toolkit/src/handler/from_fn.rs @@ -451,101 +451,3 @@ where self.metadata.clone() } } - -impl CliBindings for FromFn -where - Self: HandlerTypes, - Self::Params: FromArgMatches + CommandFactory + Serialize, - Self: PrintCliResult, -{ - type Context = ::Context; - fn cli_command(&self, _: TypeId) -> Command { - Self::Params::command() - } - fn cli_parse( - &self, - matches: &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 { - context, - parent_method, - method, - params, - inherited_params, - raw_params, - }: HandleArgs, - result: Self::Ok, - ) -> Result<(), Self::Err> { - self.print( - HandleArgs { - context, - parent_method, - method, - params, - inherited_params, - raw_params, - }, - result, - ) - } -} - -impl CliBindings for FromFnAsync -where - Self: HandlerTypes, - Self::Params: FromArgMatches + CommandFactory + Serialize, - Self: PrintCliResult, -{ - type Context = ::Context; - fn cli_command(&self, _: TypeId) -> Command { - Self::Params::command() - } - fn cli_parse( - &self, - matches: &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 { - context, - parent_method, - method, - params, - inherited_params, - raw_params, - }: HandleArgs, - result: Self::Ok, - ) -> Result<(), Self::Err> { - self.print( - HandleArgs { - context, - parent_method, - method, - params, - inherited_params, - raw_params, - }, - result, - ) - } -} diff --git a/rpc-toolkit/src/handler/mod.rs b/rpc-toolkit/src/handler/mod.rs index d4e58ed..6ea8c73 100644 --- a/rpc-toolkit/src/handler/mod.rs +++ b/rpc-toolkit/src/handler/mod.rs @@ -3,7 +3,7 @@ use std::collections::VecDeque; use std::ops::Deref; use std::sync::Arc; -use clap::{ArgMatches, Command, Parser}; +use clap::{ArgMatches, Command, CommandFactory, FromArgMatches, Parser}; use imbl_value::imbl::{OrdMap, OrdSet}; use imbl_value::Value; use serde::de::DeserializeOwned; @@ -119,6 +119,55 @@ pub trait PrintCliResult: HandlerTypes { ) -> Result<(), Self::Err>; } +impl CliBindings for T +where + T: HandlerTypes, + T::Params: CommandFactory + FromArgMatches + Serialize, + T: PrintCliResult, +{ + type Context = T::Context; + fn cli_command(&self, _: TypeId) -> clap::Command { + Self::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 { + context, + parent_method, + method, + params, + inherited_params, + raw_params, + }: HandleArgs, + result: Self::Ok, + ) -> Result<(), Self::Err> { + self.print( + HandleArgs { + context, + parent_method, + method, + params, + inherited_params, + raw_params, + }, + result, + ) + } +} + pub(crate) trait HandleAnyWithCli: HandleAny + CliBindingsAny {} impl HandleAnyWithCli for T {}