mirror of
https://github.com/Start9Labs/rpc-toolkit.git
synced 2026-03-26 02:11:56 +00:00
Feat: Add with about (#2)
* FEat: Add with about * chore: Fix the tests
This commit is contained in:
@@ -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<Context: crate::Context>: HandlerFor<Context> + Sized {
|
||||
where
|
||||
F: Fn(Params, InheritedParams) -> Self::InheritedParams;
|
||||
fn with_call_remote<C>(self) -> RemoteCaller<C, Context, Self>;
|
||||
fn with_about<M>(self, message: M) -> WithAbout<M, Self>
|
||||
where
|
||||
M: IntoResettable<StyledStr>;
|
||||
}
|
||||
|
||||
impl<Context: crate::Context, T: HandlerFor<Context> + Sized> HandlerExt<Context> for T {
|
||||
@@ -93,6 +99,16 @@ impl<Context: crate::Context, T: HandlerFor<Context> + Sized> HandlerExt<Context
|
||||
handler: self,
|
||||
}
|
||||
}
|
||||
|
||||
fn with_about<M>(self, message: M) -> WithAbout<M, Self>
|
||||
where
|
||||
M: IntoResettable<StyledStr>,
|
||||
{
|
||||
WithAbout {
|
||||
handler: self,
|
||||
message,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
@@ -757,3 +773,96 @@ where
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct WithAbout<M, H> {
|
||||
handler: H,
|
||||
message: M,
|
||||
}
|
||||
impl<M, H> HandlerTypes for WithAbout<M, H>
|
||||
where
|
||||
H: HandlerTypes,
|
||||
{
|
||||
type Params = H::Params;
|
||||
type InheritedParams = H::InheritedParams;
|
||||
type Ok = H::Ok;
|
||||
type Err = H::Err;
|
||||
}
|
||||
impl<Context, M, H> HandlerFor<Context> for WithAbout<M, H>
|
||||
where
|
||||
Context: crate::Context,
|
||||
H: HandlerFor<Context>,
|
||||
M: Clone + Send + Sync + 'static,
|
||||
{
|
||||
fn handle_sync(
|
||||
&self,
|
||||
HandlerArgs {
|
||||
context,
|
||||
parent_method,
|
||||
method,
|
||||
params,
|
||||
inherited_params,
|
||||
raw_params,
|
||||
}: HandlerArgsFor<Context, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
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<Context, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
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<VecDeque<&'static str>> {
|
||||
self.handler.method_from_dots(method)
|
||||
}
|
||||
}
|
||||
impl<Context, M, H> CliBindings<Context> for WithAbout<M, H>
|
||||
where
|
||||
Context: crate::Context,
|
||||
H: CliBindings<Context>,
|
||||
M: IntoResettable<StyledStr> + 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<Context, Self>,
|
||||
result: Self::Ok,
|
||||
) -> Result<(), Self::Err> {
|
||||
self.handler.cli_display(handler, result)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<C: Context>() -> ParentHandler<C> {
|
||||
},
|
||||
)
|
||||
.with_custom_display_fn(|_, a| Ok(println!("{a}")))
|
||||
.with_about("Testing")
|
||||
.with_call_remote::<CliContext>(),
|
||||
)
|
||||
.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()
|
||||
@@ -246,7 +239,7 @@ async fn test_server() {
|
||||
"bar",
|
||||
]
|
||||
.iter()
|
||||
.map(|s| OsString::from(s)),
|
||||
.map(OsString::from),
|
||||
)
|
||||
.unwrap();
|
||||
shutdown.shutdown()
|
||||
|
||||
@@ -19,14 +19,14 @@
|
||||
// impl Context for CliContext {
|
||||
// type Metadata = ();
|
||||
// }
|
||||
//
|
||||
|
||||
// impl CliContextSocket for CliContext {
|
||||
// type Stream = UnixStream;
|
||||
// async fn connect(&self) -> std::io::Result<Self::Stream> {
|
||||
// UnixStream::connect(&self.0).await
|
||||
// }
|
||||
// }
|
||||
//
|
||||
|
||||
// impl rpc_toolkit::CliContext for CliContext {
|
||||
// async fn call_remote(
|
||||
// &self,
|
||||
@@ -92,7 +92,7 @@
|
||||
// println!("{}", res);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
|
||||
// impl AsyncCommand<ServerContext> for Thing1 {
|
||||
// async fn implementation(
|
||||
// self,
|
||||
|
||||
Reference in New Issue
Block a user