diff --git a/rpc-toolkit-macro-internals/src/command/build.rs b/rpc-toolkit-macro-internals/src/command/build.rs index 15d45dd..8b3190f 100644 --- a/rpc-toolkit-macro-internals/src/command/build.rs +++ b/rpc-toolkit-macro-internals/src/command/build.rs @@ -1362,7 +1362,7 @@ fn build_inherited(parent_data: Option, generics: &Generics) -> (TokenStre ) } else { ( - quote! { type InheritedParams = ::rpc_toolkit::NoParams; }, + quote! { type InheritedParams = ::rpc_toolkit::Empty; }, inherited_generics, ) } diff --git a/rpc-toolkit/src/handler/adapters.rs b/rpc-toolkit/src/handler/adapters.rs index 0eca646..5b1c5af 100644 --- a/rpc-toolkit/src/handler/adapters.rs +++ b/rpc-toolkit/src/handler/adapters.rs @@ -3,7 +3,6 @@ use std::collections::VecDeque; use std::fmt::Debug; use std::marker::PhantomData; use std::sync::Arc; -use std::task::Context; use clap::{CommandFactory, FromArgMatches}; use imbl_value::imbl::{OrdMap, OrdSet}; @@ -14,8 +13,8 @@ use yajrc::RpcError; use crate::util::{internal_error, parse_error, Flat}; use crate::{ - intersect_type_ids, iter_from_ctx_and_handler, AnyHandler, CallRemote, CliBindings, DynHandler, - HandleArgs, Handler, HandlerTypes, IntoContext, IntoHandlers, NoParams, PrintCliResult, + iter_from_ctx_and_handler, AnyHandler, CallRemote, CliBindings, DynHandler, EitherContext, + HandleArgs, Handler, HandlerTypes, IntoContext, IntoHandlers, PrintCliResult, }; pub trait HandlerExt: HandlerTypes + Sized { @@ -34,6 +33,13 @@ pub trait HandlerExt: HandlerTypes + Sized { ) -> CustomDisplayFn where F: Fn(HandleArgs, Self::Ok) -> Result<(), Self::Err>; + fn with_inherited( + self, + f: F, + ) -> InheritanceHandler + where + F: Fn(Params, InheritedParams) -> Self::InheritedParams; + fn with_remote_cli(self) -> RemoteCli; } impl HandlerExt for T { @@ -67,6 +73,25 @@ impl HandlerExt for T { handler: self, } } + fn with_inherited( + self, + f: F, + ) -> InheritanceHandler + where + F: Fn(Params, InheritedParams) -> Self::InheritedParams, + { + InheritanceHandler { + _phantom: PhantomData, + handler: self, + inherit: f, + } + } + fn with_remote_cli(self) -> RemoteCli { + RemoteCli { + _phantom: PhantomData, + handler: self, + } + } } #[derive(Debug, Clone)] @@ -77,6 +102,21 @@ impl HandlerTypes for NoCli { type Ok = H::Ok; type Err = H::Err; } +impl IntoHandlers for NoCli +where + H: Handler, + H::Params: DeserializeOwned, + H::InheritedParams: DeserializeOwned, + H::Ok: Serialize + DeserializeOwned, + RpcError: From, +{ + fn into_handlers(self) -> impl IntoIterator, DynHandler)> { + iter_from_ctx_and_handler( + self.0.contexts(), + DynHandler::WithoutCli(Arc::new(AnyHandler::new(self.0))), + ) + } +} #[derive(Clone, Debug)] pub struct CustomDisplay { @@ -171,13 +211,13 @@ where + 'static, { type Context = P::Context; - fn cli_command(&self, ctx_ty: TypeId) -> clap::Command { + fn cli_command(&self, _: TypeId) -> clap::Command { H::Params::command() } fn cli_parse( &self, matches: &clap::ArgMatches, - ctx_ty: TypeId, + _: TypeId, ) -> Result<(VecDeque<&'static str>, Value), clap::Error> { Self::Params::from_arg_matches(matches).and_then(|a| { Ok(( @@ -212,21 +252,6 @@ where ) } } -impl IntoHandlers for CustomDisplay -where - Self: HandlerTypes + Handler + CliBindings, - ::Params: DeserializeOwned, - ::InheritedParams: DeserializeOwned, - ::Ok: Serialize + DeserializeOwned, - RpcError: From<::Err>, -{ - fn into_handlers(self) -> impl IntoIterator, crate::DynHandler)> { - iter_from_ctx_and_handler( - intersect_type_ids(self.contexts(), ::Context::type_ids()), - DynHandler::WithCli(Arc::new(AnyHandler::new(self))), - ) - } -} pub struct CustomDisplayFn { _phantom: PhantomData, @@ -371,115 +396,282 @@ where } } -// pub struct RemoteCli { -// _phantom: PhantomData<(CliContext, RemoteContext)>, -// handler: H, -// } -// impl Clone for RemoteCli { -// fn clone(&self) -> Self { -// Self { -// _phantom: PhantomData, -// handler: self.handler.clone(), -// } -// } -// } -// impl Debug for RemoteCli { -// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { -// f.debug_tuple("RemoteCli").field(&self.handler).finish() -// } -// } -// impl HandlerTypes for RemoteCli -// where -// H: HandlerTypes, -// { -// type Params = H::Params; -// type InheritedParams = H::InheritedParams; -// type Ok = H::Ok; -// type Err = H::Err; -// } -// #[async_trait::async_trait] -// impl Handler> -// for RemoteCli -// where -// CliContext: CallRemote, -// H: Handler, -// { -// async fn handle_async( -// &self, -// HandleArgs { -// context, -// parent_method, -// method, -// params, -// inherited_params, -// raw_params, -// }: HandleArgs, -// ) -> Result { -// let full_method = parent_method.into_iter().chain(method).collect::>(); -// match context -// .call_remote( -// &full_method.join("."), -// imbl_value::to_value(&Flat(params, inherited_params)).map_err(parse_error)?, -// ) -// .await -// { -// Ok(a) => imbl_value::from_value(a) -// .map_err(internal_error) -// .map_err(Self::Err::from), -// Err(e) => Err(Self::Err::from(e)), -// } -// } -// fn metadata( -// &self, -// method: VecDeque<&'static str>, -// ctx_ty: TypeId, -// ) -> OrdMap<&'static str, Value> { -// self.handler.metadata(method, ctx_ty) -// } -// fn contexts(&self) -> Option> { -// todo!() -// } -// fn method_from_dots(&self, method: &str, ctx_ty: TypeId) -> Option> { -// self.handler.method_from_dots(method, ctx_ty) -// } -// } -// impl CliBindings for RemoteCli -// where -// Context: crate::Context, -// H: CliBindings, -// { -// fn cli_command(&self, ctx_ty: TypeId) -> clap::Command { -// self.handler.cli_command(ctx_ty) -// } -// fn cli_parse( -// &self, -// matches: &clap::ArgMatches, -// ctx_ty: TypeId, -// ) -> Result<(VecDeque<&'static str>, Value), clap::Error> { -// self.handler.cli_parse(matches, ctx_ty) -// } -// fn cli_display( -// &self, -// HandleArgs { -// context, -// parent_method, -// method, -// params, -// inherited_params, -// raw_params, -// }: HandleArgs, -// result: Self::Ok, -// ) -> Result<(), Self::Err> { -// self.handler.cli_display( -// HandleArgs { -// context, -// parent_method, -// method, -// params, -// inherited_params, -// raw_params, -// }, -// result, -// ) -// } -// } +pub struct RemoteCli { + _phantom: PhantomData, + handler: H, +} +impl Clone for RemoteCli { + fn clone(&self) -> Self { + Self { + _phantom: PhantomData, + handler: self.handler.clone(), + } + } +} +impl Debug for RemoteCli { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_tuple("RemoteCli").field(&self.handler).finish() + } +} +impl HandlerTypes for RemoteCli +where + H: HandlerTypes, +{ + type Params = H::Params; + type InheritedParams = H::InheritedParams; + type Ok = H::Ok; + type Err = H::Err; +} +#[async_trait::async_trait] +impl Handler for RemoteCli +where + Context: CallRemote, + H: Handler, + H::Params: Serialize, + H::InheritedParams: Serialize, + H::Ok: DeserializeOwned, + H::Err: From, +{ + type Context = EitherContext; + async fn handle_async( + &self, + HandleArgs { + context, + parent_method, + method, + params, + inherited_params, + raw_params, + }: HandleArgs, + ) -> Result { + match context { + EitherContext::C1(context) => { + let full_method = parent_method.into_iter().chain(method).collect::>(); + match context + .call_remote( + &full_method.join("."), + imbl_value::to_value(&Flat(params, inherited_params)) + .map_err(parse_error)?, + ) + .await + { + Ok(a) => imbl_value::from_value(a) + .map_err(internal_error) + .map_err(Self::Err::from), + Err(e) => Err(Self::Err::from(e)), + } + } + EitherContext::C2(context) => { + self.handler + .handle_async(HandleArgs { + context, + parent_method, + method, + params, + inherited_params, + raw_params, + }) + .await + } + } + } + fn metadata( + &self, + method: VecDeque<&'static str>, + ctx_ty: TypeId, + ) -> OrdMap<&'static str, Value> { + self.handler.metadata(method, ctx_ty) + } + fn contexts(&self) -> Option> { + todo!() + } + fn method_from_dots(&self, method: &str, ctx_ty: TypeId) -> Option> { + self.handler.method_from_dots(method, ctx_ty) + } +} +impl CliBindings for RemoteCli +where + H: CliBindings, +{ + type Context = H::Context; + fn cli_command(&self, ctx_ty: TypeId) -> clap::Command { + self.handler.cli_command(ctx_ty) + } + fn cli_parse( + &self, + matches: &clap::ArgMatches, + ctx_ty: TypeId, + ) -> Result<(VecDeque<&'static str>, Value), clap::Error> { + self.handler.cli_parse(matches, ctx_ty) + } + fn cli_display( + &self, + HandleArgs { + context, + parent_method, + method, + params, + inherited_params, + raw_params, + }: HandleArgs, + result: Self::Ok, + ) -> Result<(), Self::Err> { + self.handler.cli_display( + HandleArgs { + context, + parent_method, + method, + params, + inherited_params, + raw_params, + }, + result, + ) + } +} + +pub struct InheritanceHandler { + _phantom: PhantomData<(Params, InheritedParams)>, + handler: H, + inherit: F, +} +impl Clone + for InheritanceHandler +{ + fn clone(&self) -> Self { + Self { + _phantom: PhantomData, + handler: self.handler.clone(), + inherit: self.inherit.clone(), + } + } +} +impl std::fmt::Debug + for InheritanceHandler +{ + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_tuple("InheritanceHandler") + .field(&self.handler) + .finish() + } +} +impl HandlerTypes + for InheritanceHandler +where + H: HandlerTypes, + Params: Send + Sync, + InheritedParams: Send + Sync, +{ + type Params = H::Params; + type InheritedParams = Flat; + type Ok = H::Ok; + type Err = H::Err; +} +#[async_trait::async_trait] +impl Handler for InheritanceHandler +where + Params: Send + Sync + 'static, + InheritedParams: Send + Sync + 'static, + H: Handler, + F: Fn(Params, InheritedParams) -> H::InheritedParams + Send + Sync + Clone + 'static, +{ + type Context = H::Context; + fn handle_sync( + &self, + HandleArgs { + context, + parent_method, + method, + params, + inherited_params, + raw_params, + }: HandleArgs, + ) -> Result { + self.handler.handle_sync(HandleArgs { + context, + parent_method, + method, + params, + inherited_params: (self.inherit)(inherited_params.0, inherited_params.1), + raw_params, + }) + } + async fn handle_async( + &self, + HandleArgs { + context, + parent_method, + method, + params, + inherited_params, + raw_params, + }: HandleArgs, + ) -> Result { + self.handler.handle_sync(HandleArgs { + context, + parent_method, + method, + params, + inherited_params: (self.inherit)(inherited_params.0, inherited_params.1), + raw_params, + }) + } + fn metadata( + &self, + method: VecDeque<&'static str>, + ctx_ty: TypeId, + ) -> OrdMap<&'static str, Value> { + self.handler.metadata(method, ctx_ty) + } + fn contexts(&self) -> Option> { + self.handler.contexts() + } + fn method_from_dots(&self, method: &str, ctx_ty: TypeId) -> Option> { + self.handler.method_from_dots(method, ctx_ty) + } +} + +impl CliBindings + for InheritanceHandler +where + Params: Send + Sync + 'static, + InheritedParams: Send + Sync + 'static, + H: CliBindings, + 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) + } + fn cli_parse( + &self, + matches: &clap::ArgMatches, + ctx_ty: TypeId, + ) -> Result<(VecDeque<&'static str>, Value), clap::Error> { + self.handler.cli_parse(matches, ctx_ty) + } + fn cli_display( + &self, + HandleArgs { + context, + parent_method, + method, + params, + inherited_params, + raw_params, + }: HandleArgs, + result: Self::Ok, + ) -> Result<(), Self::Err> { + self.handler.cli_display( + HandleArgs { + context, + parent_method, + method, + params, + inherited_params: (self.inherit)(inherited_params.0, inherited_params.1), + raw_params, + }, + result, + ) + } +} diff --git a/rpc-toolkit/src/handler/from_fn.rs b/rpc-toolkit/src/handler/from_fn.rs index fa06850..e3bec14 100644 --- a/rpc-toolkit/src/handler/from_fn.rs +++ b/rpc-toolkit/src/handler/from_fn.rs @@ -2,7 +2,6 @@ use std::any::TypeId; use std::collections::VecDeque; use std::fmt::Display; use std::marker::PhantomData; -use std::sync::Arc; use clap::{ArgMatches, Command, CommandFactory, FromArgMatches}; use futures::Future; @@ -10,12 +9,10 @@ use imbl_value::imbl::OrdMap; use imbl_value::Value; use serde::de::DeserializeOwned; use serde::Serialize; -use yajrc::RpcError; use crate::marker::LeafHandler; use crate::{ - intersect_type_ids, iter_from_ctx_and_handler, AnyContext, AnyHandler, CliBindings, DynHandler, - HandleArgs, Handler, HandlerTypes, IntoContext, IntoHandlers, NoCli, NoParams, PrintCliResult, + AnyContext, CliBindings, Empty, HandleArgs, Handler, HandlerTypes, IntoContext, PrintCliResult, }; pub struct FromFn { @@ -58,36 +55,6 @@ where Ok(println!("{result}")) } } -impl IntoHandlers for FromFn -where - Self: HandlerTypes + Handler + CliBindings, - ::Params: DeserializeOwned, - ::InheritedParams: DeserializeOwned, - ::Ok: Serialize + DeserializeOwned, - RpcError: From<::Err>, -{ - fn into_handlers(self) -> impl IntoIterator, crate::DynHandler)> { - iter_from_ctx_and_handler( - intersect_type_ids(self.contexts(), ::Context::type_ids()), - DynHandler::WithCli(Arc::new(AnyHandler::new(self))), - ) - } -} -impl IntoHandlers for NoCli> -where - Self: HandlerTypes + Handler, - ::Params: DeserializeOwned, - ::InheritedParams: DeserializeOwned, - ::Ok: Serialize + DeserializeOwned, - RpcError: From<::Err>, -{ - fn into_handlers(self) -> impl IntoIterator, crate::DynHandler)> { - iter_from_ctx_and_handler( - self.contexts(), - DynHandler::WithoutCli(Arc::new(AnyHandler::new(self))), - ) - } -} pub fn from_fn(function: F) -> FromFn { FromFn { @@ -136,36 +103,6 @@ where Ok(println!("{result}")) } } -impl IntoHandlers for FromFnAsync -where - Self: HandlerTypes + Handler + CliBindings, - ::Params: DeserializeOwned, - ::InheritedParams: DeserializeOwned, - ::Ok: Serialize + DeserializeOwned, - RpcError: From<::Err>, -{ - fn into_handlers(self) -> impl IntoIterator, crate::DynHandler)> { - iter_from_ctx_and_handler( - intersect_type_ids(self.contexts(), ::Context::type_ids()), - DynHandler::WithCli(Arc::new(AnyHandler::new(self))), - ) - } -} -impl IntoHandlers for NoCli> -where - Self: HandlerTypes + Handler, - ::Params: DeserializeOwned, - ::InheritedParams: DeserializeOwned, - ::Ok: Serialize + DeserializeOwned, - RpcError: From<::Err>, -{ - fn into_handlers(self) -> impl IntoIterator, crate::DynHandler)> { - iter_from_ctx_and_handler( - self.contexts(), - DynHandler::WithoutCli(Arc::new(AnyHandler::new(self))), - ) - } -} pub fn from_fn_async(function: F) -> FromFnAsync { FromFnAsync { @@ -181,8 +118,8 @@ where T: Send + Sync + 'static, E: Send + Sync + 'static, { - type Params = NoParams; - type InheritedParams = NoParams; + type Params = Empty; + type InheritedParams = Empty; type Ok = T; type Err = E; } @@ -218,8 +155,8 @@ where T: Send + Sync + 'static, E: Send + Sync + 'static, { - type Params = NoParams; - type InheritedParams = NoParams; + type Params = Empty; + type InheritedParams = Empty; type Ok = T; type Err = E; } @@ -250,8 +187,8 @@ where T: Send + Sync + 'static, E: Send + Sync + 'static, { - type Params = NoParams; - type InheritedParams = NoParams; + type Params = Empty; + type InheritedParams = Empty; type Ok = T; type Err = E; } @@ -292,8 +229,8 @@ where T: Send + Sync + 'static, E: Send + Sync + 'static, { - type Params = NoParams; - type InheritedParams = NoParams; + type Params = Empty; + type InheritedParams = Empty; type Ok = T; type Err = E; } @@ -327,7 +264,7 @@ where E: Send + Sync + 'static, { type Params = Params; - type InheritedParams = NoParams; + type InheritedParams = Empty; type Ok = T; type Err = E; } @@ -374,7 +311,7 @@ where E: Send + Sync + 'static, { type Params = Params; - type InheritedParams = NoParams; + type InheritedParams = Empty; type Ok = T; type Err = E; } diff --git a/rpc-toolkit/src/handler/mod.rs b/rpc-toolkit/src/handler/mod.rs index d859c7c..f531a5b 100644 --- a/rpc-toolkit/src/handler/mod.rs +++ b/rpc-toolkit/src/handler/mod.rs @@ -11,7 +11,7 @@ use serde::{Deserialize, Serialize}; use yajrc::RpcError; use crate::context::{AnyContext, IntoContext}; -use crate::util::{internal_error, invalid_params}; +use crate::util::{internal_error, invalid_params, Flat}; pub mod adapters; pub mod from_fn; @@ -311,7 +311,11 @@ where } #[derive(Debug, Clone, Copy, Deserialize, Serialize, Parser)] -pub struct NoParams {} +pub struct Empty {} + +pub(crate) trait OrEmpty {} +impl OrEmpty for T {} +impl OrEmpty> for Empty {} #[derive(Debug, Clone, Copy, Deserialize, Serialize, Parser)] pub enum Never {} diff --git a/rpc-toolkit/src/handler/parent.rs b/rpc-toolkit/src/handler/parent.rs index 998f6d6..f742d12 100644 --- a/rpc-toolkit/src/handler/parent.rs +++ b/rpc-toolkit/src/handler/parent.rs @@ -12,15 +12,22 @@ use yajrc::RpcError; use crate::util::{combine, Flat}; use crate::{ - AnyContext, AnyHandler, CliBindings, DynHandler, HandleAny, HandleAnyArgs, HandleArgs, Handler, - HandlerTypes, IntoContext, NoCli, NoParams, + AnyContext, AnyHandler, CliBindings, DynHandler, Empty, HandleAny, HandleAnyArgs, HandleArgs, + Handler, HandlerTypes, IntoContext, OrEmpty, }; pub trait IntoHandlers: HandlerTypes { fn into_handlers(self) -> impl IntoIterator, DynHandler)>; } -impl IntoHandlers for H { +impl IntoHandlers for H +where + H: Handler + CliBindings, + H::Params: DeserializeOwned, + H::InheritedParams: DeserializeOwned, + H::Ok: Serialize + DeserializeOwned, + RpcError: From, +{ fn into_handlers(self) -> impl IntoIterator, DynHandler)> { iter_from_ctx_and_handler( intersect_type_ids(self.contexts(), ::Context::type_ids()), @@ -89,7 +96,7 @@ impl SubcommandMap { } } -pub struct ParentHandler { +pub struct ParentHandler { _phantom: PhantomData<(Params, InheritedParams)>, pub(crate) subcommands: SubcommandMap, metadata: OrdMap<&'static str, Value>, @@ -124,152 +131,6 @@ impl std::fmt::Debug for ParentHandler { - _phantom: PhantomData<(Params, InheritedParams)>, - handler: H, - inherit: F, -} -impl Clone - for InheritanceHandler -{ - fn clone(&self) -> Self { - Self { - _phantom: PhantomData, - handler: self.handler.clone(), - inherit: self.inherit.clone(), - } - } -} -impl std::fmt::Debug - for InheritanceHandler -{ - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_tuple("InheritanceHandler") - .field(&self.handler) - .finish() - } -} -impl HandlerTypes - for InheritanceHandler -where - H: HandlerTypes, - Params: Send + Sync, - InheritedParams: Send + Sync, -{ - type Params = H::Params; - type InheritedParams = Flat; - type Ok = H::Ok; - type Err = H::Err; -} -#[async_trait::async_trait] -impl Handler for InheritanceHandler -where - Params: Send + Sync + 'static, - InheritedParams: Send + Sync + 'static, - H: Handler, - F: Fn(Params, InheritedParams) -> H::InheritedParams + Send + Sync + Clone + 'static, -{ - type Context = H::Context; - fn handle_sync( - &self, - HandleArgs { - context, - parent_method, - method, - params, - inherited_params, - raw_params, - }: HandleArgs, - ) -> Result { - self.handler.handle_sync(HandleArgs { - context, - parent_method, - method, - params, - inherited_params: (self.inherit)(inherited_params.0, inherited_params.1), - raw_params, - }) - } - async fn handle_async( - &self, - HandleArgs { - context, - parent_method, - method, - params, - inherited_params, - raw_params, - }: HandleArgs, - ) -> Result { - self.handler.handle_sync(HandleArgs { - context, - parent_method, - method, - params, - inherited_params: (self.inherit)(inherited_params.0, inherited_params.1), - raw_params, - }) - } - fn metadata( - &self, - method: VecDeque<&'static str>, - ctx_ty: TypeId, - ) -> OrdMap<&'static str, Value> { - self.handler.metadata(method, ctx_ty) - } - fn contexts(&self) -> Option> { - self.handler.contexts() - } - fn method_from_dots(&self, method: &str, ctx_ty: TypeId) -> Option> { - self.handler.method_from_dots(method, ctx_ty) - } -} - -impl CliBindings - for InheritanceHandler -where - Params: Send + Sync + 'static, - InheritedParams: Send + Sync + 'static, - H: CliBindings, - F: Fn(Params, InheritedParams) -> H::InheritedParams + Send + Sync + Clone + 'static, -{ - type Context = H::Context; - fn cli_command(&self, ctx_ty: TypeId) -> Command { - self.handler.cli_command(ctx_ty) - } - fn cli_parse( - &self, - matches: &ArgMatches, - ctx_ty: TypeId, - ) -> Result<(VecDeque<&'static str>, Value), clap::Error> { - self.handler.cli_parse(matches, ctx_ty) - } - fn cli_display( - &self, - HandleArgs { - context, - parent_method, - method, - params, - inherited_params, - raw_params, - }: HandleArgs, - result: Self::Ok, - ) -> Result<(), Self::Err> { - self.handler.cli_display( - HandleArgs { - context, - parent_method, - method, - params, - inherited_params: (self.inherit)(inherited_params.0, inherited_params.1), - raw_params, - }, - result, - ) - } -} - impl ParentHandler { fn get_contexts(&self) -> Option> { let mut set = OrdSet::new(); @@ -278,17 +139,21 @@ impl ParentHandler { } Some(set) } + #[allow(private_bounds)] pub fn subcommand(mut self, name: &'static str, handler: H) -> Self where - H: IntoHandlers>, + H: IntoHandlers, + H::InheritedParams: OrEmpty>, { self.subcommands .insert(name.into(), handler.into_handlers()); self } + #[allow(private_bounds)] pub fn root_handler(mut self, handler: H) -> Self where - H: IntoHandlers>, + H: IntoHandlers, + H::InheritedParams: OrEmpty>, { self.subcommands.insert(None, handler.into_handlers()); self @@ -488,33 +353,3 @@ where } } } -impl IntoHandlers for ParentHandler -where - Self: HandlerTypes + Handler + CliBindings, - ::Params: DeserializeOwned, - ::InheritedParams: DeserializeOwned, - ::Ok: Serialize + DeserializeOwned, - RpcError: From<::Err>, -{ - fn into_handlers(self) -> impl IntoIterator, DynHandler)> { - iter_from_ctx_and_handler( - self.contexts(), - DynHandler::WithoutCli(Arc::new(AnyHandler::new(self))), - ) - } -} -impl IntoHandlers for NoCli> -where - ParentHandler: HandlerTypes + Handler, - as HandlerTypes>::Params: DeserializeOwned, - as HandlerTypes>::InheritedParams: DeserializeOwned, - as HandlerTypes>::Ok: Serialize + DeserializeOwned, - RpcError: From< as HandlerTypes>::Err>, -{ - fn into_handlers(self) -> impl IntoIterator, DynHandler)> { - iter_from_ctx_and_handler( - self.0.contexts(), - DynHandler::WithoutCli(Arc::new(AnyHandler::new(self.0))), - ) - } -} diff --git a/rpc-toolkit/src/server/http.rs b/rpc-toolkit/src/server/http.rs index 00f092e..8eba483 100644 --- a/rpc-toolkit/src/server/http.rs +++ b/rpc-toolkit/src/server/http.rs @@ -1,10 +1,8 @@ use std::any::TypeId; -use std::task::Context; use futures::future::{join_all, BoxFuture}; use futures::FutureExt; use http::header::{CONTENT_LENGTH, CONTENT_TYPE}; -use http::request::Parts; use http_body_util::BodyExt; use hyper::body::{Bytes, Incoming}; use hyper::service::Service; @@ -16,7 +14,7 @@ use yajrc::{RpcError, RpcMethod}; use crate::server::{RpcRequest, RpcResponse, SingleOrBatchRpcRequest}; use crate::util::{internal_error, parse_error}; -use crate::{handler, HandleAny, Server}; +use crate::{HandleAny, Server}; const FALLBACK_ERROR: &str = "{\"error\":{\"code\":-32603,\"message\":\"Internal error\",\"data\":\"Failed to serialize rpc response\"}}"; diff --git a/rpc-toolkit/src/server/mod.rs b/rpc-toolkit/src/server/mod.rs index 52f13f6..fca5ccb 100644 --- a/rpc-toolkit/src/server/mod.rs +++ b/rpc-toolkit/src/server/mod.rs @@ -8,7 +8,7 @@ use imbl_value::Value; use yajrc::{RpcError, RpcMethod}; use crate::util::{invalid_request, JobRunner}; -use crate::{AnyContext, AnyHandler, HandleAny, HandleAnyArgs, IntoContext, ParentHandler}; +use crate::{AnyHandler, HandleAny, HandleAnyArgs, IntoContext, ParentHandler}; type GenericRpcMethod = yajrc::GenericRpcMethod; type RpcRequest = yajrc::RpcRequest; diff --git a/rpc-toolkit/tests/handler.rs b/rpc-toolkit/tests/handler.rs index 1415599..cc6e203 100644 --- a/rpc-toolkit/tests/handler.rs +++ b/rpc-toolkit/tests/handler.rs @@ -8,8 +8,8 @@ use clap::Parser; use futures::future::ready; use imbl_value::Value; use rpc_toolkit::{ - call_remote_socket, from_fn, from_fn_async, AnyContext, CallRemote, CliApp, Context, NoParams, - ParentHandler, Server, + call_remote_socket, from_fn, from_fn_async, AnyContext, CallRemote, CliApp, Context, Empty, + HandlerExt, ParentHandler, Server, }; use serde::{Deserialize, Serialize}; use tokio::runtime::{Handle, Runtime}; @@ -141,7 +141,7 @@ fn make_api() -> ParentHandler { .subcommand("a_hello", from_fn_async(a_hello)) .subcommand( "dondes", - ParentHandler::::new().subcommand_with_inherited_no_cli( + ParentHandler::::new().subcommand( "donde", from_fn(|c: CliContext, _: (), donde| { Ok::<_, RpcError>( @@ -151,8 +151,9 @@ fn make_api() -> ParentHandler { ) .to_string(), ) - }), - |InheritParams { donde }, _| donde, + }) + .with_inherited(|InheritParams { donde }, _| donde) + .no_cli(), ), ) .subcommand( @@ -166,21 +167,22 @@ fn make_api() -> ParentHandler { ) .to_string(), ) - }), - |id, _| id, + }) + .with_inherited(|a, _| a), ), ) .subcommand( "error", - ParentHandler::::new().root_handler_no_cli( + ParentHandler::::new().root_handler( from_fn(|c: CliContext, _, InheritParams { donde }| { Err::(RpcError { code: 1, message: "This is an example message".into(), data: None, }) - }), - |id, _| id, + }) + .with_inherited(|a, _| a) + .no_cli(), ), ) }