ditch SeedableContext

This commit is contained in:
Aiden McClelland
2021-04-12 16:14:21 -06:00
parent fba286e7a8
commit b9676c591a
10 changed files with 34 additions and 54 deletions

View File

@@ -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,
)

View File

@@ -2,7 +2,7 @@ use syn::*;
pub struct RpcServerArgs {
command: Path,
seed: Expr,
ctx: Expr,
status_fn: Option<Expr>,
}

View File

@@ -6,7 +6,7 @@ impl Parse for RpcServerArgs {
fn parse(input: ParseStream) -> Result<Self> {
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,
})
}

View File

@@ -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

View File

@@ -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<MutApp>,
make_seed: Option<MakeSeed>,
make_ctx: Option<MakeCtx>,
exit_fn: Option<Expr>,
}

View File

@@ -2,12 +2,12 @@ use syn::parse::{Parse, ParseStream};
use super::*;
impl Parse for MakeSeed {
impl Parse for MakeCtx {
fn parse(input: ParseStream) -> Result<Self> {
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,
})
}