diff --git a/src/handler/adapters.rs b/src/handler/adapters.rs index 024f7c0..844d813 100644 --- a/src/handler/adapters.rs +++ b/src/handler/adapters.rs @@ -13,7 +13,7 @@ use yajrc::RpcError; use crate::util::{Flat, PhantomData}; use crate::{ CallRemote, CallRemoteHandler, CliBindings, DynHandler, Handler, HandlerArgs, HandlerArgsFor, - HandlerFor, HandlerTypes, OrEmpty, PrintCliResult, WithContext, + HandlerFor, HandlerTypes, LeafHandler, OrEmpty, PrintCliResult, WithContext, }; pub trait HandlerExt: HandlerFor + Sized { @@ -130,6 +130,9 @@ impl + Sized> HandlerExt(pub H); + +impl LeafHandler for NoCli {} + impl HandlerTypes for NoCli { type Params = H::Params; type InheritedParams = H::InheritedParams; @@ -221,6 +224,9 @@ where #[derive(Debug, Clone)] pub struct NoDisplay(pub H); + +impl LeafHandler for NoDisplay {} + impl HandlerTypes for NoDisplay { type Params = H::Params; type InheritedParams = H::InheritedParams; @@ -337,6 +343,9 @@ pub struct CustomDisplay { print: P, handler: H, } + +impl LeafHandler for CustomDisplay {} + impl HandlerTypes for CustomDisplay where H: HandlerTypes, @@ -488,6 +497,9 @@ pub struct CustomDisplayFn { print: F, handler: H, } + +impl LeafHandler for CustomDisplayFn {} + impl Clone for CustomDisplayFn { fn clone(&self) -> Self { Self { @@ -648,6 +660,12 @@ pub struct RemoteCaller { _phantom: PhantomData<(Context, RemoteContext)>, handler: H, } + +impl LeafHandler + for RemoteCaller +{ +} + impl Clone for RemoteCaller { fn clone(&self) -> Self { Self { @@ -694,6 +712,12 @@ pub struct InheritanceHandler { handler: H, inherit: F, } + +impl LeafHandler + for InheritanceHandler +{ +} + impl Clone for InheritanceHandler { @@ -848,6 +872,9 @@ pub struct WithAbout { handler: H, message: M, } + +impl LeafHandler for WithAbout {} + impl HandlerTypes for WithAbout where H: HandlerTypes, @@ -949,6 +976,8 @@ where #[derive(Debug, Clone)] pub struct NoTS(pub H); +impl LeafHandler for NoTS {} + impl HandlerTypes for NoTS where H: HandlerTypes, @@ -1047,6 +1076,8 @@ where #[derive(Debug, Clone)] pub struct UnknownTS(pub H); +impl LeafHandler for UnknownTS {} + impl HandlerTypes for UnknownTS where H: HandlerTypes, @@ -1058,7 +1089,7 @@ where } #[cfg(feature = "ts-rs")] -impl crate::handler::HandlerTS for UnknownTS { +impl crate::handler::HandlerTS for UnknownTS { fn type_info(&self) -> Option { Some("{_PARAMS:unknown,_RETURN:unknown}".to_string()) } @@ -1149,6 +1180,8 @@ pub struct CustomTS { pub return_ty: String, } +impl LeafHandler for CustomTS {} + impl HandlerTypes for CustomTS where H: HandlerTypes, @@ -1160,7 +1193,7 @@ where } #[cfg(feature = "ts-rs")] -impl crate::handler::HandlerTS for CustomTS { +impl crate::handler::HandlerTS for CustomTS { fn type_info(&self) -> Option { Some(format!( "{{_PARAMS:{},_RETURN:{}}}", diff --git a/src/handler/from_fn.rs b/src/handler/from_fn.rs index d1500e1..250ea34 100644 --- a/src/handler/from_fn.rs +++ b/src/handler/from_fn.rs @@ -12,7 +12,8 @@ use ts_rs::TS; use crate::util::PhantomData; use crate::{ - CliBindings, Empty, HandlerArgs, HandlerArgsFor, HandlerFor, HandlerTypes, PrintCliResult, + CliBindings, Empty, HandlerArgs, HandlerArgsFor, HandlerFor, HandlerTypes, LeafHandler, + PrintCliResult, }; pub struct FromFn { @@ -21,6 +22,8 @@ pub struct FromFn { blocking: bool, metadata: OrdMap<&'static str, Value>, } + +impl LeafHandler for FromFn {} impl FromFn { pub fn with_metadata(mut self, key: &'static str, value: Value) -> Self { self.metadata.insert(key, value); @@ -147,6 +150,8 @@ pub struct FromFnAsync { function: F, metadata: OrdMap<&'static str, Value>, } + +impl LeafHandler for FromFnAsync {} impl FromFnAsync { pub fn with_metadata(mut self, key: &'static str, value: Value) -> Self { self.metadata.insert(key, value); @@ -257,6 +262,8 @@ pub struct FromFnAsyncLocal { function: F, metadata: OrdMap<&'static str, Value>, } + +impl LeafHandler for FromFnAsyncLocal {} impl FromFnAsyncLocal { pub fn with_metadata(mut self, key: &'static str, value: Value) -> Self { self.metadata.insert(key, value); diff --git a/src/handler/mod.rs b/src/handler/mod.rs index fdf6220..9236ee9 100644 --- a/src/handler/mod.rs +++ b/src/handler/mod.rs @@ -227,6 +227,8 @@ pub trait HandlerTypes { type Err: Send + Sync; } +pub trait LeafHandler {} + pub trait HandlerTS { fn type_info(&self) -> Option; } diff --git a/src/handler/parent.rs b/src/handler/parent.rs index fb0292c..5513280 100644 --- a/src/handler/parent.rs +++ b/src/handler/parent.rs @@ -14,6 +14,8 @@ use crate::{ CliBindings, DynHandler, Empty, HandleAny, HandleAnyArgs, Handler, HandlerArgs, HandlerArgsFor, HandlerFor, HandlerRequires, HandlerTypes, WithContext, }; +#[cfg(feature = "ts-rs")] +use crate::{CustomTS, UnknownTS}; #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] pub(crate) struct Name(pub(crate) &'static str); @@ -86,6 +88,31 @@ impl ParentHandler Option { + use std::fmt::Write; + let mut res = "{".to_owned(); + res.push_str("_CHILDREN:{"); + for (name, handler) in &self.subcommands.1 { + let Some(ty) = handler.type_info() else { + continue; + }; + write!( + &mut res, + "{}:{};", + serde_json::to_string(&name.0).unwrap(), + ty, + ) + .ok()?; + } + res.push_str("};}"); + if let Some(ty) = self.subcommands.0.as_ref().and_then(|h| h.type_info()) { + write!(&mut res, "&{}", ty).ok()?; + } else { + write!(&mut res, "&{{_PARAMS:{}}}", params_ty).ok()?; + } + Some(res) + } } impl Clone for ParentHandler { fn clone(&self) -> Self { @@ -150,28 +177,29 @@ where InheritedParams: Send + Sync + 'static, { fn type_info(&self) -> Option { - use std::fmt::Write; - let mut res = "{".to_owned(); - res.push_str("_CHILDREN:{"); - for (name, handler) in &self.subcommands.1 { - let Some(ty) = handler.type_info() else { - continue; - }; - write!( - &mut res, - "{}:{};", - serde_json::to_string(&name.0).unwrap(), - ty, - ) - .ok()?; - } - res.push_str("};}"); - if let Some(ty) = self.subcommands.0.as_ref().and_then(|h| h.type_info()) { - write!(&mut res, "&{}", ty).ok()?; - } else { - write!(&mut res, "&{{_PARAMS:{}}}", Params::inline()).ok()?; - } - Some(res) + self.type_info_impl(&Params::inline()) + } +} +#[cfg(feature = "ts-rs")] +impl crate::handler::HandlerTS + for CustomTS> +where + Params: Send + Sync + 'static, + InheritedParams: Send + Sync + 'static, +{ + fn type_info(&self) -> Option { + self.handler.type_info_impl(&self.params_ty) + } +} +#[cfg(feature = "ts-rs")] +impl crate::handler::HandlerTS + for UnknownTS> +where + Params: Send + Sync + 'static, + InheritedParams: Send + Sync + 'static, +{ + fn type_info(&self) -> Option { + self.0.type_info_impl("unknown") } }