add no_display adapter

This commit is contained in:
Aiden McClelland
2024-01-02 13:14:32 -07:00
parent 3748c4bf89
commit 4bcaf67a03

View File

@@ -13,12 +13,13 @@ use yajrc::RpcError;
use crate::util::{internal_error, parse_error, Flat}; use crate::util::{internal_error, parse_error, Flat};
use crate::{ use crate::{
iter_from_ctx_and_handler, AnyHandler, CallRemote, CliBindings, DynHandler, EitherContext, iter_from_ctx_and_handler, AnyContext, AnyHandler, CallRemote, CliBindings, DynHandler,
HandleArgs, Handler, HandlerTypes, IntoContext, IntoHandlers, PrintCliResult, EitherContext, HandleArgs, Handler, HandlerTypes, IntoContext, IntoHandlers, PrintCliResult,
}; };
pub trait HandlerExt: HandlerTypes + Sized { pub trait HandlerExt: Handler + Sized {
fn no_cli(self) -> NoCli<Self>; fn no_cli(self) -> NoCli<Self>;
fn no_display(self) -> NoDisplay<Self>;
fn with_custom_display<P>(self, display: P) -> CustomDisplay<P, Self> fn with_custom_display<P>(self, display: P) -> CustomDisplay<P, Self>
where where
P: PrintCliResult< P: PrintCliResult<
@@ -42,10 +43,13 @@ pub trait HandlerExt: HandlerTypes + Sized {
fn with_remote_cli<Context>(self) -> RemoteCli<Context, Self>; fn with_remote_cli<Context>(self) -> RemoteCli<Context, Self>;
} }
impl<T: HandlerTypes + Sized> HandlerExt for T { impl<T: Handler + Sized> HandlerExt for T {
fn no_cli(self) -> NoCli<Self> { fn no_cli(self) -> NoCli<Self> {
NoCli(self) NoCli(self)
} }
fn no_display(self) -> NoDisplay<Self> {
NoDisplay(self)
}
fn with_custom_display<P>(self, display: P) -> CustomDisplay<P, Self> fn with_custom_display<P>(self, display: P) -> CustomDisplay<P, Self>
where where
P: PrintCliResult< P: PrintCliResult<
@@ -118,6 +122,107 @@ where
} }
} }
#[derive(Debug, Clone)]
pub struct NoDisplay<H>(pub H);
impl<H: HandlerTypes> HandlerTypes for NoDisplay<H> {
type Params = H::Params;
type InheritedParams = H::InheritedParams;
type Ok = H::Ok;
type Err = H::Err;
}
#[async_trait::async_trait]
impl<H> Handler for NoDisplay<H>
where
H: Handler,
{
type Context = H::Context;
fn handle_sync(
&self,
HandleArgs {
context,
parent_method,
method,
params,
inherited_params,
raw_params,
}: HandleArgs<Self::Context, Self>,
) -> Result<Self::Ok, Self::Err> {
self.0.handle_sync(HandleArgs {
context,
parent_method,
method,
params,
inherited_params,
raw_params,
})
}
async fn handle_async(
&self,
HandleArgs {
context,
parent_method,
method,
params,
inherited_params,
raw_params,
}: HandleArgs<Self::Context, Self>,
) -> Result<Self::Ok, Self::Err> {
self.0
.handle_async(HandleArgs {
context,
parent_method,
method,
params,
inherited_params,
raw_params,
})
.await
}
fn metadata(
&self,
method: VecDeque<&'static str>,
ctx_ty: TypeId,
) -> OrdMap<&'static str, Value> {
self.0.metadata(method, ctx_ty)
}
fn contexts(&self) -> Option<OrdSet<TypeId>> {
self.0.contexts()
}
fn method_from_dots(&self, method: &str, ctx_ty: TypeId) -> Option<VecDeque<&'static str>> {
self.0.method_from_dots(method, ctx_ty)
}
}
impl<H> CliBindings 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> {
Ok(())
}
}
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct CustomDisplay<P, H> { pub struct CustomDisplay<P, H> {
print: P, print: P,