mirror of
https://github.com/Start9Labs/rpc-toolkit.git
synced 2026-03-30 12:21:58 +00:00
separate CLI traits
This commit is contained in:
@@ -14,7 +14,7 @@ use yajrc::{Id, RpcError};
|
|||||||
use crate::util::{internal_error, parse_error, Flat, PhantomData};
|
use crate::util::{internal_error, parse_error, Flat, PhantomData};
|
||||||
use crate::{
|
use crate::{
|
||||||
AnyHandler, CliBindings, CliBindingsAny, DynHandler, HandleAny, HandleAnyArgs, HandleArgs,
|
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>;
|
type GenericRpcMethod<'a> = yajrc::GenericRpcMethod<&'a str, Value, Value>;
|
||||||
@@ -238,26 +238,17 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl<Context: CallRemote, RemoteHandler> CliBindings for CallRemoteHandler<Context, RemoteHandler>
|
impl<Context: CallRemote, RemoteHandler> PrintCliResult
|
||||||
|
for CallRemoteHandler<Context, RemoteHandler>
|
||||||
where
|
where
|
||||||
RemoteHandler: Handler + CliBindings<Context = Context>,
|
RemoteHandler: PrintCliResult<Context = Context>,
|
||||||
RemoteHandler::Params: Serialize,
|
RemoteHandler::Params: Serialize,
|
||||||
RemoteHandler::InheritedParams: Serialize,
|
RemoteHandler::InheritedParams: Serialize,
|
||||||
RemoteHandler::Ok: DeserializeOwned,
|
RemoteHandler::Ok: DeserializeOwned,
|
||||||
RemoteHandler::Err: From<RpcError>,
|
RemoteHandler::Err: From<RpcError>,
|
||||||
{
|
{
|
||||||
type Context = Context;
|
type Context = Context;
|
||||||
fn cli_command(&self, ctx_ty: TypeId) -> clap::Command {
|
fn print(
|
||||||
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(
|
|
||||||
&self,
|
&self,
|
||||||
HandleArgs {
|
HandleArgs {
|
||||||
context,
|
context,
|
||||||
@@ -266,10 +257,10 @@ where
|
|||||||
params,
|
params,
|
||||||
inherited_params,
|
inherited_params,
|
||||||
raw_params,
|
raw_params,
|
||||||
}: HandleArgs<Context, Self>,
|
}: HandleArgs<Self::Context, Self>,
|
||||||
result: Self::Ok,
|
result: Self::Ok,
|
||||||
) -> Result<(), Self::Err> {
|
) -> Result<(), Self::Err> {
|
||||||
self.handler.cli_display(
|
self.handler.print(
|
||||||
HandleArgs {
|
HandleArgs {
|
||||||
context,
|
context,
|
||||||
parent_method,
|
parent_method,
|
||||||
|
|||||||
@@ -191,33 +191,13 @@ where
|
|||||||
self.0.method_from_dots(method, ctx_ty)
|
self.0.method_from_dots(method, ctx_ty)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl<H> CliBindings for NoDisplay<H>
|
impl<H> PrintCliResult for NoDisplay<H>
|
||||||
where
|
where
|
||||||
H: HandlerTypes,
|
H: HandlerTypes,
|
||||||
H::Params: FromArgMatches + CommandFactory + Serialize,
|
H::Params: FromArgMatches + CommandFactory + Serialize,
|
||||||
{
|
{
|
||||||
type Context = AnyContext;
|
type Context = AnyContext;
|
||||||
fn cli_command(&self, _: TypeId) -> clap::Command {
|
fn print(&self, _: HandleArgs<Self::Context, Self>, _: Self::Ok) -> Result<(), Self::Err> {
|
||||||
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::Context, Self>,
|
|
||||||
_: Self::Ok,
|
|
||||||
) -> Result<(), Self::Err> {
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -299,10 +279,9 @@ where
|
|||||||
self.handler.method_from_dots(method, ctx_ty)
|
self.handler.method_from_dots(method, ctx_ty)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl<P, H> CliBindings for CustomDisplay<P, H>
|
impl<P, H> PrintCliResult for CustomDisplay<P, H>
|
||||||
where
|
where
|
||||||
H: HandlerTypes,
|
H: HandlerTypes,
|
||||||
H::Params: FromArgMatches + CommandFactory + Serialize,
|
|
||||||
P: PrintCliResult<
|
P: PrintCliResult<
|
||||||
Params = H::Params,
|
Params = H::Params,
|
||||||
InheritedParams = H::InheritedParams,
|
InheritedParams = H::InheritedParams,
|
||||||
@@ -314,23 +293,7 @@ where
|
|||||||
+ 'static,
|
+ 'static,
|
||||||
{
|
{
|
||||||
type Context = P::Context;
|
type Context = P::Context;
|
||||||
fn cli_command(&self, _: TypeId) -> clap::Command {
|
fn print(
|
||||||
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,
|
&self,
|
||||||
HandleArgs {
|
HandleArgs {
|
||||||
context,
|
context,
|
||||||
@@ -451,31 +414,14 @@ where
|
|||||||
self.handler.method_from_dots(method, ctx_ty)
|
self.handler.method_from_dots(method, ctx_ty)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl<Context, F, H> CliBindings for CustomDisplayFn<Context, F, H>
|
impl<Context, F, H> PrintCliResult for CustomDisplayFn<Context, F, H>
|
||||||
where
|
where
|
||||||
Context: IntoContext,
|
Context: IntoContext,
|
||||||
H: HandlerTypes,
|
H: HandlerTypes,
|
||||||
H::Params: FromArgMatches + CommandFactory + Serialize,
|
|
||||||
F: Fn(HandleArgs<Context, H>, H::Ok) -> Result<(), H::Err> + Send + Sync + Clone + 'static,
|
F: Fn(HandleArgs<Context, H>, H::Ok) -> Result<(), H::Err> + Send + Sync + Clone + 'static,
|
||||||
{
|
{
|
||||||
type Context = Context;
|
type Context = Context;
|
||||||
fn cli_command(&self, _: TypeId) -> clap::Command {
|
fn print(
|
||||||
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,
|
&self,
|
||||||
HandleArgs {
|
HandleArgs {
|
||||||
context,
|
context,
|
||||||
@@ -594,22 +540,13 @@ where
|
|||||||
self.handler.method_from_dots(method, ctx_ty)
|
self.handler.method_from_dots(method, ctx_ty)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl<Context, H> CliBindings for RemoteCli<Context, H>
|
impl<Context, H> PrintCliResult for RemoteCli<Context, H>
|
||||||
where
|
where
|
||||||
H: CliBindings,
|
Context: IntoContext,
|
||||||
|
H: PrintCliResult,
|
||||||
{
|
{
|
||||||
type Context = H::Context;
|
type Context = H::Context;
|
||||||
fn cli_command(&self, ctx_ty: TypeId) -> clap::Command {
|
fn print(
|
||||||
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(
|
|
||||||
&self,
|
&self,
|
||||||
HandleArgs {
|
HandleArgs {
|
||||||
context,
|
context,
|
||||||
@@ -621,7 +558,7 @@ where
|
|||||||
}: HandleArgs<Self::Context, Self>,
|
}: HandleArgs<Self::Context, Self>,
|
||||||
result: Self::Ok,
|
result: Self::Ok,
|
||||||
) -> Result<(), Self::Err> {
|
) -> Result<(), Self::Err> {
|
||||||
self.handler.cli_display(
|
self.handler.print(
|
||||||
HandleArgs {
|
HandleArgs {
|
||||||
context,
|
context,
|
||||||
parent_method,
|
parent_method,
|
||||||
|
|||||||
@@ -451,101 +451,3 @@ where
|
|||||||
self.metadata.clone()
|
self.metadata.clone()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F, T, E, Args> CliBindings for FromFn<F, T, E, Args>
|
|
||||||
where
|
|
||||||
Self: HandlerTypes,
|
|
||||||
Self::Params: FromArgMatches + CommandFactory + Serialize,
|
|
||||||
Self: PrintCliResult,
|
|
||||||
{
|
|
||||||
type Context = <Self as PrintCliResult>::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<Self::Context, Self>,
|
|
||||||
result: Self::Ok,
|
|
||||||
) -> Result<(), Self::Err> {
|
|
||||||
self.print(
|
|
||||||
HandleArgs {
|
|
||||||
context,
|
|
||||||
parent_method,
|
|
||||||
method,
|
|
||||||
params,
|
|
||||||
inherited_params,
|
|
||||||
raw_params,
|
|
||||||
},
|
|
||||||
result,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<F, Fut, T, E, Args> CliBindings for FromFnAsync<F, Fut, T, E, Args>
|
|
||||||
where
|
|
||||||
Self: HandlerTypes,
|
|
||||||
Self::Params: FromArgMatches + CommandFactory + Serialize,
|
|
||||||
Self: PrintCliResult,
|
|
||||||
{
|
|
||||||
type Context = <Self as PrintCliResult>::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<Self::Context, Self>,
|
|
||||||
result: Self::Ok,
|
|
||||||
) -> Result<(), Self::Err> {
|
|
||||||
self.print(
|
|
||||||
HandleArgs {
|
|
||||||
context,
|
|
||||||
parent_method,
|
|
||||||
method,
|
|
||||||
params,
|
|
||||||
inherited_params,
|
|
||||||
raw_params,
|
|
||||||
},
|
|
||||||
result,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ use std::collections::VecDeque;
|
|||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
use std::sync::Arc;
|
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::imbl::{OrdMap, OrdSet};
|
||||||
use imbl_value::Value;
|
use imbl_value::Value;
|
||||||
use serde::de::DeserializeOwned;
|
use serde::de::DeserializeOwned;
|
||||||
@@ -119,6 +119,55 @@ pub trait PrintCliResult: HandlerTypes {
|
|||||||
) -> Result<(), Self::Err>;
|
) -> Result<(), Self::Err>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T> 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<Self::Context, Self>,
|
||||||
|
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 {}
|
pub(crate) trait HandleAnyWithCli: HandleAny + CliBindingsAny {}
|
||||||
impl<T: HandleAny + CliBindingsAny> HandleAnyWithCli for T {}
|
impl<T: HandleAny + CliBindingsAny> HandleAnyWithCli for T {}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user