Skip to content

Commit

Permalink
refactor: Avoid unnecessary "interactive_clap::FromCli" implementatio…
Browse files Browse the repository at this point in the history
…ns (#288)
  • Loading branch information
FroVolod authored Jan 16, 2024
1 parent 253ac00 commit a89d570
Show file tree
Hide file tree
Showing 24 changed files with 63 additions and 1,179 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ near-token = { version = "0.2.0", features = [
] }

keyring = "2.0.5"
interactive-clap = "0.2.7"
interactive-clap-derive = "0.2.7"
interactive-clap = "0.2.8"
interactive-clap-derive = "0.2.8"

near-socialdb-client = "0.2.1"

Expand Down
75 changes: 1 addition & 74 deletions src/commands/account/add_key/access_key_type/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ impl From<FullAccessTypeContext> for AccessTypeContext {

#[derive(Debug, Clone, interactive_clap::InteractiveClap)]
#[interactive_clap(input_context = super::AddKeyCommandContext)]
#[interactive_clap(output_context = AccessTypeContext)]
#[interactive_clap(skip_default_from_cli)]
#[interactive_clap(output_context = FunctionCallTypeContext)]
pub struct FunctionCallType {
#[interactive_clap(long)]
#[interactive_clap(skip_default_input_arg)]
Expand Down Expand Up @@ -105,78 +104,6 @@ impl From<FunctionCallTypeContext> for AccessTypeContext {
}
}

impl interactive_clap::FromCli for FunctionCallType {
type FromCliContext = super::AddKeyCommandContext;
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.unwrap_or_default();

if clap_variant.allowance.is_none() {
clap_variant.allowance = match Self::input_allowance(&context) {
Ok(optional_allowance) => optional_allowance,
Err(err) => return interactive_clap::ResultFromCli::Err(Some(clap_variant), err),
};
}
let allowance = clap_variant.allowance;
if clap_variant.receiver_account_id.is_none() {
clap_variant.receiver_account_id = match Self::input_receiver_account_id(&context) {
Ok(Some(first_receiver_account_id)) => Some(first_receiver_account_id),
Ok(None) => return interactive_clap::ResultFromCli::Cancel(Some(clap_variant)),
Err(err) => return interactive_clap::ResultFromCli::Err(Some(clap_variant), err),
};
}
let receiver_account_id = clap_variant
.receiver_account_id
.clone()
.expect("Unexpected error");
if clap_variant.method_names.is_none() {
clap_variant.method_names = match Self::input_method_names(&context) {
Ok(Some(first_method_names)) => Some(first_method_names),
Ok(None) => return interactive_clap::ResultFromCli::Cancel(Some(clap_variant)),
Err(err) => return interactive_clap::ResultFromCli::Err(Some(clap_variant), err),
};
}
let method_names = clap_variant.method_names.clone().expect("Unexpected error");

let new_context_scope = InteractiveClapContextScopeForFunctionCallType {
allowance,
receiver_account_id,
method_names,
};
let new_context =
match FunctionCallTypeContext::from_previous_context(context, &new_context_scope) {
Ok(new_context) => new_context,
Err(err) => return interactive_clap::ResultFromCli::Err(Some(clap_variant), err),
};
let output_context = AccessTypeContext::from(new_context);

match super::AccessKeyMode::from_cli(clap_variant.access_key_mode.take(), output_context) {
interactive_clap::ResultFromCli::Ok(cli_access_key_mode) => {
clap_variant.access_key_mode = Some(cli_access_key_mode);
interactive_clap::ResultFromCli::Ok(clap_variant)
}
interactive_clap::ResultFromCli::Cancel(optional_cli_access_key_mode) => {
clap_variant.access_key_mode = optional_cli_access_key_mode;
interactive_clap::ResultFromCli::Cancel(Some(clap_variant))
}
interactive_clap::ResultFromCli::Back => interactive_clap::ResultFromCli::Back,
interactive_clap::ResultFromCli::Err(optional_cli_access_key_mode, err) => {
clap_variant.access_key_mode = optional_cli_access_key_mode;
interactive_clap::ResultFromCli::Err(Some(clap_variant), err)
}
}
}
}

impl FunctionCallType {
pub fn input_method_names(
_context: &super::AddKeyCommandContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::str::FromStr;
#[interactive_clap(input_context = super::access_key_type::AccessTypeContext)]
#[interactive_clap(output_context = AddAccessWithSeedPhraseActionContext)]
pub struct AddAccessWithSeedPhraseAction {
/// Enter the seed-phrase for this sub-account:
/// Enter the seed-phrase:
master_seed_phrase: String,
#[interactive_clap(named_arg)]
/// Select network
Expand Down
2 changes: 1 addition & 1 deletion src/commands/account/add_key/use_public_key/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#[interactive_clap(input_context = super::access_key_type::AccessTypeContext)]
#[interactive_clap(output_context = AddAccessKeyActionContext)]
pub struct AddAccessKeyAction {
/// Enter the public key for this account:
/// Enter the public key:
public_key: crate::types::public_key::PublicKey,
#[interactive_clap(named_arg)]
/// Select network
Expand Down
41 changes: 13 additions & 28 deletions src/commands/account/create_account/create_implicit_account/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,41 +37,26 @@ pub enum Mode {
}

#[derive(Debug, Clone, interactive_clap::InteractiveClap)]
#[interactive_clap(context = SaveImplicitAccountContext)]
#[interactive_clap(skip_default_from_cli)]
#[interactive_clap(input_context = SaveImplicitAccountContext)]
#[interactive_clap(output_context = SaveToFolderContext)]
pub struct SaveToFolder {
#[interactive_clap(skip_default_input_arg)]
/// Where to save the implicit account file?
folder_path: crate::types::path_buf::PathBuf,
}

impl interactive_clap::FromCli for SaveToFolder {
type FromCliContext = SaveImplicitAccountContext;
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.unwrap_or_default();
if clap_variant.folder_path.is_none() {
clap_variant.folder_path = match Self::input_folder_path(&context) {
Ok(Some(folder_path)) => Some(folder_path),
Ok(None) => return interactive_clap::ResultFromCli::Cancel(Some(clap_variant)),
Err(err) => return interactive_clap::ResultFromCli::Err(Some(clap_variant), err),
};
};
let folder_path = clap_variant.folder_path.clone().expect("Unexpected error");
#[derive(Clone)]
struct SaveToFolderContext;

match (context.on_after_getting_folder_path_callback)(&folder_path.into()) {
Ok(_) => interactive_clap::ResultFromCli::Ok(clap_variant),
Err(err) => interactive_clap::ResultFromCli::Err(Some(clap_variant), err),
}
impl SaveToFolderContext {
pub fn from_previous_context(
previous_context: SaveImplicitAccountContext,
scope: &<SaveToFolder as interactive_clap::ToInteractiveClapContextScope>::InteractiveClapContextScope,
) -> color_eyre::eyre::Result<Self> {
(previous_context.on_after_getting_folder_path_callback)(
&scope.folder_path.clone().into(),
)?;
Ok(Self)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ use std::collections::HashMap;
use inquire::{CustomType, Select, Text};

#[derive(Debug, Clone, interactive_clap::InteractiveClap)]
#[interactive_clap(input_context = super::super::UpdateAccountProfileContext)]
#[interactive_clap(input_context = super::super::UpdateSocialProfileContext)]
#[interactive_clap(output_context = ManuallyContext)]
#[interactive_clap(skip_default_from_cli)]
pub struct Manually {
#[interactive_clap(long)]
#[interactive_clap(skip_default_input_arg)]
Expand Down Expand Up @@ -123,146 +122,6 @@ impl From<ManuallyContext> for super::ArgsContext {
}
}

impl interactive_clap::FromCli for Manually {
type FromCliContext = super::super::UpdateSocialProfileContext;
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.unwrap_or_default();
if clap_variant.name.is_none() {
clap_variant.name = match Self::input_name(&context) {
Ok(optional_name) => optional_name,
Err(err) => return interactive_clap::ResultFromCli::Err(Some(clap_variant), err),
};
};
let name = clap_variant.name.clone();
if clap_variant.image_url.is_none() {
clap_variant.image_url = match Self::input_image_url(&context) {
Ok(optional_image_url) => optional_image_url,
Err(err) => return interactive_clap::ResultFromCli::Err(Some(clap_variant), err),
};
};
let image_url = clap_variant.image_url.clone();
if clap_variant.image_ipfs_cid.is_none() {
clap_variant.image_ipfs_cid = match Self::input_image_ipfs_cid(&context) {
Ok(optional_image_ipfs_cid) => optional_image_ipfs_cid,
Err(err) => return interactive_clap::ResultFromCli::Err(Some(clap_variant), err),
};
};
let image_ipfs_cid = clap_variant.image_ipfs_cid.clone();
if clap_variant.background_image_url.is_none() {
clap_variant.background_image_url = match Self::input_background_image_url(&context) {
Ok(optional_background_image_url) => optional_background_image_url,
Err(err) => return interactive_clap::ResultFromCli::Err(Some(clap_variant), err),
};
};
let background_image_url = clap_variant.background_image_url.clone();
if clap_variant.background_image_ipfs_cid.is_none() {
clap_variant.background_image_ipfs_cid =
match Self::input_background_image_ipfs_cid(&context) {
Ok(optional_background_image_ipfs_cid) => optional_background_image_ipfs_cid,
Err(err) => {
return interactive_clap::ResultFromCli::Err(Some(clap_variant), err)
}
};
};
let background_image_ipfs_cid = clap_variant.background_image_ipfs_cid.clone();
if clap_variant.description.is_none() {
clap_variant.description = match Self::input_description(&context) {
Ok(optional_description) => optional_description,
Err(err) => return interactive_clap::ResultFromCli::Err(Some(clap_variant), err),
};
};
let description = clap_variant.description.clone();
if clap_variant.twitter.is_none() {
clap_variant.twitter = match Self::input_twitter(&context) {
Ok(optional_twitter) => optional_twitter,
Err(err) => return interactive_clap::ResultFromCli::Err(Some(clap_variant), err),
};
};
let twitter = clap_variant.twitter.clone();
if clap_variant.github.is_none() {
clap_variant.github = match Self::input_github(&context) {
Ok(optional_github) => optional_github,
Err(err) => return interactive_clap::ResultFromCli::Err(Some(clap_variant), err),
};
};
let github = clap_variant.github.clone();
if clap_variant.telegram.is_none() {
clap_variant.telegram = match Self::input_telegram(&context) {
Ok(optional_telegram) => optional_telegram,
Err(err) => return interactive_clap::ResultFromCli::Err(Some(clap_variant), err),
};
};
let telegram = clap_variant.telegram.clone();
if clap_variant.website.is_none() {
clap_variant.website = match Self::input_website(&context) {
Ok(optional_website) => optional_website,
Err(err) => return interactive_clap::ResultFromCli::Err(Some(clap_variant), err),
};
};
let website = clap_variant.website.clone();
if clap_variant.tags.is_none() {
clap_variant.tags = match Self::input_tags(&context) {
Ok(optional_tags) => optional_tags,
Err(err) => return interactive_clap::ResultFromCli::Err(Some(clap_variant), err),
};
};
let tags = clap_variant.tags.clone();
let new_context_scope = InteractiveClapContextScopeForManually {
name,
image_url,
image_ipfs_cid,
background_image_url,
background_image_ipfs_cid,
description,
twitter,
github,
telegram,
website,
tags,
};
let output_context =
match ManuallyContext::from_previous_context(context, &new_context_scope) {
Ok(new_context) => new_context,
Err(err) => return interactive_clap::ResultFromCli::Err(Some(clap_variant), err),
};
let context = output_context;
let optional_field = clap_variant
.sign_as
.take()
.map(|ClapNamedArgSignerForManually::SignAs(cli_arg)| cli_arg);
match <super::super::sign_as::Signer as interactive_clap::FromCli>::from_cli(
optional_field,
context.into(),
) {
interactive_clap::ResultFromCli::Ok(cli_field) => {
clap_variant.sign_as = Some(ClapNamedArgSignerForManually::SignAs(cli_field));
}
interactive_clap::ResultFromCli::Cancel(optional_cli_field) => {
clap_variant.sign_as =
optional_cli_field.map(ClapNamedArgSignerForManually::SignAs);
return interactive_clap::ResultFromCli::Cancel(Some(clap_variant));
}
interactive_clap::ResultFromCli::Back => return interactive_clap::ResultFromCli::Back,
interactive_clap::ResultFromCli::Err(optional_cli_field, err) => {
clap_variant.sign_as =
optional_cli_field.map(ClapNamedArgSignerForManually::SignAs);
return interactive_clap::ResultFromCli::Err(Some(clap_variant), err);
}
};
interactive_clap::ResultFromCli::Ok(clap_variant)
}
}

impl Manually {
fn input_name(
_context: &super::super::UpdateSocialProfileContext,
Expand Down
Loading

0 comments on commit a89d570

Please sign in to comment.