From 6acf4204a5a6bfc22347ce08df86034c099f6e72 Mon Sep 17 00:00:00 2001 From: Aiden McClelland Date: Fri, 22 Dec 2023 12:36:33 -0700 Subject: [PATCH] fix implementation --- rpc-toolkit/src/handler.rs | 36 +++++++++++++++++++++--------------- rpc-toolkit/tests/handler.rs | 34 ++++++++++++++++++---------------- 2 files changed, 39 insertions(+), 31 deletions(-) diff --git a/rpc-toolkit/src/handler.rs b/rpc-toolkit/src/handler.rs index b9e86f1..7187348 100644 --- a/rpc-toolkit/src/handler.rs +++ b/rpc-toolkit/src/handler.rs @@ -1,5 +1,6 @@ use std::any::TypeId; use std::collections::{BTreeMap, BTreeSet, VecDeque}; +use std::fmt::Display; use std::marker::PhantomData; use std::ops::Deref; use std::sync::Arc; @@ -98,21 +99,6 @@ pub trait PrintCliResult: Handler { ) -> Result<(), Self::Err>; } -// impl PrintCliResult for H -// where -// Context: IntoContext, -// H: Handler, -// H::Ok: Display, -// { -// fn print( -// &self, -// handle_args: HandleArgs, -// result: Self::Ok, -// ) -> Result<(), Self::Err> { -// Ok(println!("{result}")) -// } -// } - #[derive(Debug)] struct WithCliBindings { _ctx: PhantomData, @@ -906,6 +892,16 @@ impl Clone for FromFn { } } } +impl PrintCliResult for FromFn +where + Context: IntoContext, + Self: Handler, + >::Ok: Display, +{ + fn print(&self, _: HandleArgs, result: Self::Ok) -> Result<(), Self::Err> { + Ok(println!("{result}")) + } +} pub fn from_fn(function: F) -> FromFn { FromFn { @@ -935,6 +931,16 @@ impl Clone for FromFnAsync { } } } +impl PrintCliResult for FromFnAsync +where + Context: IntoContext, + Self: Handler, + >::Ok: Display, +{ + fn print(&self, _: HandleArgs, result: Self::Ok) -> Result<(), Self::Err> { + Ok(println!("{result}")) + } +} pub fn from_fn_async(function: F) -> FromFnAsync { FromFnAsync { diff --git a/rpc-toolkit/tests/handler.rs b/rpc-toolkit/tests/handler.rs index bac9e77..70025af 100644 --- a/rpc-toolkit/tests/handler.rs +++ b/rpc-toolkit/tests/handler.rs @@ -3,7 +3,7 @@ use std::path::PathBuf; use std::sync::Arc; use clap::Parser; -use rpc_toolkit::{from_fn, Context, ParentHandler}; +use rpc_toolkit::{from_fn, AnyContext, CliApp, Context, ParentHandler}; use serde::Deserialize; use tokio::runtime::{Handle, Runtime}; use tokio::sync::OnceCell; @@ -54,23 +54,25 @@ impl Context for CliContext { } } -// fn make_cli() -> CliApp { -// CliApp::new::<_, CliConfig>(|mut config| { -// config.load_rec()?; -// Ok(CliContext(Arc::new(CliContextSeed { -// host: config -// .host -// .unwrap_or_else("http://localhost:8080/rpc".parse().unwrap()), -// rt: OnceCell::new(), -// }))) -// }) -// .subcommands(make_api()) -// .subcommands(ParentHandler::new().subcommand("hello", from_fn(|| Ok("world")))); -// } +fn make_cli() -> CliApp { + CliApp::new( + |mut config: CliConfig| { + config.load_rec()?; + Ok(CliContext(Arc::new(CliContextSeed { + host: config + .host + .map(|h| h.parse().unwrap()) + .unwrap_or_else(|| "http://localhost:8080/rpc".parse().unwrap()), + rt: OnceCell::new(), + }))) + }, + make_api(), + ) +} -fn make_api() -> ParentHandler { +fn make_api() -> ParentHandler { ParentHandler::new() - .subcommand_no_cli("hello", from_fn(|_: CliContext| Ok::<_, RpcError>("world"))) + .subcommand::("hello", from_fn(|| Ok::<_, RpcError>("world".to_owned()))) } pub fn internal_error(e: impl Display) -> RpcError {