mirror of
https://github.com/Start9Labs/rpc-toolkit.git
synced 2026-03-26 02:11:56 +00:00
wip
This commit is contained in:
48
src/cli.rs
48
src/cli.rs
@@ -4,7 +4,7 @@ use std::ffi::OsString;
|
||||
|
||||
use clap::{CommandFactory, FromArgMatches};
|
||||
use futures::Future;
|
||||
use imbl_value::{InOMap, Value};
|
||||
use imbl_value::Value;
|
||||
use reqwest::header::{ACCEPT, CONTENT_LENGTH, CONTENT_TYPE};
|
||||
use reqwest::{Client, Method};
|
||||
use serde::de::DeserializeOwned;
|
||||
@@ -15,7 +15,7 @@ use yajrc::{Id, RpcError};
|
||||
|
||||
use crate::util::{internal_error, invalid_params, parse_error, without, Flat, PhantomData};
|
||||
use crate::{
|
||||
AnyHandler, CliBindingsAny, DynHandler, Empty, HandleAny, HandleAnyArgs, Handler, HandlerArgs,
|
||||
AnyHandler, CliBindingsAny, Empty, HandleAny, HandleAnyArgs, Handler, HandlerArgs,
|
||||
HandlerArgsFor, HandlerTypes, IntoContext, Name, ParentHandler, PrintCliResult,
|
||||
};
|
||||
|
||||
@@ -45,17 +45,17 @@ impl<Context: crate::Context + Clone, Config: CommandFactory + FromArgMatches>
|
||||
let ctx_ty = TypeId::of::<Context>();
|
||||
let mut cmd = Config::command();
|
||||
for (name, handlers) in &self.root_handler.subcommands.0 {
|
||||
if let (Name(Some(name)), Some(DynHandler::WithCli(handler))) = (
|
||||
if let (Name(Some(name)), Some(cli)) = (
|
||||
name,
|
||||
if let Some(handler) = handlers.get(&Some(ctx_ty)) {
|
||||
Some(handler)
|
||||
handler.cli()
|
||||
} else if let Some(handler) = handlers.get(&None) {
|
||||
Some(handler)
|
||||
handler.cli()
|
||||
} else {
|
||||
None
|
||||
},
|
||||
) {
|
||||
cmd = cmd.subcommand(handler.cli_command(ctx_ty).name(name));
|
||||
cmd = cmd.subcommand(cli.cli_command(ctx_ty).name(name));
|
||||
}
|
||||
}
|
||||
let matches = cmd.get_matches_from(args);
|
||||
@@ -172,11 +172,13 @@ pub async fn call_remote_socket(
|
||||
.result
|
||||
}
|
||||
|
||||
pub struct CallRemoteHandler<Context, RemoteHandler, Extra = Empty> {
|
||||
_phantom: PhantomData<(Context, Extra)>,
|
||||
pub struct CallRemoteHandler<Context, RemoteContext, RemoteHandler, Extra = Empty> {
|
||||
_phantom: PhantomData<(Context, RemoteContext, Extra)>,
|
||||
handler: RemoteHandler,
|
||||
}
|
||||
impl<Context, RemoteHandler, Extra> CallRemoteHandler<Context, RemoteHandler, Extra> {
|
||||
impl<Context, RemoteContext, RemoteHandler, Extra>
|
||||
CallRemoteHandler<Context, RemoteContext, RemoteHandler, Extra>
|
||||
{
|
||||
pub fn new(handler: RemoteHandler) -> Self {
|
||||
Self {
|
||||
_phantom: PhantomData::new(),
|
||||
@@ -184,8 +186,8 @@ impl<Context, RemoteHandler, Extra> CallRemoteHandler<Context, RemoteHandler, Ex
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<Context, RemoteHandler: Clone, Extra> Clone
|
||||
for CallRemoteHandler<Context, RemoteHandler, Extra>
|
||||
impl<Context, RemoteContext, RemoteHandler: Clone, Extra> Clone
|
||||
for CallRemoteHandler<Context, RemoteContext, RemoteHandler, Extra>
|
||||
{
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
@@ -202,8 +204,8 @@ impl<Context, RemoteHandler, Extra> std::fmt::Debug
|
||||
}
|
||||
}
|
||||
|
||||
impl<Context, RemoteHandler, Extra> HandlerTypes
|
||||
for CallRemoteHandler<Context, RemoteHandler, Extra>
|
||||
impl<Context, RemoteContext, RemoteHandler, Extra> HandlerTypes
|
||||
for CallRemoteHandler<Context, RemoteContext, RemoteHandler, Extra>
|
||||
where
|
||||
RemoteHandler: HandlerTypes,
|
||||
RemoteHandler::Params: Serialize,
|
||||
@@ -218,17 +220,18 @@ where
|
||||
type Err = RemoteHandler::Err;
|
||||
}
|
||||
|
||||
impl<Context, RemoteHandler, Extra> Handler for CallRemoteHandler<Context, RemoteHandler, Extra>
|
||||
impl<Context, RemoteContext, RemoteHandler, Extra> Handler<Context>
|
||||
for CallRemoteHandler<Context, RemoteContext, RemoteHandler, Extra>
|
||||
where
|
||||
Context: CallRemote<RemoteHandler::Context, Extra>,
|
||||
RemoteHandler: Handler,
|
||||
Context: CallRemote<RemoteContext, Extra>,
|
||||
RemoteContext: IntoContext,
|
||||
RemoteHandler: Handler<RemoteContext>,
|
||||
RemoteHandler::Params: Serialize,
|
||||
RemoteHandler::InheritedParams: Serialize,
|
||||
RemoteHandler::Ok: DeserializeOwned,
|
||||
RemoteHandler::Err: From<RpcError>,
|
||||
Extra: Serialize + Send + Sync + 'static,
|
||||
{
|
||||
type Context = Context;
|
||||
async fn handle_async(
|
||||
&self,
|
||||
handle_args: HandlerArgsFor<Context, Self>,
|
||||
@@ -255,18 +258,17 @@ where
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<Context, RemoteHandler, Extra> PrintCliResult
|
||||
for CallRemoteHandler<Context, RemoteHandler, Extra>
|
||||
impl<Context, RemoteContext, RemoteHandler, Extra> PrintCliResult<Context>
|
||||
for CallRemoteHandler<Context, RemoteContext, RemoteHandler, Extra>
|
||||
where
|
||||
Context: CallRemote<RemoteHandler::Context>,
|
||||
RemoteHandler: PrintCliResult<Context = Context>,
|
||||
Context: CallRemote<RemoteContext>,
|
||||
RemoteHandler: PrintCliResult<Context>,
|
||||
RemoteHandler::Params: Serialize,
|
||||
RemoteHandler::InheritedParams: Serialize,
|
||||
RemoteHandler::Ok: DeserializeOwned,
|
||||
RemoteHandler::Err: From<RpcError>,
|
||||
Extra: Send + Sync + 'static,
|
||||
{
|
||||
type Context = Context;
|
||||
fn print(
|
||||
&self,
|
||||
HandlerArgs {
|
||||
@@ -276,7 +278,7 @@ where
|
||||
params,
|
||||
inherited_params,
|
||||
raw_params,
|
||||
}: HandlerArgsFor<Self::Context, Self>,
|
||||
}: HandlerArgsFor<Context, Self>,
|
||||
result: Self::Ok,
|
||||
) -> Result<(), Self::Err> {
|
||||
self.handler.print(
|
||||
|
||||
@@ -84,6 +84,8 @@ impl<C1: IntoContext, C2: IntoContext> IntoContext for EitherContext<C1, C2> {
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<C1, C2> From<C1> for EitherContext<C1, C2> {}
|
||||
impl<C1, C2> From<C2> for EitherContext<C1, C2> {}
|
||||
|
||||
pub struct AnyContext(Box<dyn Context>);
|
||||
impl AnyContext {
|
||||
@@ -114,9 +116,25 @@ impl IntoContext for AnyContext {
|
||||
}
|
||||
}
|
||||
|
||||
mod sealed {
|
||||
pub(crate) trait Sealed {}
|
||||
impl<C: super::Context> Sealed for C {}
|
||||
impl<C1: super::IntoContext, C2: super::IntoContext> Sealed for super::EitherContext<C1, C2> {}
|
||||
impl Sealed for super::AnyContext {}
|
||||
pub(crate) mod sealed {
|
||||
use std::any::TypeId;
|
||||
|
||||
pub(crate) trait Sealed {
|
||||
fn contains_type_id(id: TypeId) -> bool;
|
||||
}
|
||||
impl<C: super::Context> Sealed for C {
|
||||
fn contains_type_id(id: TypeId) -> bool {
|
||||
id == TypeId::of::<C>()
|
||||
}
|
||||
}
|
||||
impl<C1: super::IntoContext, C2: super::IntoContext> Sealed for super::EitherContext<C1, C2> {
|
||||
fn contains_type_id(id: TypeId) -> bool {
|
||||
C1::contains_type_id(id) || C2::contains_type_id(id)
|
||||
}
|
||||
}
|
||||
impl Sealed for super::AnyContext {
|
||||
fn contains_type_id(_: TypeId) -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
use std::any::TypeId;
|
||||
use std::collections::VecDeque;
|
||||
use std::fmt::Debug;
|
||||
use std::sync::Arc;
|
||||
|
||||
use clap::{CommandFactory, FromArgMatches};
|
||||
use imbl_value::imbl::{OrdMap, OrdSet};
|
||||
@@ -10,25 +9,25 @@ use serde::de::DeserializeOwned;
|
||||
use serde::Serialize;
|
||||
use yajrc::RpcError;
|
||||
|
||||
use crate::util::{internal_error, invalid_params, without, Flat, PhantomData};
|
||||
use crate::util::{internal_error, Flat, PhantomData};
|
||||
use crate::{
|
||||
iter_from_ctx_and_handler, AnyContext, AnyHandler, CallRemote, CliBindings, DynHandler,
|
||||
EitherContext, Empty, Handler, HandlerArgs, HandlerArgsFor, HandlerTypes, IntoContext,
|
||||
IntoHandlers, OrEmpty, PrintCliResult,
|
||||
AnyContext, CallRemote, CliBindings, EitherContext, Empty, Handler, HandlerArgs,
|
||||
HandlerArgsFor, HandlerTypes, IntoContext, PrintCliResult,
|
||||
};
|
||||
|
||||
pub trait HandlerExt: Handler + Sized {
|
||||
pub trait HandlerExt<Context: IntoContext>: Handler<Context> + Sized {
|
||||
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<C: IntoContext, P>(self, display: P) -> CustomDisplay<P, Self>
|
||||
where
|
||||
P: PrintCliResult<
|
||||
C,
|
||||
Params = Self::Params,
|
||||
InheritedParams = Self::InheritedParams,
|
||||
Ok = Self::Ok,
|
||||
Err = Self::Err,
|
||||
>;
|
||||
fn with_custom_display_fn<Context: IntoContext, F>(
|
||||
fn with_custom_display_fn<C: IntoContext, F>(
|
||||
self,
|
||||
display: F,
|
||||
) -> CustomDisplayFn<F, Self, Context>
|
||||
@@ -40,19 +39,20 @@ pub trait HandlerExt: Handler + Sized {
|
||||
) -> InheritanceHandler<Params, InheritedParams, Self, F>
|
||||
where
|
||||
F: Fn(Params, InheritedParams) -> Self::InheritedParams;
|
||||
fn with_call_remote<Context>(self) -> RemoteCaller<Context, Self>;
|
||||
fn with_call_remote<C>(self) -> RemoteCaller<C, Self>;
|
||||
}
|
||||
|
||||
impl<T: Handler + Sized> HandlerExt for T {
|
||||
impl<Context: IntoContext, T: Handler<Context> + Sized> HandlerExt<Context> for T {
|
||||
fn no_cli(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<C: IntoContext, P>(self, display: P) -> CustomDisplay<P, Self>
|
||||
where
|
||||
P: PrintCliResult<
|
||||
C,
|
||||
Params = Self::Params,
|
||||
InheritedParams = Self::InheritedParams,
|
||||
Ok = Self::Ok,
|
||||
@@ -64,7 +64,7 @@ impl<T: Handler + Sized> HandlerExt for T {
|
||||
handler: self,
|
||||
}
|
||||
}
|
||||
fn with_custom_display_fn<Context: IntoContext, F>(
|
||||
fn with_custom_display_fn<C: IntoContext, F>(
|
||||
self,
|
||||
display: F,
|
||||
) -> CustomDisplayFn<F, Self, Context>
|
||||
@@ -90,7 +90,7 @@ impl<T: Handler + Sized> HandlerExt for T {
|
||||
inherit: f,
|
||||
}
|
||||
}
|
||||
fn with_call_remote<Context>(self) -> RemoteCaller<Context, Self> {
|
||||
fn with_call_remote<C>(self) -> RemoteCaller<C, Self> {
|
||||
RemoteCaller {
|
||||
_phantom: PhantomData::new(),
|
||||
handler: self,
|
||||
@@ -106,25 +106,7 @@ impl<H: HandlerTypes> HandlerTypes for NoCli<H> {
|
||||
type Ok = H::Ok;
|
||||
type Err = H::Err;
|
||||
}
|
||||
impl<H, A, B> IntoHandlers<Flat<A, B>> for NoCli<H>
|
||||
where
|
||||
H: Handler,
|
||||
H::Params: DeserializeOwned,
|
||||
H::InheritedParams: OrEmpty<Flat<A, B>>,
|
||||
H::Ok: Serialize + DeserializeOwned,
|
||||
RpcError: From<H::Err>,
|
||||
A: Send + Sync + 'static,
|
||||
B: Send + Sync + 'static,
|
||||
{
|
||||
fn into_handlers(self) -> impl IntoIterator<Item = (Option<TypeId>, DynHandler<Flat<A, B>>)> {
|
||||
iter_from_ctx_and_handler(
|
||||
self.0.contexts(),
|
||||
DynHandler::WithoutCli(Arc::new(AnyHandler::new(
|
||||
self.0.with_inherited(|a, b| OrEmpty::from_t(Flat(a, b))),
|
||||
))),
|
||||
)
|
||||
}
|
||||
}
|
||||
// TODO: implement Handler
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct NoDisplay<H>(pub H);
|
||||
@@ -135,11 +117,11 @@ impl<H: HandlerTypes> HandlerTypes for NoDisplay<H> {
|
||||
type Err = H::Err;
|
||||
}
|
||||
|
||||
impl<H> Handler for NoDisplay<H>
|
||||
impl<Context, H> Handler<Context> for NoDisplay<H>
|
||||
where
|
||||
H: Handler,
|
||||
Context: IntoContext,
|
||||
H: Handler<Context>,
|
||||
{
|
||||
type Context = H::Context;
|
||||
fn handle_sync(
|
||||
&self,
|
||||
HandlerArgs {
|
||||
@@ -149,7 +131,7 @@ where
|
||||
params,
|
||||
inherited_params,
|
||||
raw_params,
|
||||
}: HandlerArgsFor<Self::Context, Self>,
|
||||
}: HandlerArgsFor<Context, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
self.0.handle_sync(HandlerArgs {
|
||||
context,
|
||||
@@ -169,7 +151,7 @@ where
|
||||
params,
|
||||
inherited_params,
|
||||
raw_params,
|
||||
}: HandlerArgsFor<Self::Context, Self>,
|
||||
}: HandlerArgsFor<Context, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
self.0
|
||||
.handle_async(HandlerArgs {
|
||||
@@ -196,13 +178,13 @@ where
|
||||
self.0.method_from_dots(method, ctx_ty)
|
||||
}
|
||||
}
|
||||
impl<H> PrintCliResult for NoDisplay<H>
|
||||
impl<Context, H> PrintCliResult<Context> for NoDisplay<H>
|
||||
where
|
||||
Context: IntoContext,
|
||||
H: HandlerTypes,
|
||||
H::Params: FromArgMatches + CommandFactory + Serialize,
|
||||
{
|
||||
type Context = AnyContext;
|
||||
fn print(&self, _: HandlerArgsFor<Self::Context, Self>, _: Self::Ok) -> Result<(), Self::Err> {
|
||||
fn print(&self, _: HandlerArgsFor<Context, Self>, _: Self::Ok) -> Result<(), Self::Err> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -222,12 +204,12 @@ where
|
||||
type Err = H::Err;
|
||||
}
|
||||
|
||||
impl<P, H> Handler for CustomDisplay<P, H>
|
||||
impl<Context, P, H> Handler<Context> for CustomDisplay<P, H>
|
||||
where
|
||||
H: Handler,
|
||||
Context: IntoContext,
|
||||
H: Handler<Context>,
|
||||
P: Send + Sync + Clone + 'static,
|
||||
{
|
||||
type Context = H::Context;
|
||||
fn handle_sync(
|
||||
&self,
|
||||
HandlerArgs {
|
||||
@@ -237,7 +219,7 @@ where
|
||||
params,
|
||||
inherited_params,
|
||||
raw_params,
|
||||
}: HandlerArgsFor<Self::Context, Self>,
|
||||
}: HandlerArgsFor<Context, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
self.handler.handle_sync(HandlerArgs {
|
||||
context,
|
||||
@@ -257,7 +239,7 @@ where
|
||||
params,
|
||||
inherited_params,
|
||||
raw_params,
|
||||
}: HandlerArgsFor<Self::Context, Self>,
|
||||
}: HandlerArgsFor<Context, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
self.handler
|
||||
.handle_async(HandlerArgs {
|
||||
@@ -284,10 +266,12 @@ where
|
||||
self.handler.method_from_dots(method, ctx_ty)
|
||||
}
|
||||
}
|
||||
impl<P, H> PrintCliResult for CustomDisplay<P, H>
|
||||
impl<Context, P, H> PrintCliResult<Context> for CustomDisplay<P, H>
|
||||
where
|
||||
Context: IntoContext,
|
||||
H: HandlerTypes,
|
||||
P: PrintCliResult<
|
||||
Context,
|
||||
Params = H::Params,
|
||||
InheritedParams = H::InheritedParams,
|
||||
Ok = H::Ok,
|
||||
@@ -297,7 +281,6 @@ where
|
||||
+ Clone
|
||||
+ 'static,
|
||||
{
|
||||
type Context = P::Context;
|
||||
fn print(
|
||||
&self,
|
||||
HandlerArgs {
|
||||
@@ -307,7 +290,7 @@ where
|
||||
params,
|
||||
inherited_params,
|
||||
raw_params,
|
||||
}: HandlerArgsFor<Self::Context, Self>,
|
||||
}: HandlerArgsFor<Context, Self>,
|
||||
result: Self::Ok,
|
||||
) -> Result<(), Self::Err> {
|
||||
self.print.print(
|
||||
@@ -356,13 +339,13 @@ where
|
||||
type Err = H::Err;
|
||||
}
|
||||
|
||||
impl<F, H, Context> Handler for CustomDisplayFn<F, H, Context>
|
||||
impl<Context, F, H, C> Handler<Context> for CustomDisplayFn<F, H, C>
|
||||
where
|
||||
Context: Send + Sync + 'static,
|
||||
H: Handler,
|
||||
Context: IntoContext,
|
||||
C: 'static,
|
||||
H: Handler<Context>,
|
||||
F: Send + Sync + Clone + 'static,
|
||||
{
|
||||
type Context = H::Context;
|
||||
fn handle_sync(
|
||||
&self,
|
||||
HandlerArgs {
|
||||
@@ -372,7 +355,7 @@ where
|
||||
params,
|
||||
inherited_params,
|
||||
raw_params,
|
||||
}: HandlerArgsFor<Self::Context, Self>,
|
||||
}: HandlerArgsFor<Context, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
self.handler.handle_sync(HandlerArgs {
|
||||
context,
|
||||
@@ -392,7 +375,7 @@ where
|
||||
params,
|
||||
inherited_params,
|
||||
raw_params,
|
||||
}: HandlerArgsFor<Self::Context, Self>,
|
||||
}: HandlerArgsFor<Context, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
self.handler
|
||||
.handle_async(HandlerArgs {
|
||||
@@ -419,13 +402,12 @@ where
|
||||
self.handler.method_from_dots(method, ctx_ty)
|
||||
}
|
||||
}
|
||||
impl<F, H, Context> PrintCliResult for CustomDisplayFn<F, H, Context>
|
||||
impl<F, H, Context> PrintCliResult<Context> for CustomDisplayFn<F, H, Context>
|
||||
where
|
||||
Context: IntoContext,
|
||||
H: HandlerTypes,
|
||||
F: Fn(HandlerArgsFor<Context, H>, H::Ok) -> Result<(), H::Err> + Send + Sync + Clone + 'static,
|
||||
{
|
||||
type Context = Context;
|
||||
fn print(
|
||||
&self,
|
||||
HandlerArgs {
|
||||
@@ -479,16 +461,17 @@ where
|
||||
type Err = H::Err;
|
||||
}
|
||||
|
||||
impl<Context, H> Handler for RemoteCaller<Context, H>
|
||||
impl<Context, RemoteContext, H> Handler<EitherContext<Context, RemoteContext>>
|
||||
for RemoteCaller<Context, H>
|
||||
where
|
||||
Context: CallRemote<H::Context>,
|
||||
H: Handler,
|
||||
Context: CallRemote<RemoteContext>,
|
||||
RemoteContext: IntoContext,
|
||||
H: Handler<RemoteContext>,
|
||||
H::Params: Serialize,
|
||||
H::InheritedParams: Serialize,
|
||||
H::Ok: DeserializeOwned,
|
||||
H::Err: From<RpcError>,
|
||||
{
|
||||
type Context = EitherContext<Context, H::Context>;
|
||||
async fn handle_async(
|
||||
&self,
|
||||
HandlerArgs {
|
||||
@@ -498,7 +481,7 @@ where
|
||||
params,
|
||||
inherited_params,
|
||||
raw_params,
|
||||
}: HandlerArgsFor<Self::Context, Self>,
|
||||
}: HandlerArgsFor<EitherContext<Context, RemoteContext>, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
match context {
|
||||
EitherContext::C1(context) => {
|
||||
@@ -535,18 +518,17 @@ where
|
||||
self.handler.metadata(method, ctx_ty)
|
||||
}
|
||||
fn contexts(&self) -> Option<OrdSet<TypeId>> {
|
||||
Self::Context::type_ids()
|
||||
Context::type_ids()
|
||||
}
|
||||
fn method_from_dots(&self, method: &str, ctx_ty: TypeId) -> Option<VecDeque<&'static str>> {
|
||||
self.handler.method_from_dots(method, ctx_ty)
|
||||
}
|
||||
}
|
||||
impl<Context, H> PrintCliResult for RemoteCaller<Context, H>
|
||||
impl<Context, H> PrintCliResult<Context> for RemoteCaller<Context, H>
|
||||
where
|
||||
Context: IntoContext,
|
||||
H: PrintCliResult,
|
||||
H: PrintCliResult<Context>,
|
||||
{
|
||||
type Context = H::Context;
|
||||
fn print(
|
||||
&self,
|
||||
HandlerArgs {
|
||||
@@ -556,7 +538,7 @@ where
|
||||
params,
|
||||
inherited_params,
|
||||
raw_params,
|
||||
}: HandlerArgsFor<Self::Context, Self>,
|
||||
}: HandlerArgsFor<Context, Self>,
|
||||
result: Self::Ok,
|
||||
) -> Result<(), Self::Err> {
|
||||
self.handler.print(
|
||||
@@ -611,14 +593,15 @@ where
|
||||
type Err = H::Err;
|
||||
}
|
||||
|
||||
impl<Params, InheritedParams, H, F> Handler for InheritanceHandler<Params, InheritedParams, H, F>
|
||||
impl<Context, Params, InheritedParams, H, F> Handler<Context>
|
||||
for InheritanceHandler<Params, InheritedParams, H, F>
|
||||
where
|
||||
Context: IntoContext,
|
||||
Params: Send + Sync + 'static,
|
||||
InheritedParams: Send + Sync + 'static,
|
||||
H: Handler,
|
||||
H: Handler<Context>,
|
||||
F: Fn(Params, InheritedParams) -> H::InheritedParams + Send + Sync + Clone + 'static,
|
||||
{
|
||||
type Context = H::Context;
|
||||
fn handle_sync(
|
||||
&self,
|
||||
HandlerArgs {
|
||||
@@ -628,7 +611,7 @@ where
|
||||
params,
|
||||
inherited_params,
|
||||
raw_params,
|
||||
}: HandlerArgsFor<Self::Context, Self>,
|
||||
}: HandlerArgsFor<Context, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
self.handler.handle_sync(HandlerArgs {
|
||||
context,
|
||||
@@ -648,7 +631,7 @@ where
|
||||
params,
|
||||
inherited_params,
|
||||
raw_params,
|
||||
}: HandlerArgsFor<Self::Context, Self>,
|
||||
}: HandlerArgsFor<Context, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
self.handler
|
||||
.handle_async(HandlerArgs {
|
||||
@@ -676,15 +659,15 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<Params, InheritedParams, H, F> CliBindings
|
||||
impl<Context, Params, InheritedParams, H, F> CliBindings<Context>
|
||||
for InheritanceHandler<Params, InheritedParams, H, F>
|
||||
where
|
||||
Context: IntoContext,
|
||||
Params: Send + Sync + 'static,
|
||||
InheritedParams: Send + Sync + 'static,
|
||||
H: CliBindings,
|
||||
H: CliBindings<Context>,
|
||||
F: Fn(Params, InheritedParams) -> H::InheritedParams + Send + Sync + Clone + 'static,
|
||||
{
|
||||
type Context = H::Context;
|
||||
fn cli_command(&self, ctx_ty: TypeId) -> clap::Command {
|
||||
self.handler.cli_command(ctx_ty)
|
||||
}
|
||||
@@ -704,7 +687,7 @@ where
|
||||
params,
|
||||
inherited_params,
|
||||
raw_params,
|
||||
}: HandlerArgsFor<Self::Context, Self>,
|
||||
}: HandlerArgsFor<Context, Self>,
|
||||
result: Self::Ok,
|
||||
) -> Result<(), Self::Err> {
|
||||
self.handler.cli_display(
|
||||
|
||||
@@ -2,14 +2,16 @@ use std::any::TypeId;
|
||||
use std::collections::VecDeque;
|
||||
use std::fmt::Display;
|
||||
|
||||
use clap::{CommandFactory, FromArgMatches};
|
||||
use futures::Future;
|
||||
use imbl_value::imbl::OrdMap;
|
||||
use imbl_value::Value;
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::util::PhantomData;
|
||||
use crate::{
|
||||
AnyContext, Empty, Handler, HandlerArgs, HandlerArgsFor, HandlerTypes, IntoContext,
|
||||
CliBindings, Empty, Handler, HandlerArgs, HandlerArgsFor, HandlerTypes, IntoContext,
|
||||
PrintCliResult,
|
||||
};
|
||||
|
||||
@@ -42,18 +44,62 @@ impl<F, T, E, Args> std::fmt::Debug for FromFn<F, T, E, Args> {
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
impl<F, T, E, Args> PrintCliResult for FromFn<F, T, E, Args>
|
||||
impl<Context, F, T, E, Args> PrintCliResult<Context> for FromFn<F, T, E, Args>
|
||||
where
|
||||
Context: IntoContext,
|
||||
Self: HandlerTypes,
|
||||
<Self as HandlerTypes>::Ok: Display,
|
||||
{
|
||||
type Context = AnyContext;
|
||||
fn print(
|
||||
fn print(&self, _: HandlerArgsFor<Context, Self>, result: Self::Ok) -> Result<(), Self::Err> {
|
||||
Ok(println!("{result}"))
|
||||
}
|
||||
}
|
||||
impl<Context, F, T, E, Args> CliBindings<Context> for FromFn<F, T, E, Args>
|
||||
where
|
||||
Context: IntoContext,
|
||||
Self: HandlerTypes,
|
||||
Self::Params: CommandFactory + FromArgMatches + Serialize,
|
||||
Self: PrintCliResult<Context>,
|
||||
{
|
||||
fn cli_command(&self, _: TypeId) -> clap::Command {
|
||||
Self::Params::command()
|
||||
}
|
||||
fn cli_parse(
|
||||
&self,
|
||||
_: HandlerArgsFor<Self::Context, 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,
|
||||
HandlerArgs {
|
||||
context,
|
||||
parent_method,
|
||||
method,
|
||||
params,
|
||||
inherited_params,
|
||||
raw_params,
|
||||
}: HandlerArgsFor<Context, Self>,
|
||||
result: Self::Ok,
|
||||
) -> Result<(), Self::Err> {
|
||||
Ok(println!("{result}"))
|
||||
self.print(
|
||||
HandlerArgs {
|
||||
context,
|
||||
parent_method,
|
||||
method,
|
||||
params,
|
||||
inherited_params,
|
||||
raw_params,
|
||||
},
|
||||
result,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,18 +152,62 @@ impl<F, Fut, T, E, Args> std::fmt::Debug for FromFnAsync<F, Fut, T, E, Args> {
|
||||
f.debug_struct("FromFnAsync").finish()
|
||||
}
|
||||
}
|
||||
impl<F, Fut, T, E, Args> PrintCliResult for FromFnAsync<F, Fut, T, E, Args>
|
||||
impl<Context, F, Fut, T, E, Args> PrintCliResult<Context> for FromFnAsync<F, Fut, T, E, Args>
|
||||
where
|
||||
Context: IntoContext,
|
||||
Self: HandlerTypes,
|
||||
<Self as HandlerTypes>::Ok: Display,
|
||||
{
|
||||
type Context = AnyContext;
|
||||
fn print(
|
||||
fn print(&self, _: HandlerArgsFor<Context, Self>, result: Self::Ok) -> Result<(), Self::Err> {
|
||||
Ok(println!("{result}"))
|
||||
}
|
||||
}
|
||||
impl<Context, F, Fut, T, E, Args> CliBindings<Context> for FromFnAsync<F, Fut, T, E, Args>
|
||||
where
|
||||
Context: IntoContext,
|
||||
Self: HandlerTypes,
|
||||
Self::Params: CommandFactory + FromArgMatches + Serialize,
|
||||
Self: PrintCliResult<Context>,
|
||||
{
|
||||
fn cli_command(&self, _: TypeId) -> clap::Command {
|
||||
Self::Params::command()
|
||||
}
|
||||
fn cli_parse(
|
||||
&self,
|
||||
_: HandlerArgsFor<Self::Context, 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,
|
||||
HandlerArgs {
|
||||
context,
|
||||
parent_method,
|
||||
method,
|
||||
params,
|
||||
inherited_params,
|
||||
raw_params,
|
||||
}: HandlerArgsFor<Context, Self>,
|
||||
result: Self::Ok,
|
||||
) -> Result<(), Self::Err> {
|
||||
Ok(println!("{result}"))
|
||||
self.print(
|
||||
HandlerArgs {
|
||||
context,
|
||||
parent_method,
|
||||
method,
|
||||
params,
|
||||
inherited_params,
|
||||
raw_params,
|
||||
},
|
||||
result,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -152,7 +242,7 @@ where
|
||||
type Err = E;
|
||||
}
|
||||
|
||||
impl<F, T, E, Context, Params, InheritedParams> Handler
|
||||
impl<F, T, E, Context, Params, InheritedParams> Handler<Context>
|
||||
for FromFn<F, T, E, HandlerArgs<Context, Params, InheritedParams>>
|
||||
where
|
||||
F: Fn(HandlerArgs<Context, Params, InheritedParams>) -> Result<T, E>
|
||||
@@ -166,16 +256,15 @@ where
|
||||
Params: Send + Sync + 'static,
|
||||
InheritedParams: Send + Sync + 'static,
|
||||
{
|
||||
type Context = Context;
|
||||
fn handle_sync(
|
||||
&self,
|
||||
handle_args: HandlerArgsFor<Self::Context, Self>,
|
||||
handle_args: HandlerArgsFor<Context, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
(self.function)(handle_args)
|
||||
}
|
||||
async fn handle_async(
|
||||
&self,
|
||||
handle_args: HandlerArgsFor<Self::Context, Self>,
|
||||
handle_args: HandlerArgsFor<Context, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
if self.blocking {
|
||||
self.handle_async_with_sync_blocking(handle_args).await
|
||||
@@ -204,7 +293,7 @@ where
|
||||
type Err = E;
|
||||
}
|
||||
|
||||
impl<F, Fut, T, E, Context, Params, InheritedParams> Handler
|
||||
impl<F, Fut, T, E, Context, Params, InheritedParams> Handler<Context>
|
||||
for FromFnAsync<F, Fut, T, E, HandlerArgs<Context, Params, InheritedParams>>
|
||||
where
|
||||
F: Fn(HandlerArgs<Context, Params, InheritedParams>) -> Fut + Send + Sync + Clone + 'static,
|
||||
@@ -215,10 +304,9 @@ where
|
||||
Params: Send + Sync + 'static,
|
||||
InheritedParams: Send + Sync + 'static,
|
||||
{
|
||||
type Context = Context;
|
||||
async fn handle_async(
|
||||
&self,
|
||||
handle_args: HandlerArgsFor<Self::Context, Self>,
|
||||
handle_args: HandlerArgsFor<Context, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
(self.function)(handle_args).await
|
||||
}
|
||||
@@ -239,19 +327,19 @@ where
|
||||
type Err = E;
|
||||
}
|
||||
|
||||
impl<F, T, E> Handler for FromFn<F, T, E, ()>
|
||||
impl<Context, F, T, E> Handler<Context> for FromFn<F, T, E, ()>
|
||||
where
|
||||
Context: IntoContext,
|
||||
F: Fn() -> Result<T, E> + Send + Sync + Clone + 'static,
|
||||
T: Send + Sync + 'static,
|
||||
E: Send + Sync + 'static,
|
||||
{
|
||||
type Context = AnyContext;
|
||||
fn handle_sync(&self, _: HandlerArgsFor<Self::Context, Self>) -> Result<Self::Ok, Self::Err> {
|
||||
fn handle_sync(&self, _: HandlerArgsFor<Context, Self>) -> Result<Self::Ok, Self::Err> {
|
||||
(self.function)()
|
||||
}
|
||||
async fn handle_async(
|
||||
&self,
|
||||
handle_args: HandlerArgsFor<Self::Context, Self>,
|
||||
handle_args: HandlerArgsFor<Context, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
if self.blocking {
|
||||
self.handle_async_with_sync_blocking(handle_args).await
|
||||
@@ -276,18 +364,15 @@ where
|
||||
type Err = E;
|
||||
}
|
||||
|
||||
impl<F, Fut, T, E> Handler for FromFnAsync<F, Fut, T, E, ()>
|
||||
impl<Context, F, Fut, T, E> Handler<Context> for FromFnAsync<F, Fut, T, E, ()>
|
||||
where
|
||||
Context: IntoContext,
|
||||
F: Fn() -> Fut + Send + Sync + Clone + 'static,
|
||||
Fut: Future<Output = Result<T, E>> + Send + 'static,
|
||||
T: Send + Sync + 'static,
|
||||
E: Send + Sync + 'static,
|
||||
{
|
||||
type Context = AnyContext;
|
||||
async fn handle_async(
|
||||
&self,
|
||||
_: HandlerArgsFor<Self::Context, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
async fn handle_async(&self, _: HandlerArgsFor<Context, Self>) -> Result<Self::Ok, Self::Err> {
|
||||
(self.function)().await
|
||||
}
|
||||
fn metadata(&self, _: VecDeque<&'static str>, _: TypeId) -> OrdMap<&'static str, Value> {
|
||||
@@ -308,23 +393,22 @@ where
|
||||
type Err = E;
|
||||
}
|
||||
|
||||
impl<Context, F, T, E> Handler for FromFn<F, T, E, (Context,)>
|
||||
impl<Context, F, T, E> Handler<Context> for FromFn<F, T, E, (Context,)>
|
||||
where
|
||||
Context: IntoContext,
|
||||
F: Fn(Context) -> Result<T, E> + Send + Sync + Clone + 'static,
|
||||
T: Send + Sync + 'static,
|
||||
E: Send + Sync + 'static,
|
||||
{
|
||||
type Context = Context;
|
||||
fn handle_sync(
|
||||
&self,
|
||||
handle_args: HandlerArgsFor<Self::Context, Self>,
|
||||
handle_args: HandlerArgsFor<Context, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
(self.function)(handle_args.context)
|
||||
}
|
||||
async fn handle_async(
|
||||
&self,
|
||||
handle_args: HandlerArgsFor<Self::Context, Self>,
|
||||
handle_args: HandlerArgsFor<Context, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
if self.blocking {
|
||||
self.handle_async_with_sync_blocking(handle_args).await
|
||||
@@ -350,7 +434,7 @@ where
|
||||
type Err = E;
|
||||
}
|
||||
|
||||
impl<Context, F, Fut, T, E> Handler for FromFnAsync<F, Fut, T, E, (Context,)>
|
||||
impl<Context, F, Fut, T, E> Handler<Context> for FromFnAsync<F, Fut, T, E, (Context,)>
|
||||
where
|
||||
Context: IntoContext,
|
||||
F: Fn(Context) -> Fut + Send + Sync + Clone + 'static,
|
||||
@@ -358,10 +442,9 @@ where
|
||||
T: Send + Sync + 'static,
|
||||
E: Send + Sync + 'static,
|
||||
{
|
||||
type Context = Context;
|
||||
async fn handle_async(
|
||||
&self,
|
||||
handle_args: HandlerArgsFor<Self::Context, Self>,
|
||||
handle_args: HandlerArgsFor<Context, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
(self.function)(handle_args.context).await
|
||||
}
|
||||
@@ -384,7 +467,7 @@ where
|
||||
type Err = E;
|
||||
}
|
||||
|
||||
impl<Context, F, T, E, Params> Handler for FromFn<F, T, E, (Context, Params)>
|
||||
impl<Context, F, T, E, Params> Handler<Context> for FromFn<F, T, E, (Context, Params)>
|
||||
where
|
||||
Context: IntoContext,
|
||||
F: Fn(Context, Params) -> Result<T, E> + Send + Sync + Clone + 'static,
|
||||
@@ -392,10 +475,9 @@ where
|
||||
T: Send + Sync + 'static,
|
||||
E: Send + Sync + 'static,
|
||||
{
|
||||
type Context = Context;
|
||||
fn handle_sync(
|
||||
&self,
|
||||
handle_args: HandlerArgsFor<Self::Context, Self>,
|
||||
handle_args: HandlerArgsFor<Context, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
let HandlerArgs {
|
||||
context, params, ..
|
||||
@@ -404,7 +486,7 @@ where
|
||||
}
|
||||
async fn handle_async(
|
||||
&self,
|
||||
handle_args: HandlerArgsFor<Self::Context, Self>,
|
||||
handle_args: HandlerArgsFor<Context, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
if self.blocking {
|
||||
self.handle_async_with_sync_blocking(handle_args).await
|
||||
@@ -431,7 +513,8 @@ where
|
||||
type Err = E;
|
||||
}
|
||||
|
||||
impl<Context, F, Fut, T, E, Params> Handler for FromFnAsync<F, Fut, T, E, (Context, Params)>
|
||||
impl<Context, F, Fut, T, E, Params> Handler<Context>
|
||||
for FromFnAsync<F, Fut, T, E, (Context, Params)>
|
||||
where
|
||||
Context: IntoContext,
|
||||
F: Fn(Context, Params) -> Fut + Send + Sync + Clone + 'static,
|
||||
@@ -440,10 +523,9 @@ where
|
||||
T: Send + Sync + 'static,
|
||||
E: Send + Sync + 'static,
|
||||
{
|
||||
type Context = Context;
|
||||
async fn handle_async(
|
||||
&self,
|
||||
handle_args: HandlerArgsFor<Self::Context, Self>,
|
||||
handle_args: HandlerArgsFor<Context, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
let HandlerArgs {
|
||||
context, params, ..
|
||||
@@ -471,7 +553,7 @@ where
|
||||
type Err = E;
|
||||
}
|
||||
|
||||
impl<Context, F, T, E, Params, InheritedParams> Handler
|
||||
impl<Context, F, T, E, Params, InheritedParams> Handler<Context>
|
||||
for FromFn<F, T, E, (Context, Params, InheritedParams)>
|
||||
where
|
||||
Context: IntoContext,
|
||||
@@ -481,10 +563,9 @@ where
|
||||
T: Send + Sync + 'static,
|
||||
E: Send + Sync + 'static,
|
||||
{
|
||||
type Context = Context;
|
||||
fn handle_sync(
|
||||
&self,
|
||||
handle_args: HandlerArgsFor<Self::Context, Self>,
|
||||
handle_args: HandlerArgsFor<Context, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
let HandlerArgs {
|
||||
context,
|
||||
@@ -496,7 +577,7 @@ where
|
||||
}
|
||||
async fn handle_async(
|
||||
&self,
|
||||
handle_args: HandlerArgsFor<Self::Context, Self>,
|
||||
handle_args: HandlerArgsFor<Context, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
if self.blocking {
|
||||
self.handle_async_with_sync_blocking(handle_args).await
|
||||
@@ -525,7 +606,7 @@ where
|
||||
type Err = E;
|
||||
}
|
||||
|
||||
impl<Context, F, Fut, T, E, Params, InheritedParams> Handler
|
||||
impl<Context, F, Fut, T, E, Params, InheritedParams> Handler<Context>
|
||||
for FromFnAsync<F, Fut, T, E, (Context, Params, InheritedParams)>
|
||||
where
|
||||
Context: IntoContext,
|
||||
@@ -536,10 +617,9 @@ where
|
||||
T: Send + Sync + 'static,
|
||||
E: Send + Sync + 'static,
|
||||
{
|
||||
type Context = Context;
|
||||
async fn handle_async(
|
||||
&self,
|
||||
handle_args: HandlerArgsFor<Self::Context, Self>,
|
||||
handle_args: HandlerArgsFor<Context, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
let HandlerArgs {
|
||||
context,
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
use std::any::TypeId;
|
||||
use std::collections::VecDeque;
|
||||
use std::marker::PhantomData;
|
||||
use std::ops::Deref;
|
||||
use std::sync::Arc;
|
||||
|
||||
use clap::{ArgMatches, Command, CommandFactory, FromArgMatches, Parser};
|
||||
use clap::{ArgMatches, Command, Parser};
|
||||
use futures::Future;
|
||||
use imbl_value::imbl::{OrdMap, OrdSet};
|
||||
use imbl_value::Value;
|
||||
@@ -72,6 +73,7 @@ pub(crate) trait HandleAny: Send + Sync {
|
||||
ctx_ty: TypeId,
|
||||
) -> OrdMap<&'static str, Value>;
|
||||
fn method_from_dots(&self, method: &str, ctx_ty: TypeId) -> Option<VecDeque<&'static str>>;
|
||||
fn cli(&self) -> Option<&dyn CliBindingsAny<Inherited = Self::Inherited>>;
|
||||
}
|
||||
#[async_trait::async_trait]
|
||||
impl<T: HandleAny> HandleAny for Arc<T> {
|
||||
@@ -95,6 +97,9 @@ impl<T: HandleAny> HandleAny for Arc<T> {
|
||||
fn method_from_dots(&self, method: &str, ctx_ty: TypeId) -> Option<VecDeque<&'static str>> {
|
||||
self.deref().method_from_dots(method, ctx_ty)
|
||||
}
|
||||
fn cli(&self) -> Option<&dyn CliBindingsAny<Inherited = Self::Inherited>> {
|
||||
self.deref().cli()
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) trait CliBindingsAny {
|
||||
@@ -112,8 +117,8 @@ pub(crate) trait CliBindingsAny {
|
||||
) -> Result<(), RpcError>;
|
||||
}
|
||||
|
||||
pub trait CliBindings: HandlerTypes {
|
||||
type Context: IntoContext;
|
||||
pub trait CliBindings<Context: IntoContext>: HandlerTypes {
|
||||
const NO_CLI: bool = false;
|
||||
fn cli_command(&self, ctx_ty: TypeId) -> Command;
|
||||
fn cli_parse(
|
||||
&self,
|
||||
@@ -122,124 +127,57 @@ pub trait CliBindings: HandlerTypes {
|
||||
) -> Result<(VecDeque<&'static str>, Value), clap::Error>;
|
||||
fn cli_display(
|
||||
&self,
|
||||
handle_args: HandlerArgsFor<Self::Context, Self>,
|
||||
handle_args: HandlerArgsFor<Context, Self>,
|
||||
result: Self::Ok,
|
||||
) -> Result<(), Self::Err>;
|
||||
}
|
||||
|
||||
pub trait PrintCliResult: HandlerTypes {
|
||||
type Context: IntoContext;
|
||||
pub trait PrintCliResult<Context: IntoContext>: HandlerTypes {
|
||||
fn print(
|
||||
&self,
|
||||
handle_args: HandlerArgsFor<Self::Context, Self>,
|
||||
handle_args: HandlerArgsFor<Context, Self>,
|
||||
result: Self::Ok,
|
||||
) -> 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,
|
||||
HandlerArgs {
|
||||
context,
|
||||
parent_method,
|
||||
method,
|
||||
params,
|
||||
inherited_params,
|
||||
raw_params,
|
||||
}: HandlerArgsFor<Self::Context, Self>,
|
||||
result: Self::Ok,
|
||||
) -> Result<(), Self::Err> {
|
||||
self.print(
|
||||
HandlerArgs {
|
||||
context,
|
||||
parent_method,
|
||||
method,
|
||||
params,
|
||||
inherited_params,
|
||||
raw_params,
|
||||
},
|
||||
result,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) trait HandleAnyWithCli<Inherited>:
|
||||
HandleAny<Inherited = Inherited> + CliBindingsAny<Inherited = Inherited>
|
||||
{
|
||||
}
|
||||
impl<Inherited, T: HandleAny<Inherited = Inherited> + CliBindingsAny<Inherited = Inherited>>
|
||||
HandleAnyWithCli<Inherited> for T
|
||||
{
|
||||
}
|
||||
|
||||
#[allow(private_interfaces)]
|
||||
pub enum DynHandler<Inherited> {
|
||||
WithoutCli(Arc<dyn HandleAny<Inherited = Inherited>>),
|
||||
WithCli(Arc<dyn HandleAnyWithCli<Inherited>>),
|
||||
pub struct DynHandler<Inherited>(Arc<dyn HandleAny<Inherited = Inherited>>);
|
||||
impl<Inherited> DynHandler<Inherited> {
|
||||
pub fn iter<C: IntoContext, H: Handler<C> + CliBindings<C>>(
|
||||
h: H,
|
||||
) -> Option<impl IntoIterator<Item = (Option<TypeId>, Self)>> {
|
||||
iter_from_ctx_and_handler(ctx, handler)
|
||||
}
|
||||
}
|
||||
impl<Inherited> Clone for DynHandler<Inherited> {
|
||||
fn clone(&self) -> Self {
|
||||
match self {
|
||||
Self::WithCli(a) => Self::WithCli(a.clone()),
|
||||
Self::WithoutCli(a) => Self::WithoutCli(a.clone()),
|
||||
}
|
||||
Self(self.0.clone())
|
||||
}
|
||||
}
|
||||
#[async_trait::async_trait]
|
||||
impl<Inherited: Send> HandleAny for DynHandler<Inherited> {
|
||||
type Inherited = Inherited;
|
||||
fn handle_sync(&self, handle_args: HandleAnyArgs<Self::Inherited>) -> Result<Value, RpcError> {
|
||||
match self {
|
||||
DynHandler::WithoutCli(h) => h.handle_sync(handle_args),
|
||||
DynHandler::WithCli(h) => h.handle_sync(handle_args),
|
||||
}
|
||||
self.0.handle_sync(handle_args)
|
||||
}
|
||||
async fn handle_async(
|
||||
&self,
|
||||
handle_args: HandleAnyArgs<Self::Inherited>,
|
||||
) -> Result<Value, RpcError> {
|
||||
match self {
|
||||
DynHandler::WithoutCli(h) => h.handle_async(handle_args).await,
|
||||
DynHandler::WithCli(h) => h.handle_async(handle_args).await,
|
||||
}
|
||||
self.0.handle_async(handle_args).await
|
||||
}
|
||||
fn metadata(
|
||||
&self,
|
||||
method: VecDeque<&'static str>,
|
||||
ctx_ty: TypeId,
|
||||
) -> OrdMap<&'static str, Value> {
|
||||
match self {
|
||||
DynHandler::WithoutCli(h) => h.metadata(method, ctx_ty),
|
||||
DynHandler::WithCli(h) => h.metadata(method, ctx_ty),
|
||||
}
|
||||
self.0.metadata(method, ctx_ty)
|
||||
}
|
||||
fn method_from_dots(&self, method: &str, ctx_ty: TypeId) -> Option<VecDeque<&'static str>> {
|
||||
match self {
|
||||
DynHandler::WithoutCli(h) => h.method_from_dots(method, ctx_ty),
|
||||
DynHandler::WithCli(h) => h.method_from_dots(method, ctx_ty),
|
||||
}
|
||||
self.0.method_from_dots(method, ctx_ty)
|
||||
}
|
||||
fn cli(&self) -> Option<&dyn CliBindingsAny<Inherited = Self::Inherited>> {
|
||||
self.0.cli()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -268,11 +206,10 @@ pub trait HandlerTypes {
|
||||
type Err: Send + Sync;
|
||||
}
|
||||
|
||||
pub trait Handler: HandlerTypes + Clone + Send + Sync + 'static {
|
||||
type Context: IntoContext;
|
||||
pub trait Handler<Context: IntoContext>: HandlerTypes + Clone + Send + Sync + 'static {
|
||||
fn handle_sync(
|
||||
&self,
|
||||
handle_args: HandlerArgsFor<Self::Context, Self>,
|
||||
handle_args: HandlerArgsFor<Context, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
handle_args
|
||||
.context
|
||||
@@ -281,17 +218,17 @@ pub trait Handler: HandlerTypes + Clone + Send + Sync + 'static {
|
||||
}
|
||||
fn handle_async(
|
||||
&self,
|
||||
handle_args: HandlerArgsFor<Self::Context, Self>,
|
||||
handle_args: HandlerArgsFor<Context, Self>,
|
||||
) -> impl Future<Output = Result<Self::Ok, Self::Err>> + Send;
|
||||
fn handle_async_with_sync<'a>(
|
||||
&'a self,
|
||||
handle_args: HandlerArgsFor<Self::Context, Self>,
|
||||
handle_args: HandlerArgsFor<Context, Self>,
|
||||
) -> impl Future<Output = Result<Self::Ok, Self::Err>> + Send + 'a {
|
||||
async move { self.handle_sync(handle_args) }
|
||||
}
|
||||
fn handle_async_with_sync_blocking<'a>(
|
||||
&'a self,
|
||||
handle_args: HandlerArgsFor<Self::Context, Self>,
|
||||
handle_args: HandlerArgsFor<Context, Self>,
|
||||
) -> impl Future<Output = Result<Self::Ok, Self::Err>> + Send + 'a {
|
||||
async move {
|
||||
let s = self.clone();
|
||||
@@ -312,7 +249,7 @@ pub trait Handler: HandlerTypes + Clone + Send + Sync + 'static {
|
||||
OrdMap::new()
|
||||
}
|
||||
fn contexts(&self) -> Option<OrdSet<TypeId>> {
|
||||
Self::Context::type_ids()
|
||||
Context::type_ids()
|
||||
}
|
||||
#[allow(unused_variables)]
|
||||
fn method_from_dots(&self, method: &str, ctx_ty: TypeId) -> Option<VecDeque<&'static str>> {
|
||||
@@ -324,30 +261,40 @@ pub trait Handler: HandlerTypes + Clone + Send + Sync + 'static {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct AnyHandler<H>(H);
|
||||
impl<H> AnyHandler<H> {
|
||||
pub(crate) struct AnyHandler<Context, H> {
|
||||
_phantom: PhantomData<Context>,
|
||||
handler: H,
|
||||
}
|
||||
impl<Context, H> AnyHandler<Context, H> {
|
||||
pub(crate) fn new(handler: H) -> Self {
|
||||
Self(handler)
|
||||
Self {
|
||||
_phantom: PhantomData,
|
||||
handler,
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<H: std::fmt::Debug> std::fmt::Debug for AnyHandler<H> {
|
||||
impl<Context, H: std::fmt::Debug> std::fmt::Debug for AnyHandler<Context, H> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_tuple("AnyHandler").field(&self.0).finish()
|
||||
f.debug_struct("AnyHandler")
|
||||
.field("handler", &self.handler)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl<H: Handler> HandleAny for AnyHandler<H>
|
||||
impl<Context, H> HandleAny for AnyHandler<Context, H>
|
||||
where
|
||||
Context: IntoContext,
|
||||
H: Handler<Context> + CliBindings<Context>,
|
||||
H::Params: DeserializeOwned,
|
||||
H::Ok: Serialize,
|
||||
H::Ok: Serialize + DeserializeOwned,
|
||||
RpcError: From<H::Err>,
|
||||
{
|
||||
type Inherited = H::InheritedParams;
|
||||
fn handle_sync(&self, handle_args: HandleAnyArgs<Self::Inherited>) -> Result<Value, RpcError> {
|
||||
imbl_value::to_value(
|
||||
&self
|
||||
.0
|
||||
.handler
|
||||
.handle_sync(handle_args.downcast::<_, H>().map_err(invalid_params)?)?,
|
||||
)
|
||||
.map_err(internal_error)
|
||||
@@ -358,7 +305,7 @@ where
|
||||
) -> Result<Value, RpcError> {
|
||||
imbl_value::to_value(
|
||||
&self
|
||||
.0
|
||||
.handler
|
||||
.handle_async(handle_args.downcast::<_, H>().map_err(invalid_params)?)
|
||||
.await?,
|
||||
)
|
||||
@@ -369,37 +316,45 @@ where
|
||||
method: VecDeque<&'static str>,
|
||||
ctx_ty: TypeId,
|
||||
) -> OrdMap<&'static str, Value> {
|
||||
self.0.metadata(method, ctx_ty)
|
||||
self.handler.metadata(method, ctx_ty)
|
||||
}
|
||||
fn method_from_dots(&self, method: &str, ctx_ty: TypeId) -> Option<VecDeque<&'static str>> {
|
||||
self.0.method_from_dots(method, ctx_ty)
|
||||
self.handler.method_from_dots(method, ctx_ty)
|
||||
}
|
||||
fn cli(&self) -> Option<&dyn CliBindingsAny<Inherited = Self::Inherited>> {
|
||||
if H::NO_CLI {
|
||||
None
|
||||
} else {
|
||||
Some(self)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<H: CliBindings> CliBindingsAny for AnyHandler<H>
|
||||
impl<Context, H> CliBindingsAny for AnyHandler<Context, H>
|
||||
where
|
||||
H: CliBindings,
|
||||
Context: IntoContext,
|
||||
H: CliBindings<Context>,
|
||||
H::Params: DeserializeOwned,
|
||||
H::Ok: Serialize + DeserializeOwned,
|
||||
RpcError: From<H::Err>,
|
||||
{
|
||||
type Inherited = H::InheritedParams;
|
||||
fn cli_command(&self, ctx_ty: TypeId) -> Command {
|
||||
self.0.cli_command(ctx_ty)
|
||||
self.handler.cli_command(ctx_ty)
|
||||
}
|
||||
fn cli_parse(
|
||||
&self,
|
||||
matches: &ArgMatches,
|
||||
ctx_ty: TypeId,
|
||||
) -> Result<(VecDeque<&'static str>, Value), clap::Error> {
|
||||
self.0.cli_parse(matches, ctx_ty)
|
||||
self.handler.cli_parse(matches, ctx_ty)
|
||||
}
|
||||
fn cli_display(
|
||||
&self,
|
||||
handle_args: HandleAnyArgs<Self::Inherited>,
|
||||
result: Value,
|
||||
) -> Result<(), RpcError> {
|
||||
self.0
|
||||
self.handler
|
||||
.cli_display(
|
||||
handle_args.downcast::<_, H>().map_err(invalid_params)?,
|
||||
imbl_value::from_value(result).map_err(internal_error)?,
|
||||
|
||||
@@ -1,44 +1,18 @@
|
||||
use std::any::TypeId;
|
||||
use std::collections::VecDeque;
|
||||
use std::sync::Arc;
|
||||
|
||||
use clap::{ArgMatches, Command, CommandFactory, FromArgMatches};
|
||||
use imbl_value::imbl::{OrdMap, OrdSet};
|
||||
use imbl_value::Value;
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::Serialize;
|
||||
use yajrc::RpcError;
|
||||
|
||||
use crate::util::{combine, Flat, PhantomData};
|
||||
use crate::{
|
||||
AnyContext, AnyHandler, CliBindings, DynHandler, Empty, HandleAny, HandleAnyArgs, Handler,
|
||||
HandlerArgs, HandlerArgsFor, HandlerExt, HandlerTypes, IntoContext, OrEmpty,
|
||||
AnyContext, CliBindings, DynHandler, Empty, HandleAny, HandleAnyArgs, Handler, HandlerArgs,
|
||||
HandlerArgsFor, HandlerTypes, IntoContext,
|
||||
};
|
||||
|
||||
pub trait IntoHandlers<Inherited>: HandlerTypes {
|
||||
fn into_handlers(self) -> impl IntoIterator<Item = (Option<TypeId>, DynHandler<Inherited>)>;
|
||||
}
|
||||
|
||||
impl<H, A, B> IntoHandlers<Flat<A, B>> for H
|
||||
where
|
||||
H: Handler + CliBindings,
|
||||
H::Params: DeserializeOwned,
|
||||
H::InheritedParams: OrEmpty<Flat<A, B>>,
|
||||
H::Ok: Serialize + DeserializeOwned,
|
||||
RpcError: From<H::Err>,
|
||||
A: Send + Sync + 'static,
|
||||
B: Send + Sync + 'static,
|
||||
{
|
||||
fn into_handlers(self) -> impl IntoIterator<Item = (Option<TypeId>, DynHandler<Flat<A, B>>)> {
|
||||
iter_from_ctx_and_handler(
|
||||
intersect_type_ids(self.contexts(), <Self as CliBindings>::Context::type_ids()),
|
||||
DynHandler::WithCli(Arc::new(AnyHandler::new(
|
||||
self.with_inherited(|a, b| OrEmpty::from_t(Flat(a, b))),
|
||||
))),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn iter_from_ctx_and_handler<Inherited>(
|
||||
ctx: Option<OrdSet<TypeId>>,
|
||||
handler: DynHandler<Inherited>,
|
||||
@@ -109,12 +83,12 @@ impl<Inherited> SubcommandMap<Inherited> {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ParentHandler<Params = Empty, InheritedParams = Empty> {
|
||||
_phantom: PhantomData<Params>,
|
||||
pub struct ParentHandler<Context = AnyContext, Params = Empty, InheritedParams = Empty> {
|
||||
_phantom: PhantomData<Context>,
|
||||
pub(crate) subcommands: SubcommandMap<Flat<Params, InheritedParams>>,
|
||||
metadata: OrdMap<&'static str, Value>,
|
||||
}
|
||||
impl<Params, InheritedParams> ParentHandler<Params, InheritedParams> {
|
||||
impl<Context, Params, InheritedParams> ParentHandler<Context, Params, InheritedParams> {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
_phantom: PhantomData::new(),
|
||||
@@ -127,7 +101,7 @@ impl<Params, InheritedParams> ParentHandler<Params, InheritedParams> {
|
||||
self
|
||||
}
|
||||
}
|
||||
impl<Params, InheritedParams> Clone for ParentHandler<Params, InheritedParams> {
|
||||
impl<Context, Params, InheritedParams> Clone for ParentHandler<Context, Params, InheritedParams> {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
_phantom: PhantomData::new(),
|
||||
@@ -136,7 +110,9 @@ impl<Params, InheritedParams> Clone for ParentHandler<Params, InheritedParams> {
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<Params, InheritedParams> std::fmt::Debug for ParentHandler<Params, InheritedParams> {
|
||||
impl<Context, Params, InheritedParams> std::fmt::Debug
|
||||
for ParentHandler<Context, Params, InheritedParams>
|
||||
{
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_tuple("ParentHandler")
|
||||
// .field(&self.subcommands)
|
||||
@@ -144,7 +120,9 @@ impl<Params, InheritedParams> std::fmt::Debug for ParentHandler<Params, Inherite
|
||||
}
|
||||
}
|
||||
|
||||
impl<Params, InheritedParams> ParentHandler<Params, InheritedParams> {
|
||||
impl<Context: IntoContext, Params, InheritedParams>
|
||||
ParentHandler<Context, Params, InheritedParams>
|
||||
{
|
||||
fn get_contexts(&self) -> Option<OrdSet<TypeId>> {
|
||||
let mut set = OrdSet::new();
|
||||
for ctx_ty in self.subcommands.0.values().flat_map(|c| c.keys()) {
|
||||
@@ -153,25 +131,30 @@ impl<Params, InheritedParams> ParentHandler<Params, InheritedParams> {
|
||||
Some(set)
|
||||
}
|
||||
#[allow(private_bounds)]
|
||||
pub fn subcommand<H>(mut self, name: &'static str, handler: H) -> Self
|
||||
pub fn subcommand<C: IntoContext, H>(mut self, name: &'static str, handler: H) -> Self
|
||||
where
|
||||
H: IntoHandlers<Flat<Params, InheritedParams>>,
|
||||
H: Handler<C, InheritedParams = Flat<Params, InheritedParams>> + CliBindings<C>,
|
||||
{
|
||||
self.subcommands
|
||||
.insert(name.into(), handler.into_handlers());
|
||||
if let Some(h) = DynHandler::iter(handler) {
|
||||
self.subcommands.insert(name.into(), h);
|
||||
}
|
||||
self
|
||||
}
|
||||
#[allow(private_bounds)]
|
||||
pub fn root_handler<H>(mut self, handler: H) -> Self
|
||||
pub fn root_handler<C: IntoContext, H>(mut self, handler: H) -> Self
|
||||
where
|
||||
H: IntoHandlers<Flat<Params, InheritedParams>> + HandlerTypes<Params = Empty>,
|
||||
H: Handler<C, Params = Empty, InheritedParams = Flat<Params, InheritedParams>>
|
||||
+ CliBindings<C>,
|
||||
{
|
||||
self.subcommands.insert(None, handler.into_handlers());
|
||||
if let Some((c, h)) = DynHandler::iter(handler) {
|
||||
self.subcommands.insert(None, h);
|
||||
}
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<Params, InheritedParams> HandlerTypes for ParentHandler<Params, InheritedParams>
|
||||
impl<Context, Params, InheritedParams> HandlerTypes
|
||||
for ParentHandler<Context, Params, InheritedParams>
|
||||
where
|
||||
Params: Send + Sync,
|
||||
InheritedParams: Send + Sync,
|
||||
@@ -182,12 +165,13 @@ where
|
||||
type Err = RpcError;
|
||||
}
|
||||
|
||||
impl<Params, InheritedParams> Handler for ParentHandler<Params, InheritedParams>
|
||||
impl<Context, Params, InheritedParams> Handler<Context>
|
||||
for ParentHandler<Context, Params, InheritedParams>
|
||||
where
|
||||
Context: IntoContext,
|
||||
Params: Send + Sync + 'static,
|
||||
InheritedParams: Serialize + Send + Sync + 'static,
|
||||
{
|
||||
type Context = AnyContext;
|
||||
fn handle_sync(
|
||||
&self,
|
||||
HandlerArgs {
|
||||
@@ -197,7 +181,7 @@ where
|
||||
params,
|
||||
inherited_params,
|
||||
raw_params,
|
||||
}: HandlerArgsFor<AnyContext, Self>,
|
||||
}: HandlerArgsFor<Context, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
let cmd = method.pop_front();
|
||||
if let Some(cmd) = cmd {
|
||||
@@ -205,7 +189,7 @@ where
|
||||
}
|
||||
if let Some((_, sub_handler)) = &self.subcommands.get(context.inner_type_id(), cmd) {
|
||||
sub_handler.handle_sync(HandleAnyArgs {
|
||||
context,
|
||||
context: context.upcast(),
|
||||
parent_method,
|
||||
method,
|
||||
params: raw_params,
|
||||
@@ -224,7 +208,7 @@ where
|
||||
params,
|
||||
inherited_params,
|
||||
raw_params,
|
||||
}: HandlerArgsFor<AnyContext, Self>,
|
||||
}: HandlerArgsFor<Context, Self>,
|
||||
) -> Result<Self::Ok, Self::Err> {
|
||||
let cmd = method.pop_front();
|
||||
if let Some(cmd) = cmd {
|
||||
@@ -233,7 +217,7 @@ where
|
||||
if let Some((_, sub_handler)) = self.subcommands.get(context.inner_type_id(), cmd) {
|
||||
sub_handler
|
||||
.handle_async(HandleAnyArgs {
|
||||
context,
|
||||
context: context.upcast(),
|
||||
parent_method,
|
||||
method,
|
||||
params: raw_params,
|
||||
@@ -280,29 +264,30 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<Params, InheritedParams> CliBindings for ParentHandler<Params, InheritedParams>
|
||||
impl<Context, Params, InheritedParams> CliBindings<Context>
|
||||
for ParentHandler<Context, Params, InheritedParams>
|
||||
where
|
||||
Context: IntoContext,
|
||||
Params: FromArgMatches + CommandFactory + Serialize + Send + Sync + 'static,
|
||||
InheritedParams: Serialize + Send + Sync + 'static,
|
||||
{
|
||||
type Context = AnyContext;
|
||||
fn cli_command(&self, ctx_ty: TypeId) -> Command {
|
||||
let mut base = Params::command().subcommand_required(true);
|
||||
for (name, handlers) in &self.subcommands.0 {
|
||||
match (
|
||||
name,
|
||||
if let Some(handler) = handlers.get(&Some(ctx_ty)) {
|
||||
Some(handler)
|
||||
handler.cli()
|
||||
} else if let Some(handler) = handlers.get(&None) {
|
||||
Some(handler)
|
||||
handler.cli()
|
||||
} else {
|
||||
None
|
||||
},
|
||||
) {
|
||||
(Name(Some(name)), Some(DynHandler::WithCli(handler))) => {
|
||||
base = base.subcommand(handler.cli_command(ctx_ty).name(name));
|
||||
(Name(Some(name)), Some(cli)) => {
|
||||
base = base.subcommand(cli.cli_command(ctx_ty).name(name));
|
||||
}
|
||||
(Name(None), Some(DynHandler::WithCli(_))) => {
|
||||
(Name(None), Some(_)) => {
|
||||
base = base.subcommand_required(false);
|
||||
}
|
||||
_ => (),
|
||||
@@ -321,10 +306,12 @@ where
|
||||
Some((name, matches)) => (Some(name), matches),
|
||||
None => (None, matches),
|
||||
};
|
||||
if let Some((Name(Some(name)), DynHandler::WithCli(handler))) =
|
||||
self.subcommands.get(ctx_ty, name)
|
||||
if let Some((Name(Some(name)), cli)) = self
|
||||
.subcommands
|
||||
.get(ctx_ty, name)
|
||||
.and_then(|(n, h)| h.cli().map(|c| (n, c)))
|
||||
{
|
||||
let (mut method, params) = handler.cli_parse(matches, ctx_ty)?;
|
||||
let (mut method, params) = cli.cli_parse(matches, ctx_ty)?;
|
||||
method.push_front(name);
|
||||
|
||||
Ok((
|
||||
@@ -345,19 +332,21 @@ where
|
||||
params,
|
||||
inherited_params,
|
||||
raw_params,
|
||||
}: HandlerArgsFor<AnyContext, Self>,
|
||||
}: HandlerArgsFor<Context, Self>,
|
||||
result: Self::Ok,
|
||||
) -> Result<(), Self::Err> {
|
||||
let cmd = method.pop_front();
|
||||
if let Some(cmd) = cmd {
|
||||
parent_method.push_back(cmd);
|
||||
}
|
||||
if let Some((_, DynHandler::WithCli(sub_handler))) =
|
||||
self.subcommands.get(context.inner_type_id(), cmd)
|
||||
if let Some((_, cli)) = self
|
||||
.subcommands
|
||||
.get(context.inner_type_id(), cmd)
|
||||
.and_then(|(n, h)| h.cli().map(|c| (n, c)))
|
||||
{
|
||||
sub_handler.cli_display(
|
||||
cli.cli_display(
|
||||
HandleAnyArgs {
|
||||
context,
|
||||
context: context.upcast(),
|
||||
parent_method,
|
||||
method,
|
||||
params: raw_params,
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
#![cfg_attr(feature = "nightly", feature(const_trait_impl, const_type_id))]
|
||||
|
||||
pub use cli::*;
|
||||
// pub use command::*;
|
||||
pub use context::*;
|
||||
|
||||
@@ -4,11 +4,11 @@ use std::sync::Arc;
|
||||
|
||||
use futures::future::{join_all, BoxFuture};
|
||||
use futures::{Future, FutureExt, Stream, StreamExt};
|
||||
use imbl_value::{InOMap, Value};
|
||||
use imbl_value::Value;
|
||||
use yajrc::{RpcError, RpcMethod};
|
||||
|
||||
use crate::util::{invalid_request, JobRunner};
|
||||
use crate::{AnyHandler, Empty, HandleAny, HandleAnyArgs, IntoContext, ParentHandler};
|
||||
use crate::{AnyHandler, HandleAny, HandleAnyArgs, IntoContext, ParentHandler};
|
||||
|
||||
pub type GenericRpcMethod = yajrc::GenericRpcMethod<String, Value, Value>;
|
||||
pub type RpcRequest = yajrc::RpcRequest<GenericRpcMethod>;
|
||||
@@ -23,7 +23,7 @@ pub use socket::*;
|
||||
|
||||
pub struct Server<Context: crate::Context> {
|
||||
make_ctx: Arc<dyn Fn() -> BoxFuture<'static, Result<Context, RpcError>> + Send + Sync>,
|
||||
root_handler: Arc<AnyHandler<ParentHandler>>,
|
||||
root_handler: Arc<AnyHandler<Context, ParentHandler<Context>>>,
|
||||
}
|
||||
impl<Context: crate::Context> Clone for Server<Context> {
|
||||
fn clone(&self) -> Self {
|
||||
@@ -39,7 +39,7 @@ impl<Context: crate::Context> Server<Context> {
|
||||
Fut: Future<Output = Result<Context, RpcError>> + Send + 'static,
|
||||
>(
|
||||
make_ctx: MakeCtx,
|
||||
root_handler: ParentHandler,
|
||||
root_handler: ParentHandler<Context>,
|
||||
) -> Self {
|
||||
Server {
|
||||
make_ctx: Arc::new(move || make_ctx().boxed()),
|
||||
|
||||
Reference in New Issue
Block a user