fix implementation

This commit is contained in:
Aiden McClelland
2023-12-22 12:36:33 -07:00
parent c8bd32ba97
commit 6acf4204a5
2 changed files with 39 additions and 31 deletions

View File

@@ -1,5 +1,6 @@
use std::any::TypeId; use std::any::TypeId;
use std::collections::{BTreeMap, BTreeSet, VecDeque}; use std::collections::{BTreeMap, BTreeSet, VecDeque};
use std::fmt::Display;
use std::marker::PhantomData; use std::marker::PhantomData;
use std::ops::Deref; use std::ops::Deref;
use std::sync::Arc; use std::sync::Arc;
@@ -98,21 +99,6 @@ pub trait PrintCliResult<Context: IntoContext>: Handler<Context> {
) -> Result<(), Self::Err>; ) -> Result<(), Self::Err>;
} }
// impl<Context, H> PrintCliResult<Context> for H
// where
// Context: IntoContext,
// H: Handler<Context>,
// H::Ok: Display,
// {
// fn print(
// &self,
// handle_args: HandleArgs<Context, Self>,
// result: Self::Ok,
// ) -> Result<(), Self::Err> {
// Ok(println!("{result}"))
// }
// }
#[derive(Debug)] #[derive(Debug)]
struct WithCliBindings<Context, H> { struct WithCliBindings<Context, H> {
_ctx: PhantomData<Context>, _ctx: PhantomData<Context>,
@@ -906,6 +892,16 @@ impl<F: Clone, T, E, Args> Clone for FromFn<F, T, E, Args> {
} }
} }
} }
impl<Context, F, T, E, Args> PrintCliResult<Context> for FromFn<F, T, E, Args>
where
Context: IntoContext,
Self: Handler<Context>,
<Self as Handler<Context>>::Ok: Display,
{
fn print(&self, _: HandleArgs<Context, Self>, result: Self::Ok) -> Result<(), Self::Err> {
Ok(println!("{result}"))
}
}
pub fn from_fn<F, T, E, Args>(function: F) -> FromFn<F, T, E, Args> { pub fn from_fn<F, T, E, Args>(function: F) -> FromFn<F, T, E, Args> {
FromFn { FromFn {
@@ -935,6 +931,16 @@ impl<F: Clone, Fut, T, E, Args> Clone for FromFnAsync<F, Fut, T, E, Args> {
} }
} }
} }
impl<Context, F, Fut, T, E, Args> PrintCliResult<Context> for FromFnAsync<F, Fut, T, E, Args>
where
Context: IntoContext,
Self: Handler<Context>,
<Self as Handler<Context>>::Ok: Display,
{
fn print(&self, _: HandleArgs<Context, Self>, result: Self::Ok) -> Result<(), Self::Err> {
Ok(println!("{result}"))
}
}
pub fn from_fn_async<F, Fut, T, E, Args>(function: F) -> FromFnAsync<F, Fut, T, E, Args> { pub fn from_fn_async<F, Fut, T, E, Args>(function: F) -> FromFnAsync<F, Fut, T, E, Args> {
FromFnAsync { FromFnAsync {

View File

@@ -3,7 +3,7 @@ use std::path::PathBuf;
use std::sync::Arc; use std::sync::Arc;
use clap::Parser; use clap::Parser;
use rpc_toolkit::{from_fn, Context, ParentHandler}; use rpc_toolkit::{from_fn, AnyContext, CliApp, Context, ParentHandler};
use serde::Deserialize; use serde::Deserialize;
use tokio::runtime::{Handle, Runtime}; use tokio::runtime::{Handle, Runtime};
use tokio::sync::OnceCell; use tokio::sync::OnceCell;
@@ -54,23 +54,25 @@ impl Context for CliContext {
} }
} }
// fn make_cli() -> CliApp<CliConfig> { fn make_cli() -> CliApp<CliContext, CliConfig> {
// CliApp::new::<_, CliConfig>(|mut config| { CliApp::new(
// config.load_rec()?; |mut config: CliConfig| {
// Ok(CliContext(Arc::new(CliContextSeed { config.load_rec()?;
// host: config Ok(CliContext(Arc::new(CliContextSeed {
// .host host: config
// .unwrap_or_else("http://localhost:8080/rpc".parse().unwrap()), .host
// rt: OnceCell::new(), .map(|h| h.parse().unwrap())
// }))) .unwrap_or_else(|| "http://localhost:8080/rpc".parse().unwrap()),
// }) rt: OnceCell::new(),
// .subcommands(make_api()) })))
// .subcommands(ParentHandler::new().subcommand("hello", from_fn(|| Ok("world")))); },
// } make_api(),
)
}
fn make_api() -> ParentHandler<CliContext> { fn make_api() -> ParentHandler {
ParentHandler::new() ParentHandler::new()
.subcommand_no_cli("hello", from_fn(|_: CliContext| Ok::<_, RpcError>("world"))) .subcommand::<AnyContext, _>("hello", from_fn(|| Ok::<_, RpcError>("world".to_owned())))
} }
pub fn internal_error(e: impl Display) -> RpcError { pub fn internal_error(e: impl Display) -> RpcError {