Skip to content

Commit

Permalink
review: addresses near-cli-rs#26 (comment)
Browse files Browse the repository at this point in the history
  • Loading branch information
dj8yf0μl committed Feb 10, 2025
1 parent 804e120 commit 793bc83
Show file tree
Hide file tree
Showing 11 changed files with 230 additions and 226 deletions.
27 changes: 0 additions & 27 deletions interactive-clap-derive/docs/clap_enum_for_named_arg_docstring.md

This file was deleted.

70 changes: 0 additions & 70 deletions interactive-clap-derive/docs/structs_from_cli_trait_docstring.md

This file was deleted.

44 changes: 0 additions & 44 deletions interactive-clap-derive/docs/structs_input_args_impl_docstring.md

This file was deleted.

54 changes: 0 additions & 54 deletions interactive-clap-derive/docs/structs_to_cli_trait_docstring.md

This file was deleted.

This file was deleted.

5 changes: 0 additions & 5 deletions interactive-clap-derive/src/derives/interactive_clap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,19 +177,14 @@ quote::quote! {
```
*/
pub(crate) mod structs {
#[doc = include_str!("../../../docs/structs_to_cli_trait_docstring.md")]
pub(crate) mod to_cli_trait;

#[doc = include_str!("../../../docs/structs_input_args_impl_docstring.md")]
mod input_args_impl;

#[doc = include_str!("../../../docs/structs_to_interactive_clap_context_scope_trait_docstring.md")]
mod to_interactive_clap_context_scope_trait;

#[doc = include_str!("../../../docs/structs_from_cli_trait_docstring.md")]
mod from_cli_trait;

#[doc = include_str!("../../../docs/clap_enum_for_named_arg_docstring.md")]
mod clap_for_named_arg_enum;

/// these are common field methods, reused by other [structs](super::structs) submodules
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
/*!
derive of helper enum for structs with `#[interactive_clap(named_arg)]` on fields
```rust,ignore
struct #name {
#[interactive_clap(named_arg)]
///Specify a sender
field_name: Sender,
}
```
gets transformed
=>
```rust,ignore
#[derive(Debug, Clone, clap::Parser, interactive_clap_derive::ToCliArgs)]
pub enum ClapNamedArgSenderFor#name {
///Specify a sender
FieldName(<Sender as interactive_clap::ToCli>::CliVariant),
}
impl From<Sender> for ClapNamedArgSenderFor#name {
fn from(item: Sender) -> Self {
Self::FieldName(<Sender as interactive_clap::ToCli>::CliVariant::from(item))
}
}
```
*/
use proc_macro2::Span;
use proc_macro_error::abort_call_site;
use quote::{quote, ToTokens};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,75 @@
/*!
`interactive_clap::FromCli` derive
This modules describes derive of `interactive_clap::FromCli` trait for `#name` struct,
which happens during derive of [`crate::InteractiveClap`] for `#name` struct:
The implementation combines usages of all of [super::structs::to_cli_trait], [super::structs::input_args_impl],
[super::structs::to_interactive_clap_context_scope_trait]
derive input `#name`
```rust,ignore
struct #name {
age: u64,
first_name: String,
}
```
gets transformed
=>
```rust,ignore
impl interactive_clap::FromCli for #name {
type FromCliContext = ();
type FromCliError = color_eyre::eyre::Error;
fn from_cli(
optional_clap_variant: Option<<Self as interactive_clap::ToCli>::CliVariant>,
context: Self::FromCliContext,
) -> interactive_clap::ResultFromCli<
<Self as interactive_clap::ToCli>::CliVariant,
Self::FromCliError,
>
where
Self: Sized + interactive_clap::ToCli,
{
let mut clap_variant = optional_clap_variant.clone().unwrap_or_default();
if clap_variant.age.is_none() {
clap_variant
.age = match Self::input_age(&context) {
Ok(Some(age)) => Some(age),
Ok(None) => {
return interactive_clap::ResultFromCli::Cancel(Some(clap_variant));
}
Err(err) => {
return interactive_clap::ResultFromCli::Err(Some(clap_variant), err);
}
};
}
let age = clap_variant.age.clone().expect("Unexpected error");
if clap_variant.first_name.is_none() {
clap_variant
.first_name = match Self::input_first_name(&context) {
Ok(Some(first_name)) => Some(first_name),
Ok(None) => {
return interactive_clap::ResultFromCli::Cancel(Some(clap_variant));
}
Err(err) => {
return interactive_clap::ResultFromCli::Err(Some(clap_variant), err);
}
};
}
let first_name = clap_variant.first_name.clone().expect("Unexpected error");
let new_context_scope = InteractiveClapContextScopeFor#name {
age: age.into(),
first_name: first_name.into(),
};
interactive_clap::ResultFromCli::Ok(clap_variant)
}
}
```
*/
use proc_macro2::Span;
use proc_macro_error::abort_call_site;
use quote::{quote, ToTokens};
Expand Down
Loading

0 comments on commit 793bc83

Please sign in to comment.