separate CLI traits

This commit is contained in:
Aiden McClelland
2024-01-05 14:27:15 -07:00
parent 9f7e731fd1
commit 543ce4f2c0
4 changed files with 68 additions and 189 deletions

View File

@@ -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<Context: CallRemote, RemoteHandler> CliBindings for CallRemoteHandler<Context, RemoteHandler>
impl<Context: CallRemote, RemoteHandler> PrintCliResult
for CallRemoteHandler<Context, RemoteHandler>
where
RemoteHandler: Handler + CliBindings<Context = Context>,
RemoteHandler: PrintCliResult<Context = Context>,
RemoteHandler::Params: Serialize,
RemoteHandler::InheritedParams: Serialize,
RemoteHandler::Ok: DeserializeOwned,
RemoteHandler::Err: From<RpcError>,
{
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<Context, Self>,
}: HandleArgs<Self::Context, Self>,
result: Self::Ok,
) -> Result<(), Self::Err> {
self.handler.cli_display(
self.handler.print(
HandleArgs {
context,
parent_method,

View File

@@ -191,33 +191,13 @@ where
self.0.method_from_dots(method, ctx_ty)
}
}
impl<H> CliBindings for NoDisplay<H>
impl<H> PrintCliResult for NoDisplay<H>
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::Context, Self>,
_: Self::Ok,
) -> Result<(), Self::Err> {
fn print(&self, _: HandleArgs<Self::Context, Self>, _: Self::Ok) -> Result<(), Self::Err> {
Ok(())
}
}
@@ -299,10 +279,9 @@ where
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
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<Context, F, H> CliBindings for CustomDisplayFn<Context, F, H>
impl<Context, F, H> PrintCliResult for CustomDisplayFn<Context, F, H>
where
Context: IntoContext,
H: HandlerTypes,
H::Params: FromArgMatches + CommandFactory + Serialize,
F: Fn(HandleArgs<Context, H>, 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<Context, H> CliBindings for RemoteCli<Context, H>
impl<Context, H> PrintCliResult for RemoteCli<Context, H>
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<Self::Context, Self>,
result: Self::Ok,
) -> Result<(), Self::Err> {
self.handler.cli_display(
self.handler.print(
HandleArgs {
context,
parent_method,

View File

@@ -451,101 +451,3 @@ where
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,
)
}
}

View File

@@ -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<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 {}
impl<T: HandleAny + CliBindingsAny> HandleAnyWithCli for T {}