fixed output_context

This commit is contained in:
FroVolod
2023-02-24 09:55:16 +02:00
parent bdda4aecfb
commit 95d20ca7b5
4 changed files with 30 additions and 27 deletions

View File

@@ -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: &<OfflineArgs as interactive_clap::ToInteractiveClapContextScope>::InteractiveClapContextScope,
) -> Self {
Self {
scope: &<OfflineArgs as interactive_clap::ToInteractiveClapContextScope>::InteractiveClapContextScope,
) -> color_eyre::eyre::Result<Self> {
Ok(Self {
some_context_field: 42,
}
})
}
}
@@ -43,19 +43,20 @@ impl From<OfflineArgsContext> for NetworkContext {
}
}
#[derive(Debug)]
pub struct NetworkContext {
pub connection_config: Option<common::ConnectionConfig>,
}
#[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<String> {
fn input_sender_account_id(context: &NetworkContext) -> color_eyre::eyre::Result<String> {
println!("Let's use context: {:?}", context);
Ok(Text::new("What is the account ID?").prompt()?)
}

View File

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

View File

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

View File

@@ -48,18 +48,17 @@ impl InteractiveClapAttrsContext {
} else {
Some(context_dir)
};
let input_context_dir: Option<proc_macro2::TokenStream> =
if input_context_dir.is_empty() {
None
} else {
Some(input_context_dir)
};
let output_context_dir: Option<proc_macro2::TokenStream> =
if output_context_dir.is_empty() {
None
} else {
Some(output_context_dir)
};
let input_context_dir: Option<proc_macro2::TokenStream> = if input_context_dir.is_empty() {
None
} else {
Some(input_context_dir)
};
let output_context_dir: Option<proc_macro2::TokenStream> = if output_context_dir.is_empty()
{
None
} else {
Some(output_context_dir)
};
Self {
context_dir,
input_context_dir,