fixed from_cli_arg

This commit is contained in:
FroVolod
2023-02-19 21:22:23 +02:00
parent 0151a59442
commit 9cc2279697
2 changed files with 37 additions and 23 deletions

View File

@@ -106,21 +106,13 @@ fn fields_value(field: &syn::Field) -> proc_macro2::TokenStream {
},
_ => String::new(),
};
if let "Option" = type_string.as_str() {
quote! {
let #ident_field = optional_clap_variant
.as_ref()
.and_then(|clap_variant| clap_variant.#ident_field.clone());
}
} else {
quote! {
let #ident_field = Self::#fn_from_cli_arg(
optional_clap_variant
.clone()
.and_then(|clap_variant| clap_variant.#ident_field),
&context,
)?;
}
quote! {
let #ident_field = Self::#fn_from_cli_arg(
optional_clap_variant
.clone()
.and_then(|clap_variant| clap_variant.#ident_field),
&context,
)?;
}
} else {
quote!()

View File

@@ -60,14 +60,36 @@ pub fn from_cli_arg(ast: &syn::DeriveInput, fields: &syn::Fields) -> Vec<proc_ma
let cli_field_type = super::cli_field_type::cli_field_type(ty);
let fn_input_arg =
syn::Ident::new(&format!("input_{}", &ident_field), Span::call_site());
quote! {
fn #fn_from_cli_arg(
#optional_cli_field_name: #cli_field_type,
context: &#input_context_dir,
) -> color_eyre::eyre::Result<#ty> {
match #optional_cli_field_name {
Some(#ident_field) => Ok(#ident_field),
None => Self::#fn_input_arg(&context),
let type_string = match &ty {
syn::Type::Path(type_path) => match type_path.path.segments.last() {
Some(path_segment) => path_segment.ident.to_string(),
_ => String::new(),
},
_ => String::new(),
};
if let "Option" = type_string.as_str() {
quote! {
fn #fn_from_cli_arg(
#optional_cli_field_name: #cli_field_type,
context: &#input_context_dir,
) -> color_eyre::eyre::Result<#ty> {
match #optional_cli_field_name {
Some(#ident_field) => Ok(Some(#ident_field)),
None => Self::#fn_input_arg(&context),
}
}
}
} else {
quote! {
fn #fn_from_cli_arg(
#optional_cli_field_name: #cli_field_type,
context: &#input_context_dir,
) -> color_eyre::eyre::Result<#ty> {
match #optional_cli_field_name {
Some(#ident_field) => Ok(#ident_field),
None => Self::#fn_input_arg(&context),
}
}
}
}