mirror of
https://github.com/Drop-OSS/interactive-clap.git
synced 2026-07-20 23:44:51 -04:00
fixed "context"
This commit is contained in:
+10
-6
@@ -22,7 +22,9 @@ pub enum Submit {
|
||||
}
|
||||
|
||||
impl Submit {
|
||||
fn choose_variant(connection_config: common::ConnectionConfig) -> color_eyre::eyre::Result<Self> {
|
||||
fn choose_variant(
|
||||
connection_config: common::ConnectionConfig,
|
||||
) -> color_eyre::eyre::Result<Self> {
|
||||
let variants = SubmitDiscriminants::iter().collect::<Vec<_>>();
|
||||
let submits = variants
|
||||
.iter()
|
||||
@@ -44,13 +46,11 @@ impl Submit {
|
||||
optional_clap_variant: Option<<Submit as interactive_clap::ToCli>::CliVariant>,
|
||||
context: common::ConnectionConfig,
|
||||
) -> color_eyre::eyre::Result<Self> {
|
||||
let submit: Option<Submit> = optional_clap_variant
|
||||
.clone();
|
||||
let submit: Option<Submit> = optional_clap_variant.clone();
|
||||
match submit {
|
||||
Some(submit) => Ok(submit),
|
||||
None => Ok(Submit::Display)
|
||||
None => Ok(Submit::Display),
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,7 +61,11 @@ impl interactive_clap::ToCli for Submit {
|
||||
fn main() {
|
||||
let mut cli_online_args = OnlineArgs::parse();
|
||||
println!("cli_online_args: {:?}", &cli_online_args);
|
||||
let online_args = OnlineArgs::from_cli(Some(cli_online_args.clone()), common::ConnectionConfig::Testnet).unwrap();
|
||||
let online_args = OnlineArgs::from_cli(
|
||||
Some(cli_online_args.clone()),
|
||||
common::ConnectionConfig::Testnet,
|
||||
)
|
||||
.unwrap();
|
||||
println!("online_args: {:?}", online_args);
|
||||
// cli_online_args = CliOnlineArgs::from(online_args);
|
||||
cli_online_args = online_args.into();
|
||||
|
||||
@@ -8,149 +8,122 @@ use quote::quote;
|
||||
|
||||
pub fn fn_choose_variant(ast: &syn::DeriveInput, variants: &syn::punctuated::Punctuated<syn::Variant, syn::token::Comma>) -> proc_macro2::TokenStream {
|
||||
let name = &ast.ident;
|
||||
let interactive_clap_attrs_context = super::interactive_clap_attrs_context::InteractiveClapAttrsContext::new(&ast);
|
||||
let command_discriminants = syn::Ident::new(&format!("{}Discriminants", name), Span::call_site());
|
||||
let cli_command = syn::Ident::new(&format!("Cli{}", name), Span::call_site());
|
||||
|
||||
let variant_ident = &variants[0].ident;
|
||||
let mut cli_variant = quote! ();
|
||||
let mut context_dir = quote! ();
|
||||
let mut input_context_dir = quote! ();
|
||||
|
||||
for attr in ast.attrs.clone() {
|
||||
if attr.path.is_ident("interactive_clap".into()) {
|
||||
for attr_token in attr.tokens.clone() {
|
||||
match attr_token {
|
||||
proc_macro2::TokenTree::Group(group) => {
|
||||
if group.stream().to_string().contains("disable_strum_discriminants").clone() {
|
||||
|
||||
match &variants[0].fields {
|
||||
syn::Fields::Unnamed(_) => {
|
||||
cli_variant = quote! {
|
||||
let cli_variant = #cli_command::#variant_ident(Default::default());
|
||||
};
|
||||
},
|
||||
syn::Fields::Unit => {
|
||||
cli_variant = quote! {
|
||||
let cli_variant = #cli_command::#variant_ident;
|
||||
};
|
||||
},
|
||||
_ => abort_call_site!("Only option `Fields::Unnamed` or `Fields::Unit` is needed")
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
if group.stream().to_string().contains("output_context") {
|
||||
continue;
|
||||
} else if group.stream().to_string().contains("input_context") {
|
||||
let group_stream = &group.stream()
|
||||
.into_iter()
|
||||
// .enumerate()
|
||||
// .filter(|&(i,_)| i != 0 || i != 1)
|
||||
// .map(|(_, v)| v)
|
||||
.collect::<Vec<_>>()[2..];
|
||||
input_context_dir = quote! {#(#group_stream)*};
|
||||
} else if group.stream().to_string().contains("context") {
|
||||
let group_stream = &group.stream()
|
||||
.into_iter()
|
||||
// .enumerate()
|
||||
// .filter(|&(i,_)| i != 0 || i != 1)
|
||||
// .map(|(_, v)| v)
|
||||
.collect::<Vec<_>>()[2..];
|
||||
context_dir = quote! {#(#group_stream)*};
|
||||
};
|
||||
}
|
||||
_ => () //abort_call_site!("Only option `TokenTree::Group` is needed")
|
||||
}
|
||||
}
|
||||
};
|
||||
if attr.path.is_ident("strum_discriminants".into()) {
|
||||
for attr_token in attr.tokens.clone() {
|
||||
match attr_token {
|
||||
proc_macro2::TokenTree::Group(group) => {
|
||||
if &group.stream().to_string() == "derive(EnumMessage, EnumIter)" {
|
||||
let doc_attrs = ast.attrs.iter()
|
||||
.filter(|attr| attr.path.is_ident("doc".into()))
|
||||
.map(|attr| {
|
||||
let mut literal_string = String::new();
|
||||
for attr_token in attr.tokens.clone() {
|
||||
match attr_token {
|
||||
proc_macro2::TokenTree::Literal(literal) => {
|
||||
literal_string = literal.to_string();
|
||||
}
|
||||
_ => () //abort_call_site!("Only option `TokenTree::Literal` is needed")
|
||||
}
|
||||
};
|
||||
literal_string
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
let literal_vec = doc_attrs.iter().map(|s| s.replace("\"", "")).collect::<Vec<_>>();
|
||||
let literal = proc_macro2::Literal::string(literal_vec.join("\n ").as_str());
|
||||
|
||||
let enum_variants = variants.iter().map(|variant| {
|
||||
let variant_ident = &variant.ident;
|
||||
|
||||
match &variant.fields {
|
||||
if !ast.attrs.is_empty() {
|
||||
for attr in ast.attrs.clone() {
|
||||
if attr.path.is_ident("interactive_clap".into()) {
|
||||
for attr_token in attr.tokens.clone() {
|
||||
match attr_token {
|
||||
proc_macro2::TokenTree::Group(group) => {
|
||||
if group.stream().to_string().contains("disable_strum_discriminants").clone() {
|
||||
match &variants[0].fields {
|
||||
syn::Fields::Unnamed(_) => {
|
||||
quote! {
|
||||
#command_discriminants::#variant_ident => #cli_command::#variant_ident(Default::default())
|
||||
}
|
||||
cli_variant = quote! {
|
||||
let cli_variant = #cli_command::#variant_ident(Default::default());
|
||||
};
|
||||
},
|
||||
syn::Fields::Unit => {
|
||||
quote! {
|
||||
#command_discriminants::#variant_ident => #cli_command::#variant_ident
|
||||
}
|
||||
cli_variant = quote! {
|
||||
let cli_variant = #cli_command::#variant_ident;
|
||||
};
|
||||
},
|
||||
_ => abort_call_site!("Only option `Fields::Unnamed` or `Fields::Unit` is needed")
|
||||
}
|
||||
|
||||
|
||||
});
|
||||
|
||||
cli_variant = quote! {
|
||||
use dialoguer::{theme::ColorfulTheme, Select};
|
||||
use strum::{EnumMessage, IntoEnumIterator};
|
||||
fn prompt_variant<T>(prompt: &str) -> T
|
||||
where
|
||||
T: IntoEnumIterator + EnumMessage,
|
||||
T: Copy + Clone,
|
||||
{
|
||||
let variants = T::iter().collect::<Vec<_>>();
|
||||
let actions = variants
|
||||
.iter()
|
||||
.map(|p| {
|
||||
p.get_message()
|
||||
.unwrap_or_else(|| "error[This entry does not have an option message!!]")
|
||||
.to_owned()
|
||||
};
|
||||
}
|
||||
_ => () //abort_call_site!("Only option `TokenTree::Group` is needed")
|
||||
}
|
||||
}
|
||||
};
|
||||
if attr.path.is_ident("strum_discriminants".into()) {
|
||||
for attr_token in attr.tokens.clone() {
|
||||
match attr_token {
|
||||
proc_macro2::TokenTree::Group(group) => {
|
||||
if &group.stream().to_string() == "derive(EnumMessage, EnumIter)" {
|
||||
let doc_attrs = ast.attrs.iter()
|
||||
.filter(|attr| attr.path.is_ident("doc".into()))
|
||||
.map(|attr| {
|
||||
let mut literal_string = String::new();
|
||||
for attr_token in attr.tokens.clone() {
|
||||
match attr_token {
|
||||
proc_macro2::TokenTree::Literal(literal) => {
|
||||
literal_string = literal.to_string();
|
||||
}
|
||||
_ => () //abort_call_site!("Only option `TokenTree::Literal` is needed")
|
||||
}
|
||||
};
|
||||
literal_string
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
let literal_vec = doc_attrs.iter().map(|s| s.replace("\"", "")).collect::<Vec<_>>();
|
||||
let literal = proc_macro2::Literal::string(literal_vec.join("\n ").as_str());
|
||||
|
||||
let enum_variants = variants.iter().map(|variant| {
|
||||
let variant_ident = &variant.ident;
|
||||
|
||||
match &variant.fields {
|
||||
syn::Fields::Unnamed(_) => {
|
||||
quote! {
|
||||
#command_discriminants::#variant_ident => #cli_command::#variant_ident(Default::default())
|
||||
}
|
||||
},
|
||||
syn::Fields::Unit => {
|
||||
quote! {
|
||||
#command_discriminants::#variant_ident => #cli_command::#variant_ident
|
||||
}
|
||||
},
|
||||
_ => abort_call_site!("Only option `Fields::Unnamed` or `Fields::Unit` is needed")
|
||||
}
|
||||
|
||||
|
||||
let selected = Select::with_theme(&ColorfulTheme::default())
|
||||
.with_prompt(prompt)
|
||||
.items(&actions)
|
||||
.default(0)
|
||||
.interact()
|
||||
.unwrap();
|
||||
|
||||
variants[selected]
|
||||
}
|
||||
let cli_variant = match prompt_variant(#literal.to_string().as_str()) {
|
||||
#( #enum_variants, )*
|
||||
};
|
||||
});
|
||||
|
||||
cli_variant = quote! {
|
||||
use dialoguer::{theme::ColorfulTheme, Select};
|
||||
use strum::{EnumMessage, IntoEnumIterator};
|
||||
fn prompt_variant<T>(prompt: &str) -> T
|
||||
where
|
||||
T: IntoEnumIterator + EnumMessage,
|
||||
T: Copy + Clone,
|
||||
{
|
||||
let variants = T::iter().collect::<Vec<_>>();
|
||||
let actions = variants
|
||||
.iter()
|
||||
.map(|p| {
|
||||
p.get_message()
|
||||
.unwrap_or_else(|| "error[This entry does not have an option message!!]")
|
||||
.to_owned()
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let selected = Select::with_theme(&ColorfulTheme::default())
|
||||
.with_prompt(prompt)
|
||||
.items(&actions)
|
||||
.default(0)
|
||||
.interact()
|
||||
.unwrap();
|
||||
|
||||
variants[selected]
|
||||
}
|
||||
let cli_variant = match prompt_variant(#literal.to_string().as_str()) {
|
||||
#( #enum_variants, )*
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
_ => () //abort_call_site!("Only option `TokenTree::Group` is needed")
|
||||
}
|
||||
_ => () //abort_call_site!("Only option `TokenTree::Group` is needed")
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
let input_context = if let true = !context_dir.is_empty() {
|
||||
context_dir
|
||||
} else {
|
||||
input_context_dir
|
||||
};
|
||||
|
||||
let input_context = interactive_clap_attrs_context.get_inpun_context_dir();
|
||||
|
||||
quote! {
|
||||
pub fn choose_variant(context: #input_context) -> color_eyre::eyre::Result<Self> {
|
||||
@@ -159,4 +132,3 @@ pub fn fn_choose_variant(ast: &syn::DeriveInput, variants: &syn::punctuated::Pun
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+29
-27
@@ -19,34 +19,36 @@ impl InteractiveClapAttrsContext {
|
||||
let mut input_context_dir = quote! ();
|
||||
let mut output_context_dir = quote! ();
|
||||
let mut is_skip_default_from_cli = false;
|
||||
for attr in ast.attrs.clone() {
|
||||
if attr.path.is_ident("interactive_clap".into()) {
|
||||
for attr_token in attr.tokens.clone() {
|
||||
match attr_token {
|
||||
proc_macro2::TokenTree::Group(group) => {
|
||||
if group.stream().to_string().contains("output_context") {
|
||||
let group_stream = &group.stream()
|
||||
.into_iter()
|
||||
.collect::<Vec<_>>()[2..];
|
||||
output_context_dir = quote! {#(#group_stream)*};
|
||||
} else if group.stream().to_string().contains("input_context") {
|
||||
let group_stream = &group.stream()
|
||||
.into_iter()
|
||||
.collect::<Vec<_>>()[2..];
|
||||
input_context_dir = quote! {#(#group_stream)*};
|
||||
} else if group.stream().to_string().contains("context") {
|
||||
let group_stream = &group.stream()
|
||||
.into_iter()
|
||||
.collect::<Vec<_>>()[2..];
|
||||
context_dir = quote! {#(#group_stream)*};
|
||||
};
|
||||
if group.stream().to_string().contains("skip_default_from_cli") {
|
||||
is_skip_default_from_cli = true;
|
||||
};
|
||||
if !ast.attrs.is_empty() {
|
||||
for attr in ast.attrs.clone() {
|
||||
if attr.path.is_ident("interactive_clap".into()) {
|
||||
for attr_token in attr.tokens.clone() {
|
||||
match attr_token {
|
||||
proc_macro2::TokenTree::Group(group) => {
|
||||
if group.stream().to_string().contains("output_context") {
|
||||
let group_stream = &group.stream()
|
||||
.into_iter()
|
||||
.collect::<Vec<_>>()[2..];
|
||||
output_context_dir = quote! {#(#group_stream)*};
|
||||
} else if group.stream().to_string().contains("input_context") {
|
||||
let group_stream = &group.stream()
|
||||
.into_iter()
|
||||
.collect::<Vec<_>>()[2..];
|
||||
input_context_dir = quote! {#(#group_stream)*};
|
||||
} else if group.stream().to_string().contains("context") {
|
||||
let group_stream = &group.stream()
|
||||
.into_iter()
|
||||
.collect::<Vec<_>>()[2..];
|
||||
context_dir = quote! {#(#group_stream)*};
|
||||
};
|
||||
if group.stream().to_string().contains("skip_default_from_cli") {
|
||||
is_skip_default_from_cli = true;
|
||||
};
|
||||
}
|
||||
_ => () //abort_call_site!("Only option `TokenTree::Group` is needed")
|
||||
}
|
||||
_ => () //abort_call_site!("Only option `TokenTree::Group` is needed")
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
};
|
||||
let context_dir: Option<proc_macro2::TokenStream> = if let true = !context_dir.is_empty() {
|
||||
@@ -80,7 +82,7 @@ impl InteractiveClapAttrsContext {
|
||||
if !context_dir.is_empty() { return context_dir };
|
||||
let input_context_dir = match self.input_context_dir {
|
||||
Some(input_context_dir) => input_context_dir,
|
||||
None => quote! ()
|
||||
None => quote! {()}
|
||||
};
|
||||
input_context_dir
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user