Skip to content

Commit

Permalink
matcher for pubkey with option
Browse files Browse the repository at this point in the history
  • Loading branch information
ochaloup committed Jul 4, 2023
1 parent 8fdd82f commit 970b192
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
5 changes: 2 additions & 3 deletions libs/marinade-common-cli/src/config_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,11 @@ pub fn instance_arg<'a, 'b>() -> Arg<'a, 'b> {
pub const PRINT_ONLY_ARG: ArgConstant<'static> = ArgConstant {
name: "print_only",
long: "print-only",
help: "The transaction is not executed but outputed in base64 format",
help: "Transactions are not executed against the cluster, only simulation is executed.",
};
pub fn print_only_arg<'a, 'b>() -> Arg<'a, 'b> {
Arg::with_name(PRINT_ONLY_ARG.name)
.long(PRINT_ONLY_ARG.long)
.takes_value(false)
.short("p")
.help(PRINT_ONLY_ARG.long)
.help(PRINT_ONLY_ARG.help)
}
23 changes: 17 additions & 6 deletions libs/marinade-common-cli/src/matchers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,26 @@ pub fn signer_from_path_or_default(
}
}

/// Getting pubkey from the matched name or load it from the signer data
/// Getting pubkey from the matched name or load it from the signer data, when not provided, return an error
pub fn pubkey_or_of_signer(
matches: &ArgMatches<'_>,
name: &str,
wallet_manager: &mut Option<Arc<RemoteWalletManager>>,
) -> anyhow::Result<Pubkey> {
if let Some(matched_value) = matches.value_of(name) {
pubkey_or_of_signer_optional(matches, name, wallet_manager)
.map(|pubkey| {
pubkey.ok_or_else(|| anyhow!("Value for argument '{}' was not provided", name))
})
.unwrap_or_else(Err)
}

/// Getting pubkey from the matched name or load it from the signer data, when not provided, option None is returned
pub fn pubkey_or_of_signer_optional(
matches: &ArgMatches<'_>,
name: &str,
wallet_manager: &mut Option<Arc<RemoteWalletManager>>,
) -> anyhow::Result<Option<Pubkey>> {
matches.value_of(name).map_or(Ok(None), |matched_value| {
let pubkey = if let Ok(pubkey) = Pubkey::from_str(matched_value) {
pubkey
} else {
Expand All @@ -45,10 +58,8 @@ pub fn pubkey_or_of_signer(
)
})?
};
Ok(pubkey)
} else {
Err(anyhow!("Value for argument '{}' was not provided", name))
}
Ok(Some(pubkey))
})
}

/// Looking for a set of pubkeys in the matches, and return them as a vector
Expand Down

0 comments on commit 970b192

Please sign in to comment.