diff --git a/rpc-toolkit/src/cli.rs b/rpc-toolkit/src/cli.rs index 6ba2d37..7ba0cac 100644 --- a/rpc-toolkit/src/cli.rs +++ b/rpc-toolkit/src/cli.rs @@ -84,7 +84,7 @@ impl } #[async_trait::async_trait] -pub trait CallRemote: crate::Context { +pub trait CallRemote: crate::Context { async fn call_remote(&self, method: &str, params: Value) -> Result; } @@ -207,8 +207,9 @@ where type Err = RemoteHandler::Err; } #[async_trait::async_trait] -impl Handler for CallRemoteHandler +impl Handler for CallRemoteHandler where + Context: CallRemote, RemoteHandler: Handler, RemoteHandler::Params: Serialize, RemoteHandler::InheritedParams: Serialize, @@ -237,9 +238,9 @@ where } } } -impl PrintCliResult - for CallRemoteHandler +impl PrintCliResult for CallRemoteHandler where + Context: CallRemote, RemoteHandler: PrintCliResult, RemoteHandler::Params: Serialize, RemoteHandler::InheritedParams: Serialize, diff --git a/rpc-toolkit/src/handler/adapters.rs b/rpc-toolkit/src/handler/adapters.rs index a4ea662..fc40baa 100644 --- a/rpc-toolkit/src/handler/adapters.rs +++ b/rpc-toolkit/src/handler/adapters.rs @@ -40,7 +40,7 @@ pub trait HandlerExt: Handler + Sized { ) -> InheritanceHandler where F: Fn(Params, InheritedParams) -> Self::InheritedParams; - fn with_remote_cli(self) -> RemoteCli; + fn with_call_remote(self) -> RemoteCaller; } impl HandlerExt for T { @@ -90,8 +90,8 @@ impl HandlerExt for T { inherit: f, } } - fn with_remote_cli(self) -> RemoteCli { - RemoteCli { + fn with_call_remote(self) -> RemoteCaller { + RemoteCaller { _phantom: PhantomData::new(), handler: self, } @@ -452,11 +452,11 @@ where } } -pub struct RemoteCli { +pub struct RemoteCaller { _phantom: PhantomData, handler: H, } -impl Clone for RemoteCli { +impl Clone for RemoteCaller { fn clone(&self) -> Self { Self { _phantom: PhantomData::new(), @@ -464,12 +464,12 @@ impl Clone for RemoteCli { } } } -impl Debug for RemoteCli { +impl Debug for RemoteCaller { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_tuple("RemoteCli").field(&self.handler).finish() + f.debug_tuple("RemoteCaller").field(&self.handler).finish() } } -impl HandlerTypes for RemoteCli +impl HandlerTypes for RemoteCaller where H: HandlerTypes, { @@ -479,9 +479,9 @@ where type Err = H::Err; } #[async_trait::async_trait] -impl Handler for RemoteCli +impl Handler for RemoteCaller where - Context: CallRemote, + Context: CallRemote, H: Handler, H::Params: Serialize, H::InheritedParams: Serialize, @@ -541,7 +541,7 @@ where self.handler.method_from_dots(method, ctx_ty) } } -impl PrintCliResult for RemoteCli +impl PrintCliResult for RemoteCaller where Context: IntoContext, H: PrintCliResult, diff --git a/rpc-toolkit/tests/compat.rs b/rpc-toolkit/tests/compat.rs deleted file mode 100644 index cd97121..0000000 --- a/rpc-toolkit/tests/compat.rs +++ /dev/null @@ -1,169 +0,0 @@ -use std::fmt::Display; -use std::str::FromStr; -use std::sync::Arc; - -use futures::FutureExt; -use hyper::Request; -use rpc_toolkit::clap::Arg; -use rpc_toolkit::hyper::http::Error as HttpError; -use rpc_toolkit::hyper::Response; -use rpc_toolkit::serde::{Deserialize, Serialize}; -use rpc_toolkit::url::Host; -use rpc_toolkit::yajrc::RpcError; -use rpc_toolkit::{command, Context}; - -#[derive(Debug, Clone)] -pub struct AppState(Arc); -impl From for () { - fn from(_: AppState) -> Self { - () - } -} - -#[derive(Debug)] -pub struct ConfigSeed { - host: Host, - port: u16, -} - -impl Context for AppState {} - -#[command( - about = "Does the thing", - subcommands("dothething2::", self(dothething_impl(async))) -)] -async fn dothething( - #[context] _ctx: AppState, - #[arg(short = 'a')] arg1: Option, - #[arg(short = 'b', default)] val: String, - #[arg(short = 'c', help = "I am the flag `c`!", default)] arg3: bool, - #[arg(stdin)] structured: U, -) -> Result<(Option, String, bool, U), RpcError> -where - U: Serialize + for<'a> Deserialize<'a> + FromStr + Clone + 'static, - U::Err: Display, -{ - Ok((arg1, val, arg3, structured)) -} - -async fn dothething_impl( - ctx: AppState, - parent_data: (Option, String, bool, U), -) -> Result { - Ok(format!( - "{:?}, {:?}, {}, {}, {}", - ctx, - parent_data.0, - parent_data.1, - parent_data.2, - serde_json::to_string_pretty(&parent_data.3)? - )) -} - -#[command(about = "Does the thing")] -fn dothething2( - #[parent_data] parent_data: (Option, String, bool, U), - #[arg(stdin)] structured2: U, -) -> Result -where - U: Serialize + for<'a> Deserialize<'a> + FromStr + Clone + 'static, - U::Err: Display, -{ - Ok(format!( - "{:?}, {}, {}, {}, {}", - parent_data.0, - parent_data.1, - parent_data.2, - serde_json::to_string_pretty(&parent_data.3)?, - serde_json::to_string_pretty(&structured2)?, - )) -} - -// #[tokio::test] -// async fn test_rpc() { -// use tokio::io::AsyncWriteExt; - -// let seed = Arc::new(ConfigSeed { -// host: Host::parse("localhost").unwrap(), -// port: 8000, -// }); -// let server = rpc_server!({ -// command: dothething::, -// context: AppState(seed), -// middleware: [ -// cors, -// ], -// }); -// let handle = tokio::spawn(server); -// let mut cmd = tokio::process::Command::new("cargo") -// .arg("test") -// .arg("--package") -// .arg("rpc-toolkit") -// .arg("--test") -// .arg("test") -// .arg("--") -// .arg("cli_test") -// .arg("--exact") -// .arg("--nocapture") -// .arg("--") -// // .arg("-b") -// // .arg("test") -// .arg("dothething2") -// .stdin(std::process::Stdio::piped()) -// .stdout(std::process::Stdio::piped()) -// .spawn() -// .unwrap(); -// cmd.stdin -// .take() -// .unwrap() -// .write_all(b"TEST\nHAHA") -// .await -// .unwrap(); -// let out = cmd.wait_with_output().await.unwrap(); -// assert!(out.status.success()); -// assert!(dbg!(std::str::from_utf8(&out.stdout).unwrap()) -// .contains("\nNone, test, false, \"TEST\", \"HAHA\"\n")); -// handle.abort(); -// } - -// #[test] -// fn cli_test() { -// let app = dothething::build_app(); -// let mut skip = true; -// let args = std::iter::once(std::ffi::OsString::from("cli_test")) -// .chain(std::env::args_os().into_iter().skip_while(|a| { -// if a == "--" { -// skip = false; -// return true; -// } -// skip -// })) -// .collect::>(); -// if skip { -// return; -// } -// let matches = app.get_matches_from(args); -// let seed = Arc::new(ConfigSeed { -// host: Host::parse("localhost").unwrap(), -// port: 8000, -// }); -// dothething::cli_handler::(AppState(seed), (), None, &matches, "".into(), ()) -// .unwrap(); -// } - -// #[test] -// #[ignore] -// fn cli_example() { -// run_cli! ({ -// command: dothething::, -// app: app => app -// .arg(Arg::with_name("host").long("host").short('h').takes_value(true)) -// .arg(Arg::with_name("port").long("port").short('p').takes_value(true)), -// context: matches => AppState(Arc::new(ConfigSeed { -// host: Host::parse(matches.value_of("host").unwrap_or("localhost")).unwrap(), -// port: matches.value_of("port").unwrap_or("8000").parse().unwrap(), -// })) -// }) -// } - -// //////////////////////////////////////////////// diff --git a/rpc-toolkit/tests/handler.rs b/rpc-toolkit/tests/handler.rs index c07e0c9..380294e 100644 --- a/rpc-toolkit/tests/handler.rs +++ b/rpc-toolkit/tests/handler.rs @@ -1,4 +1,3 @@ -use std::any::TypeId; use std::ffi::OsString; use std::fmt::Display; use std::path::{Path, PathBuf}; @@ -62,7 +61,7 @@ impl Context for CliContext { } } #[async_trait::async_trait] -impl CallRemote for CliContext { +impl CallRemote for CliContext { async fn call_remote(&self, method: &str, params: Value) -> Result { call_remote_socket( tokio::net::UnixStream::connect(&self.0.host).await.unwrap(), @@ -130,7 +129,7 @@ fn make_api() -> ParentHandler { )) }, ) - .with_remote_cli::(), + .with_call_remote::(), ) .subcommand( "hello", @@ -174,7 +173,7 @@ fn make_api() -> ParentHandler { .subcommand( "error", ParentHandler::::new().root_handler( - from_fn(|c: CliContext, _: Empty, InheritParams { donde }| { + from_fn(|_: CliContext, _: Empty, InheritParams { .. }| { Err::(RpcError { code: 1, message: "This is an example message".into(), @@ -199,14 +198,14 @@ fn test_cli() { make_cli() .run( ["test-cli", "hello", "me"] - .into_iter() + .iter() .map(|s| OsString::from(s)), ) .unwrap(); make_cli() .run( ["test-cli", "fizz", "buzz"] - .into_iter() + .iter() .map(|s| OsString::from(s)), ) .unwrap(); @@ -230,7 +229,7 @@ async fn test_server() { "echo", "foo", ] - .into_iter() + .iter() .map(|s| OsString::from(s)), ) .unwrap(); @@ -242,7 +241,7 @@ async fn test_server() { "echo", "bar", ] - .into_iter() + .iter() .map(|s| OsString::from(s)), ) .unwrap();