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