mirror of
https://github.com/Drop-OSS/interactive-clap.git
synced 2026-07-19 20:03:34 -04:00
fixed fn is_field_without_skip_default_from_cli()
This commit is contained in:
-36
@@ -1,36 +0,0 @@
|
||||
extern crate proc_macro;
|
||||
|
||||
use proc_macro_error::abort_call_site;
|
||||
use syn;
|
||||
use quote::quote;
|
||||
|
||||
pub fn field_with_skip_default_from_cli(field: &syn::Field) -> proc_macro2::TokenStream {
|
||||
let ident_field = &field.clone().ident.expect("this field does not exist");
|
||||
if field.attrs.is_empty() {
|
||||
quote! ()
|
||||
} else {
|
||||
match field.attrs.iter()
|
||||
.filter(|attr| attr.path.is_ident("interactive_clap".into()))
|
||||
.map(|attr| attr.tokens.clone())
|
||||
.flatten()
|
||||
.filter(|attr_token| {
|
||||
match attr_token {
|
||||
proc_macro2::TokenTree::Group(group) => {
|
||||
if group.stream().to_string().contains("skip_default_from_cli") {
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
},
|
||||
_ => abort_call_site!("Only option `TokenTree::Group` is needed")
|
||||
}
|
||||
})
|
||||
.map(|_| {
|
||||
quote! {#ident_field}
|
||||
})
|
||||
.next() {
|
||||
Some(token_stream) => token_stream,
|
||||
None => quote! ()
|
||||
}
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
extern crate proc_macro;
|
||||
|
||||
use proc_macro_error::abort_call_site;
|
||||
use quote::quote;
|
||||
use syn;
|
||||
|
||||
pub fn is_field_without_skip_default_from_cli(field: &syn::Field) -> bool {
|
||||
if field.attrs.is_empty() {
|
||||
return true;
|
||||
}
|
||||
match field
|
||||
.attrs
|
||||
.iter()
|
||||
.filter(|attr| attr.path.is_ident("interactive_clap".into()))
|
||||
.map(|attr| attr.tokens.clone())
|
||||
.flatten()
|
||||
.filter(|attr_token| match attr_token {
|
||||
proc_macro2::TokenTree::Group(group) => {
|
||||
if group.stream().to_string().contains("skip_default_from_cli") {
|
||||
false
|
||||
} else {
|
||||
true
|
||||
}
|
||||
}
|
||||
_ => abort_call_site!("Only option `TokenTree::Group` is needed"),
|
||||
})
|
||||
.next()
|
||||
{
|
||||
Some(token_stream) => true,
|
||||
None => false,
|
||||
}
|
||||
}
|
||||
+11
-7
@@ -21,18 +21,22 @@ pub fn from_cli_arg(ast: &syn::DeriveInput, fields: &syn::Fields) -> Vec<proc_ma
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let fields_with_skip_default_from_cli = fields.iter().map(|field| {
|
||||
super::fields_with_skip_default_from_cli::field_with_skip_default_from_cli(field)
|
||||
})
|
||||
.filter(|token_stream| !token_stream.is_empty())
|
||||
.collect::<Vec<_>>();
|
||||
let fields_without_skip_default_from_cli = fields.iter()
|
||||
.filter(|field| {
|
||||
super::fields_without_skip_default_from_cli::is_field_without_skip_default_from_cli(field)
|
||||
})
|
||||
.map(|field| {
|
||||
let ident_field = &field.clone().ident.expect("this field does not exist");
|
||||
quote! {#ident_field}
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let get_arg_for_fields = fields.iter().map(|field| {
|
||||
let ident_field = &field.clone().ident.expect("this field does not exist");
|
||||
let ty = &field.ty;
|
||||
let fields_without_subcommand_to_string = fields_without_subcommand.iter().map(|token_stream| token_stream.to_string()).collect::<Vec<_>>();
|
||||
let fields_with_skip_default_from_cli_to_string = fields_with_skip_default_from_cli.iter().map(|token_stream| token_stream.to_string()).collect::<Vec<_>>();
|
||||
if fields_without_subcommand_to_string.contains(&ident_field.to_string()) & !fields_with_skip_default_from_cli_to_string.contains(&ident_field.to_string()) {
|
||||
let fields_without_skip_default_from_cli_to_string = fields_without_skip_default_from_cli.iter().map(|token_stream| token_stream.to_string()).collect::<Vec<_>>();
|
||||
if fields_without_subcommand_to_string.contains(&ident_field.to_string()) & fields_without_skip_default_from_cli_to_string.contains(&ident_field.to_string()) {
|
||||
let fn_from_cli_arg = syn::Ident::new(&format!("from_cli_{}", &ident_field), Span::call_site());
|
||||
let optional_cli_field_name = syn::Ident::new(&format!("optional_cli_{}", ident_field), Span::call_site());
|
||||
let input_context_dir = interactive_clap_attrs_context.clone().get_input_context_dir();
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
pub mod choose_variant;
|
||||
pub mod cli_field_type;
|
||||
pub mod fields_with_skip_default_from_cli;
|
||||
pub mod fields_without_skip_default_from_cli;
|
||||
pub mod fields_without_subcommand;
|
||||
pub mod from_cli_for_enum;
|
||||
pub mod from_cli_for_struct;
|
||||
|
||||
Reference in New Issue
Block a user