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::collections::VecDeque;
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
|
|
||||||
use clap::{CommandFactory, FromArgMatches};
|
use clap::{
|
||||||
|
builder::{IntoResettable, StyledStr},
|
||||||
|
CommandFactory, FromArgMatches,
|
||||||
|
};
|
||||||
use imbl_value::imbl::OrdMap;
|
use imbl_value::imbl::OrdMap;
|
||||||
use imbl_value::Value;
|
use imbl_value::Value;
|
||||||
use serde::de::DeserializeOwned;
|
use serde::de::DeserializeOwned;
|
||||||
@@ -40,6 +43,9 @@ pub trait HandlerExt<Context: crate::Context>: HandlerFor<Context> + Sized {
|
|||||||
where
|
where
|
||||||
F: Fn(Params, InheritedParams) -> Self::InheritedParams;
|
F: Fn(Params, InheritedParams) -> Self::InheritedParams;
|
||||||
fn with_call_remote<C>(self) -> RemoteCaller<C, Context, Self>;
|
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 {
|
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,
|
handler: self,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn with_about<M>(self, message: M) -> WithAbout<M, Self>
|
||||||
|
where
|
||||||
|
M: IntoResettable<StyledStr>,
|
||||||
|
{
|
||||||
|
WithAbout {
|
||||||
|
handler: self,
|
||||||
|
message,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[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,
|
ParentHandler, Server,
|
||||||
};
|
};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use tokio::runtime::{Handle, Runtime};
|
use tokio::runtime::Runtime;
|
||||||
use tokio::sync::{Mutex, OnceCell};
|
use tokio::sync::{Mutex, OnceCell};
|
||||||
use yajrc::RpcError;
|
use yajrc::RpcError;
|
||||||
|
|
||||||
@@ -133,6 +133,7 @@ fn make_api<C: Context>() -> ParentHandler<C> {
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
.with_custom_display_fn(|_, a| Ok(println!("{a}")))
|
.with_custom_display_fn(|_, a| Ok(println!("{a}")))
|
||||||
|
.with_about("Testing")
|
||||||
.with_call_remote::<CliContext>(),
|
.with_call_remote::<CliContext>(),
|
||||||
)
|
)
|
||||||
.subcommand(
|
.subcommand(
|
||||||
@@ -200,18 +201,10 @@ pub fn internal_error(e: impl Display) -> RpcError {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_cli() {
|
fn test_cli() {
|
||||||
make_cli()
|
make_cli()
|
||||||
.run(
|
.run(["test-cli", "hello", "me"].iter().map(OsString::from))
|
||||||
["test-cli", "hello", "me"]
|
|
||||||
.iter()
|
|
||||||
.map(|s| OsString::from(s)),
|
|
||||||
)
|
|
||||||
.unwrap();
|
.unwrap();
|
||||||
make_cli()
|
make_cli()
|
||||||
.run(
|
.run(["test-cli", "fizz", "buzz"].iter().map(OsString::from))
|
||||||
["test-cli", "fizz", "buzz"]
|
|
||||||
.iter()
|
|
||||||
.map(|s| OsString::from(s)),
|
|
||||||
)
|
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -234,7 +227,7 @@ async fn test_server() {
|
|||||||
"foo",
|
"foo",
|
||||||
]
|
]
|
||||||
.iter()
|
.iter()
|
||||||
.map(|s| OsString::from(s)),
|
.map(OsString::from),
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
make_cli()
|
make_cli()
|
||||||
@@ -246,7 +239,7 @@ async fn test_server() {
|
|||||||
"bar",
|
"bar",
|
||||||
]
|
]
|
||||||
.iter()
|
.iter()
|
||||||
.map(|s| OsString::from(s)),
|
.map(OsString::from),
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
shutdown.shutdown()
|
shutdown.shutdown()
|
||||||
|
|||||||
@@ -19,14 +19,14 @@
|
|||||||
// impl Context for CliContext {
|
// impl Context for CliContext {
|
||||||
// type Metadata = ();
|
// type Metadata = ();
|
||||||
// }
|
// }
|
||||||
//
|
|
||||||
// impl CliContextSocket for CliContext {
|
// impl CliContextSocket for CliContext {
|
||||||
// type Stream = UnixStream;
|
// type Stream = UnixStream;
|
||||||
// async fn connect(&self) -> std::io::Result<Self::Stream> {
|
// async fn connect(&self) -> std::io::Result<Self::Stream> {
|
||||||
// UnixStream::connect(&self.0).await
|
// UnixStream::connect(&self.0).await
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
//
|
|
||||||
// impl rpc_toolkit::CliContext for CliContext {
|
// impl rpc_toolkit::CliContext for CliContext {
|
||||||
// async fn call_remote(
|
// async fn call_remote(
|
||||||
// &self,
|
// &self,
|
||||||
@@ -92,7 +92,7 @@
|
|||||||
// println!("{}", res);
|
// println!("{}", res);
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
//
|
|
||||||
// impl AsyncCommand<ServerContext> for Thing1 {
|
// impl AsyncCommand<ServerContext> for Thing1 {
|
||||||
// async fn implementation(
|
// async fn implementation(
|
||||||
// self,
|
// self,
|
||||||
|
|||||||
Reference in New Issue
Block a user