From d5e81b760d7155b47d5b940f353f7958daf14042 Mon Sep 17 00:00:00 2001 From: J H Date: Fri, 22 Dec 2023 12:56:09 -0700 Subject: [PATCH] wip --- rpc-toolkit/tests/handler.rs | 44 +++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/rpc-toolkit/tests/handler.rs b/rpc-toolkit/tests/handler.rs index 70025af..b45937c 100644 --- a/rpc-toolkit/tests/handler.rs +++ b/rpc-toolkit/tests/handler.rs @@ -3,8 +3,8 @@ use std::path::PathBuf; use std::sync::Arc; use clap::Parser; -use rpc_toolkit::{from_fn, AnyContext, CliApp, Context, ParentHandler}; -use serde::Deserialize; +use rpc_toolkit::{from_fn, from_fn_async, AnyContext, CliApp, Context, ParentHandler}; +use serde::{Deserialize, Serialize}; use tokio::runtime::{Handle, Runtime}; use tokio::sync::OnceCell; use url::Url; @@ -71,8 +71,46 @@ fn make_cli() -> CliApp { } fn make_api() -> ParentHandler { + impl CliContext { + fn host(&self) -> &Url { + &self.0.host + } + } + async fn a_hello(_: CliContext) -> Result { + Ok::<_, RpcError>("Async Subcommand".to_string()) + } + #[derive(Debug, Clone, Deserialize, Serialize, Parser)] + struct HelloParams { + whom: String, + } ParentHandler::new() - .subcommand::("hello", from_fn(|| Ok::<_, RpcError>("world".to_owned()))) + .subcommand( + "echo", + ParentHandler::new() + .subcommand_no_cli( + "echo_no_cli", + from_fn(|c: CliContext| { + Ok::<_, RpcError>( + format!("Subcommand No Cli: Host {host}", host = c.host()).to_string(), + ) + }), + ) + .subcommand_no_cli( + "echo_cli", + from_fn(|c: CliContext| { + Ok::<_, RpcError>( + format!("Subcommand Cli: Host {host}", host = c.host()).to_string(), + ) + }), + ), + ) + .subcommand( + "hello", + from_fn(|_: CliContext, HelloParams { whom }: HelloParams| { + Ok::<_, RpcError>(format!("Hello {whom}").to_string()) + }), + ) + .subcommand("a_hello", from_fn_async(a_hello)) } pub fn internal_error(e: impl Display) -> RpcError {