diff --git a/src/handler/adapters.rs b/src/handler/adapters.rs index 28986ed..04f66ec 100644 --- a/src/handler/adapters.rs +++ b/src/handler/adapters.rs @@ -253,6 +253,36 @@ where Ok(()) } } +impl CliBindings for NoDisplay +where + Context: crate::Context, + Self: HandlerTypes, + Self::Params: CommandFactory + FromArgMatches + Serialize, + Self: PrintCliResult, +{ + fn cli_command(&self) -> clap::Command { + Self::Params::command() + } + fn cli_parse( + &self, + matches: &clap::ArgMatches, + ) -> 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, + handle_args: HandlerArgsFor, + result: Self::Ok, + ) -> Result<(), Self::Err> { + self.print(handle_args, result) + } +} #[derive(Clone, Debug)] pub struct CustomDisplay { @@ -364,6 +394,36 @@ where ) } } +impl CliBindings for CustomDisplay +where + Context: crate::Context, + Self: HandlerTypes, + Self::Params: CommandFactory + FromArgMatches + Serialize, + Self: PrintCliResult, +{ + fn cli_command(&self) -> clap::Command { + Self::Params::command() + } + fn cli_parse( + &self, + matches: &clap::ArgMatches, + ) -> 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, + handle_args: HandlerArgsFor, + result: Self::Ok, + ) -> Result<(), Self::Err> { + self.print(handle_args, result) + } +} pub struct CustomDisplayFn { _phantom: PhantomData, @@ -484,6 +544,36 @@ where ) } } +impl CliBindings for CustomDisplayFn +where + Context: crate::Context, + Self: HandlerTypes, + Self::Params: CommandFactory + FromArgMatches + Serialize, + Self: PrintCliResult, +{ + fn cli_command(&self) -> clap::Command { + Self::Params::command() + } + fn cli_parse( + &self, + matches: &clap::ArgMatches, + ) -> 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, + handle_args: HandlerArgsFor, + result: Self::Ok, + ) -> Result<(), Self::Err> { + self.print(handle_args, result) + } +} pub struct RemoteCaller { _phantom: PhantomData<(Context, RemoteContext)>, @@ -503,138 +593,6 @@ impl Debug for RemoteCaller HandlerTypes for RemoteCaller -// where -// H: HandlerTypes, -// { -// type Params = H::Params; -// type InheritedParams = H::InheritedParams; -// type Ok = H::Ok; -// type Err = H::Err; -// } -// impl HandlerFor -// for RemoteCaller -// where -// Context: CallRemote, -// RemoteContext: crate::Context, -// H: HandlerFor, -// H::Params: Serialize, -// H::InheritedParams: Serialize, -// H::Ok: DeserializeOwned, -// H::Err: From, -// { -// async fn handle_async( -// &self, -// HandlerArgs { -// context, -// parent_method, -// method, -// params, -// inherited_params, -// raw_params, -// }: HandlerArgsFor, -// ) -> Result { -// self.handler -// .handle_async(HandlerArgs { -// 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> { -// Context::type_ids() -// } -// fn method_from_dots(&self, method: &str, ctx_ty: TypeId) -> Option> { -// self.handler.method_from_dots(method, ctx_ty) -// } -// } -// impl HandlerFor for RemoteCaller -// where -// Context: CallRemote, -// RemoteContext: crate::Context, -// H: HandlerFor, -// H::Params: Serialize, -// H::InheritedParams: Serialize, -// H::Ok: DeserializeOwned, -// H::Err: From, -// { -// async fn handle_async( -// &self, -// HandlerArgs { -// context, -// parent_method, -// method, -// params, -// inherited_params, -// raw_params, -// }: HandlerArgsFor, -// ) -> Result { -// let full_method = parent_method.into_iter().chain(method).collect::>(); -// match context -// .call_remote(&full_method.join("."), raw_params, Empty {}) -// .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> { -// Context::type_ids() -// } -// fn method_from_dots(&self, method: &str, ctx_ty: TypeId) -> Option> { -// self.handler.method_from_dots(method, ctx_ty) -// } -// } -// impl PrintCliResult for RemoteCaller -// where -// Context: crate::Context, -// H: PrintCliResult, -// { -// fn print( -// &self, -// HandlerArgs { -// context, -// parent_method, -// method, -// params, -// inherited_params, -// raw_params, -// }: HandlerArgsFor, -// result: Self::Ok, -// ) -> Result<(), Self::Err> { -// self.handler.print( -// HandlerArgs { -// context, -// parent_method, -// method, -// params, -// inherited_params, -// raw_params, -// }, -// result, -// ) -// } -// } impl Handler for WithContext> where