From 8128b35e57d261f4c2e3b65ccdddd2e756a1445c Mon Sep 17 00:00:00 2001 From: J H Date: Fri, 20 Sep 2024 10:58:53 -0600 Subject: [PATCH] FEat: Add with about --- src/handler/adapters.rs | 112 +++++++++++++++++++++++++++++++++++++++- tests/handler.rs | 17 ++---- tests/test.rs | 6 +-- 3 files changed, 118 insertions(+), 17 deletions(-) diff --git a/src/handler/adapters.rs b/src/handler/adapters.rs index 00a49ca..ba61316 100644 --- a/src/handler/adapters.rs +++ b/src/handler/adapters.rs @@ -2,7 +2,10 @@ use std::any::TypeId; use std::collections::VecDeque; use std::fmt::Debug; -use clap::{CommandFactory, FromArgMatches}; +use clap::{ + builder::{IntoResettable, StyledStr}, + CommandFactory, FromArgMatches, +}; use imbl_value::imbl::OrdMap; use imbl_value::Value; use serde::de::DeserializeOwned; @@ -40,6 +43,9 @@ pub trait HandlerExt: HandlerFor + Sized { where F: Fn(Params, InheritedParams) -> Self::InheritedParams; fn with_call_remote(self) -> RemoteCaller; + fn with_about(self, message: M) -> WithAbout + where + M: IntoResettable; } impl + Sized> HandlerExt for T { @@ -93,6 +99,16 @@ impl + Sized> HandlerExt(self, message: M) -> WithAbout + where + M: IntoResettable, + { + WithAbout { + handler: self, + message, + } + } } #[derive(Debug, Clone)] @@ -162,7 +178,6 @@ where Context: crate::Context, H: HandlerTypes, { - const NO_CLI: bool = true; fn cli_command(&self) -> clap::Command { unimplemented!() } @@ -757,3 +772,96 @@ where ) } } + +#[derive(Debug, Clone)] +pub struct WithAbout { + handler: H, + message: M, +} +impl HandlerTypes for WithAbout +where + H: HandlerTypes, +{ + type Params = H::Params; + type InheritedParams = H::InheritedParams; + type Ok = H::Ok; + type Err = H::Err; +} +impl HandlerFor for WithAbout +where + Context: crate::Context, + H: HandlerFor, + M: Clone + Send + Sync + 'static, +{ + fn handle_sync( + &self, + HandlerArgs { + context, + parent_method, + method, + params, + inherited_params, + raw_params, + }: HandlerArgsFor, + ) -> Result { + self.handler.handle_sync(HandlerArgs { + context, + parent_method, + method, + params, + inherited_params, + raw_params, + }) + } + async fn handle_async( + &self, + HandlerArgs { + context, + parent_method, + method, + params, + inherited_params, + raw_params, + }: HandlerArgsFor, + ) -> Result { + self.handler + .handle_async(HandlerArgs { + context, + parent_method, + method, + params, + inherited_params, + raw_params, + }) + .await + } + fn metadata(&self, method: VecDeque<&'static str>) -> OrdMap<&'static str, Value> { + self.handler.metadata(method) + } + fn method_from_dots(&self, method: &str) -> Option> { + self.handler.method_from_dots(method) + } +} +impl CliBindings for WithAbout +where + Context: crate::Context, + H: CliBindings, + M: IntoResettable + Clone, +{ + fn cli_command(&self) -> clap::Command { + self.handler.cli_command().about(self.message.clone()) + } + fn cli_parse( + &self, + arg_matches: &clap::ArgMatches, + ) -> Result<(VecDeque<&'static str>, Value), clap::Error> { + self.handler.cli_parse(arg_matches) + } + fn cli_display( + &self, + handler: HandlerArgsFor, + result: Self::Ok, + ) -> Result<(), Self::Err> { + self.handler.cli_display(handler, result) + } +} diff --git a/tests/handler.rs b/tests/handler.rs index 9e89d0e..2d53202 100644 --- a/tests/handler.rs +++ b/tests/handler.rs @@ -11,7 +11,7 @@ use rpc_toolkit::{ ParentHandler, Server, }; use serde::{Deserialize, Serialize}; -use tokio::runtime::{Handle, Runtime}; +use tokio::runtime::Runtime; use tokio::sync::{Mutex, OnceCell}; use yajrc::RpcError; @@ -133,6 +133,7 @@ fn make_api() -> ParentHandler { }, ) .with_custom_display_fn(|_, a| Ok(println!("{a}"))) + .with_about("Testing") .with_call_remote::(), ) .subcommand( @@ -200,18 +201,10 @@ pub fn internal_error(e: impl Display) -> RpcError { #[test] fn test_cli() { make_cli() - .run( - ["test-cli", "hello", "me"] - .iter() - .map(|s| OsString::from(s)), - ) + .run(["test-cli", "hello", "me"].iter().map(OsString::from)) .unwrap(); make_cli() - .run( - ["test-cli", "fizz", "buzz"] - .iter() - .map(|s| OsString::from(s)), - ) + .run(["test-cli", "fizz", "buzz"].iter().map(OsString::from)) .unwrap(); } @@ -234,7 +227,7 @@ async fn test_server() { "foo", ] .iter() - .map(|s| OsString::from(s)), + .map(OsString::from), ) .unwrap(); make_cli() diff --git a/tests/test.rs b/tests/test.rs index 5ef6b81..dac9821 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -19,14 +19,14 @@ // impl Context for CliContext { // type Metadata = (); // } -// + // impl CliContextSocket for CliContext { // type Stream = UnixStream; // async fn connect(&self) -> std::io::Result { // UnixStream::connect(&self.0).await // } // } -// + // impl rpc_toolkit::CliContext for CliContext { // async fn call_remote( // &self, @@ -92,7 +92,7 @@ // println!("{}", res); // } // } -// + // impl AsyncCommand for Thing1 { // async fn implementation( // self,