From 95d20ca7b54a8d208b2581537c464922ee6b266c Mon Sep 17 00:00:00 2001 From: FroVolod Date: Fri, 24 Feb 2023 09:55:16 +0200 Subject: [PATCH] fixed output_context --- examples/struct_with_context.rs | 15 ++++++------ .../methods/from_cli_for_enum.rs | 7 +++--- .../methods/from_cli_for_struct.rs | 12 ++++++---- .../methods/interactive_clap_attrs_context.rs | 23 +++++++++---------- 4 files changed, 30 insertions(+), 27 deletions(-) diff --git a/examples/struct_with_context.rs b/examples/struct_with_context.rs index d2048b1..0bbd9c5 100644 --- a/examples/struct_with_context.rs +++ b/examples/struct_with_context.rs @@ -12,7 +12,7 @@ mod common; #[derive(Debug, Clone, interactive_clap_derive::InteractiveClap)] #[interactive_clap(input_context = ())] -#[interactive_clap(output_context = OfflineArgsContext)] +#[interactive_clap(output_context = NetworkContext)] pub struct OfflineArgs { #[interactive_clap(named_arg)] ///Specify a sender @@ -27,11 +27,11 @@ pub struct OfflineArgsContext { impl OfflineArgsContext { fn from_previous_context( _previous_context: (), - _scope: &::InteractiveClapContextScope, - ) -> Self { - Self { + scope: &::InteractiveClapContextScope, + ) -> color_eyre::eyre::Result { + Ok(Self { some_context_field: 42, - } + }) } } @@ -43,19 +43,20 @@ impl From for NetworkContext { } } +#[derive(Debug)] pub struct NetworkContext { pub connection_config: Option, } #[derive(Debug, Clone, interactive_clap_derive::InteractiveClap)] -#[interactive_clap(context = OfflineArgsContext)] +#[interactive_clap(context = NetworkContext)] pub struct Sender { #[interactive_clap(skip_default_input_arg)] pub sender_account_id: String, } impl Sender { - fn input_sender_account_id(context: &OfflineArgsContext) -> color_eyre::eyre::Result { + fn input_sender_account_id(context: &NetworkContext) -> color_eyre::eyre::Result { println!("Let's use context: {:?}", context); Ok(Text::new("What is the account ID?").prompt()?) } diff --git a/interactive-clap-derive/src/derives/interactive_clap/methods/from_cli_for_enum.rs b/interactive-clap-derive/src/derives/interactive_clap/methods/from_cli_for_enum.rs index 35399d8..eb22e0f 100644 --- a/interactive-clap-derive/src/derives/interactive_clap/methods/from_cli_for_enum.rs +++ b/interactive-clap-derive/src/derives/interactive_clap/methods/from_cli_for_enum.rs @@ -27,12 +27,13 @@ pub fn from_cli_for_enum( match &interactive_clap_attrs_context.output_context_dir { - Some(_) => quote! { + Some(output_context_dir) => quote! { Some(#cli_name::#variant_ident(inner_cli_args)) => { type Alias = <#name as interactive_clap::ToInteractiveClapContextScope>::InteractiveClapContextScope; let new_context_scope = Alias::#variant_ident; - let new_context = #context_name::from_previous_context(context.clone(), &new_context_scope); - let optional_inner_args = <#ty as interactive_clap::FromCli>::from_cli(Some(inner_cli_args), new_context.into())?; + let new_context = #context_name::from_previous_context(context.clone(), &new_context_scope)?; + let output_context = #output_context_dir::from(new_context); + let optional_inner_args = <#ty as interactive_clap::FromCli>::from_cli(Some(inner_cli_args), output_context)?; if let Some(inner_args) = optional_inner_args { Ok(Some(Self::#variant_ident(inner_args,))) } else { diff --git a/interactive-clap-derive/src/derives/interactive_clap/methods/from_cli_for_struct.rs b/interactive-clap-derive/src/derives/interactive_clap/methods/from_cli_for_struct.rs index 012a82f..e532bca 100644 --- a/interactive-clap-derive/src/derives/interactive_clap/methods/from_cli_for_struct.rs +++ b/interactive-clap-derive/src/derives/interactive_clap/methods/from_cli_for_struct.rs @@ -145,16 +145,17 @@ fn field_value_named_arg( let variant_name_string = crate::helpers::snake_case_to_camel_case::snake_case_to_camel_case(ident_field.to_string()); let variant_name = &syn::Ident::new(&variant_name_string, Span::call_site()); match output_context_dir { - Some(_) => { + Some(output_context_dir) => { let context_for_struct = syn::Ident::new(&format!("{}Context", &name), Span::call_site()); quote! { let new_context = #context_for_struct::from_previous_context(context, &new_context_scope)?; + let output_context = #output_context_dir::from(new_context); let #ident_field = <#ty as interactive_clap::FromCli>::from_cli( optional_clap_variant.and_then(|clap_variant| match clap_variant.#ident_field { Some(#enum_for_clap_named_arg::#variant_name(cli_arg)) => Some(cli_arg), None => None, }), - new_context.into(), + output_context, )?; let #ident_field = if let Some(value) = #ident_field { value @@ -207,13 +208,14 @@ fn field_value_subcommand( }) .map(|_| { match output_context_dir { - Some(_) => { + Some(output_context_dir) => { let context_for_struct = syn::Ident::new(&format!("{}Context", &name), Span::call_site()); quote! { let new_context = #context_for_struct::from_previous_context(context, &new_context_scope)?; + let output_context = #output_context_dir::from(new_context); let #ident_field = match optional_clap_variant.and_then(|clap_variant| clap_variant.#ident_field) { - Some(cli_arg) => <#ty as interactive_clap::FromCli>::from_cli(Some(cli_arg), new_context.into())?, - None => #ty::choose_variant(new_context.into())?, + Some(cli_arg) => <#ty as interactive_clap::FromCli>::from_cli(Some(cli_arg), output_context)?, + None => #ty::choose_variant(output_context)?, }; let #ident_field = if let Some(value) = #ident_field { value diff --git a/interactive-clap-derive/src/derives/interactive_clap/methods/interactive_clap_attrs_context.rs b/interactive-clap-derive/src/derives/interactive_clap/methods/interactive_clap_attrs_context.rs index 42d3dcf..23b64bf 100644 --- a/interactive-clap-derive/src/derives/interactive_clap/methods/interactive_clap_attrs_context.rs +++ b/interactive-clap-derive/src/derives/interactive_clap/methods/interactive_clap_attrs_context.rs @@ -48,18 +48,17 @@ impl InteractiveClapAttrsContext { } else { Some(context_dir) }; - let input_context_dir: Option = - if input_context_dir.is_empty() { - None - } else { - Some(input_context_dir) - }; - let output_context_dir: Option = - if output_context_dir.is_empty() { - None - } else { - Some(output_context_dir) - }; + let input_context_dir: Option = if input_context_dir.is_empty() { + None + } else { + Some(input_context_dir) + }; + let output_context_dir: Option = if output_context_dir.is_empty() + { + None + } else { + Some(output_context_dir) + }; Self { context_dir, input_context_dir,