diff --git a/rpc-toolkit-macro-internals/src/rpc_server/build.rs b/rpc-toolkit-macro-internals/src/rpc_server/build.rs index 525e477..f8c982d 100644 --- a/rpc-toolkit-macro-internals/src/rpc_server/build.rs +++ b/rpc-toolkit-macro-internals/src/rpc_server/build.rs @@ -14,20 +14,20 @@ pub fn build(args: RpcServerArgs) -> TokenStream { ident: Ident::new("rpc_handler", command.span()), arguments, }); - let seed = args.seed; + let ctx = args.ctx; let status_fn = args .status_fn .unwrap_or_else(|| syn::parse2(quote! { |_| rpc_toolkit::hyper::StatusCode::OK }).unwrap()); quote! { { - let seed = #seed; + let ctx = #ctx; let status_fn = #status_fn; - let (builder, ctx_phantom) = rpc_toolkit::rpc_server_helpers::make_builder(seed.clone()); + let (builder, ctx_phantom) = rpc_toolkit::rpc_server_helpers::make_builder(&ctx); let make_svc = rpc_toolkit::hyper::service::make_service_fn(move |_| { - let seed = seed.clone(); + let ctx = ctx.clone(); async move { - Ok::<_, hyper::Error>(rpc_toolkit::hyper::service::service_fn(move |mut req| { - let seed = seed.clone(); + Ok::<_, rpc_toolkit::hyper::Error>(rpc_toolkit::hyper::service::service_fn(move |mut req| { + let ctx = ctx.clone(); async move { let rpc_req = rpc_toolkit::rpc_server_helpers::make_request(&mut req).await; rpc_toolkit::rpc_server_helpers::to_response( @@ -36,7 +36,7 @@ pub fn build(args: RpcServerArgs) -> TokenStream { Ok(rpc_req) => Ok(( rpc_req.id, #command( - rpc_toolkit::rpc_server_helpers::bind_type(ctx_phantom, rpc_toolkit::SeedableContext::new(seed)), + rpc_toolkit::rpc_server_helpers::bind_type(ctx_phantom, ctx), rpc_toolkit::yajrc::RpcMethod::as_str(&rpc_req.method), rpc_req.params, ) diff --git a/rpc-toolkit-macro-internals/src/rpc_server/mod.rs b/rpc-toolkit-macro-internals/src/rpc_server/mod.rs index 9377905..debe917 100644 --- a/rpc-toolkit-macro-internals/src/rpc_server/mod.rs +++ b/rpc-toolkit-macro-internals/src/rpc_server/mod.rs @@ -2,7 +2,7 @@ use syn::*; pub struct RpcServerArgs { command: Path, - seed: Expr, + ctx: Expr, status_fn: Option, } diff --git a/rpc-toolkit-macro-internals/src/rpc_server/parse.rs b/rpc-toolkit-macro-internals/src/rpc_server/parse.rs index ef9e0a8..b959298 100644 --- a/rpc-toolkit-macro-internals/src/rpc_server/parse.rs +++ b/rpc-toolkit-macro-internals/src/rpc_server/parse.rs @@ -6,7 +6,7 @@ impl Parse for RpcServerArgs { fn parse(input: ParseStream) -> Result { let command = input.parse()?; let _: token::Comma = input.parse()?; - let seed = input.parse()?; + let ctx = input.parse()?; if !input.is_empty() { let _: token::Comma = input.parse()?; } @@ -17,7 +17,7 @@ impl Parse for RpcServerArgs { }; Ok(RpcServerArgs { command, - seed, + ctx, status_fn, }) } diff --git a/rpc-toolkit-macro-internals/src/run_cli/build.rs b/rpc-toolkit-macro-internals/src/run_cli/build.rs index b33922c..3de2f62 100644 --- a/rpc-toolkit-macro-internals/src/run_cli/build.rs +++ b/rpc-toolkit-macro-internals/src/run_cli/build.rs @@ -30,17 +30,17 @@ pub fn build(args: RunCliArgs) -> TokenStream { } else { quote! { #command::build_app() } }; - let make_ctx = if let Some(make_seed) = args.make_seed { - let ident = make_seed.matches_ident; - let body = make_seed.body; + let make_ctx = if let Some(make_ctx) = args.make_ctx { + let ident = make_ctx.matches_ident; + let body = make_ctx.body; quote! { { let #ident = &rpc_toolkit_matches; - rpc_toolkit::SeedableContext::new(#body) + #body } } } else { - quote! { rpc_toolkit::SeedableContext::new(&rpc_toolkit_matches) } + quote! { &rpc_toolkit_matches } }; let exit_fn = args .exit_fn diff --git a/rpc-toolkit-macro-internals/src/run_cli/mod.rs b/rpc-toolkit-macro-internals/src/run_cli/mod.rs index 21edfc7..91ae048 100644 --- a/rpc-toolkit-macro-internals/src/run_cli/mod.rs +++ b/rpc-toolkit-macro-internals/src/run_cli/mod.rs @@ -1,6 +1,6 @@ use syn::*; -pub struct MakeSeed { +pub struct MakeCtx { matches_ident: Ident, body: Expr, } @@ -13,7 +13,7 @@ pub struct MutApp { pub struct RunCliArgs { command: Path, mut_app: Option, - make_seed: Option, + make_ctx: Option, exit_fn: Option, } diff --git a/rpc-toolkit-macro-internals/src/run_cli/parse.rs b/rpc-toolkit-macro-internals/src/run_cli/parse.rs index b693d21..82bd770 100644 --- a/rpc-toolkit-macro-internals/src/run_cli/parse.rs +++ b/rpc-toolkit-macro-internals/src/run_cli/parse.rs @@ -2,12 +2,12 @@ use syn::parse::{Parse, ParseStream}; use super::*; -impl Parse for MakeSeed { +impl Parse for MakeCtx { fn parse(input: ParseStream) -> Result { let matches_ident = input.parse()?; let _: token::FatArrow = input.parse()?; let body = input.parse()?; - Ok(MakeSeed { + Ok(MakeCtx { matches_ident, body, }) @@ -37,7 +37,7 @@ impl Parse for RunCliArgs { if !input.is_empty() { let _: token::Comma = input.parse()?; } - let make_seed = if !input.is_empty() { + let make_ctx = if !input.is_empty() { Some(input.parse()?) } else { None @@ -53,7 +53,7 @@ impl Parse for RunCliArgs { Ok(RunCliArgs { command, mut_app, - make_seed, + make_ctx, exit_fn, }) } diff --git a/rpc-toolkit/src/context.rs b/rpc-toolkit/src/context.rs index eadc21a..320c836 100644 --- a/rpc-toolkit/src/context.rs +++ b/rpc-toolkit/src/context.rs @@ -26,14 +26,4 @@ pub trait Context { } } -pub trait SeedableContext: Context { - fn new(seed: T) -> Self; -} - impl Context for () {} - -impl SeedableContext for () { - fn new(_: T) -> Self { - () - } -} diff --git a/rpc-toolkit/src/lib.rs b/rpc-toolkit/src/lib.rs index 488e00b..0091680 100644 --- a/rpc-toolkit/src/lib.rs +++ b/rpc-toolkit/src/lib.rs @@ -20,20 +20,20 @@ /// /// See also: [arg](rpc_toolkit_macro::arg), [context](rpc_toolkit_macro::context) pub use rpc_toolkit_macro::command; -/// `rpc_server!(command, seed, status_fn)` +/// `rpc_server!(command, context, status_fn)` /// - returns: [Server](hyper::Server) /// - `command`: path to an rpc command (with the `#[command]` attribute) -/// - `seed`: A seed for the [SeedableContext] of the rpc command. +/// - `context`: The [Context] for `command`. Must implement [Clone](std::clone::Clone). /// - `status_fn` (optional): a function that takes a JSON RPC error code (`i32`) and returns a [StatusCode](hyper::StatusCode) /// - default: `|_| StatusCode::OK` pub use rpc_toolkit_macro::rpc_server; -/// `run_cli!(command, app_mutator, make_seed, exit_fn)` +/// `run_cli!(command, app_mutator, make_ctx, exit_fn)` /// - this function does not return /// - `command`: path to an rpc command (with the `#[command]` attribute) /// - `app_mutator` (optional): an expression that returns a mutated app. /// - example: `app => app.arg(Arg::with_name("port").long("port"))` /// - default: `app => app` -/// - `make_seed` (optional): an expression that takes [&ArgMatches](clap::ArgMatches) and returns a seed. +/// - `make_ctx` (optional): an expression that takes [&ArgMatches](clap::ArgMatches) and returns the [Context] used by `command`. /// - example: `matches => matches.value_of("port")` /// - default: `matches => matches` /// - `exit_fn` (optional): a function that takes a JSON RPC error code (`i32`) and returns an Exit code (`i32`) @@ -41,7 +41,7 @@ pub use rpc_toolkit_macro::rpc_server; pub use rpc_toolkit_macro::run_cli; pub use {clap, hyper, reqwest, serde, serde_json, tokio, url, yajrc}; -pub use crate::context::{Context, SeedableContext}; +pub use crate::context::Context; pub mod command_helpers; mod context; diff --git a/rpc-toolkit/src/rpc_server_helpers.rs b/rpc-toolkit/src/rpc_server_helpers.rs index df20a2b..d619a01 100644 --- a/rpc-toolkit/src/rpc_server_helpers.rs +++ b/rpc-toolkit/src/rpc_server_helpers.rs @@ -10,7 +10,7 @@ use serde_json::Value; use url::Host; use yajrc::{AnyRpcMethod, GenericRpcMethod, Id, RpcError, RpcRequest, RpcResponse}; -use crate::SeedableContext; +use crate::Context; lazy_static! { #[cfg(feature = "cbor")] @@ -20,10 +20,7 @@ lazy_static! { serde_json::to_vec(&RpcResponse::>::from(yajrc::INTERNAL_ERROR)).unwrap(); } -pub fn make_builder, Seed: Clone>( - seed: Seed, -) -> (Builder, PhantomData) { - let ctx = Ctx::new(seed); +pub fn make_builder(ctx: &Ctx) -> (Builder, PhantomData) { let addr = match ctx.host() { Host::Ipv4(ip) => (ip, ctx.port()).into(), Host::Ipv6(ip) => (ip, ctx.port()).into(), diff --git a/rpc-toolkit/src/test.rs b/rpc-toolkit/src/test.rs index 0b4ca10..21d9318 100644 --- a/rpc-toolkit/src/test.rs +++ b/rpc-toolkit/src/test.rs @@ -9,8 +9,9 @@ use url::Host; use yajrc::RpcError; pub use crate as rpc_toolkit; -use crate::{command, rpc_server, Context, SeedableContext}; +use crate::{command, rpc_server, Context}; +#[derive(Debug, Clone)] pub struct AppState { seed: T, data: U, @@ -29,14 +30,6 @@ pub struct ConfigSeed { port: u16, } -impl SeedableContext> for AppState, ()> { - fn new(seed: Arc) -> Self { - AppState { - seed: seed.clone(), - data: (), - } - } -} impl Context for AppState, T> { fn host(&self) -> Host<&str> { match &self.seed.host { @@ -104,7 +97,7 @@ async fn test() { host: Host::parse("localhost").unwrap(), port: 8000, }); - let server = rpc_server!(dothething::, seed); + let server = rpc_server!(dothething::, AppState { seed, data: () }); let handle = tokio::spawn(server); let mut cmd = tokio::process::Command::new("cargo") .arg("test") @@ -158,7 +151,7 @@ fn cli_test() { port: 8000, }); dothething::cli_handler::( - SeedableContext::new(seed), + AppState { seed, data: () }, None, &matches, "".into(), @@ -175,10 +168,10 @@ fn cli_example() { 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)), - matches => Arc::new(ConfigSeed { + matches => AppState { seed: Arc::new(ConfigSeed { host: Host::parse(matches.value_of("host").unwrap_or("localhost")).unwrap(), port: matches.value_of("port").unwrap_or("8000").parse().unwrap(), - }), + }), data: () }, |code| if code < 0 { 1 } else { code } ) }