mirror of
https://github.com/Start9Labs/rpc-toolkit.git
synced 2026-03-26 02:11:56 +00:00
fix implementation
This commit is contained in:
@@ -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<Context: IntoContext>: Handler<Context> {
|
||||
) -> 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)]
|
||||
struct WithCliBindings<Context, H> {
|
||||
_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> {
|
||||
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> {
|
||||
FromFnAsync {
|
||||
|
||||
@@ -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<CliConfig> {
|
||||
// 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<CliContext, CliConfig> {
|
||||
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<CliContext> {
|
||||
fn make_api() -> ParentHandler {
|
||||
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 {
|
||||
|
||||
Reference in New Issue
Block a user