Skip to content

Commit

Permalink
Fix typo on pricing_mode and define pricing_mode ARG_VALUE_NAME optio…
Browse files Browse the repository at this point in the history
…ns (#176)

* Fix typo on pricing_mode and define pricing_mode ARG_VALUE_NAME options

* Bad commit

* get PricingMode values for string match
  • Loading branch information
gRoussac authored Jul 19, 2024
1 parent 8e5e6a8 commit 3adc5f3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/cli/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub fn create_transaction(
error: "pricing_mode is required to be non empty".to_string(),
});
}
let pricing_mode = if transaction_params.payment_amount.to_lowercase().as_str() == "reserved" {
let pricing_mode = if transaction_params.pricing_mode.to_lowercase().as_str() == "reserved" {
let digest = Digest::from_hex(transaction_params.receipt).map_err(|error| {
CliError::FailedToParseDigest {
context: "pricing_digest",
Expand Down
55 changes: 49 additions & 6 deletions src/transaction/creation_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,25 +433,68 @@ pub(super) mod transfer_amount {

pub(super) mod pricing_mode {
use super::*;
use clap::{builder::PossibleValue, value_parser, ValueEnum};
use std::str::FromStr;

pub(in crate::transaction) const ARG_NAME: &str = "pricing-mode";

const ARG_VALUE_NAME: &str = common::ARG_STRING;
const ARG_VALUE_NAME: &str = "classic|reserved|fixed";
const ARG_HELP: &str = "Used to identify the payment mode chosen to execute the transaction";
const ARG_DEFAULT: &str = PricingMode::FIXED;

pub(in crate::transaction) fn arg() -> Arg {
Arg::new(ARG_NAME)
.long(ARG_NAME)
.required(false)
.value_name(ARG_VALUE_NAME)
.default_value(ARG_DEFAULT)
.help(ARG_HELP)
.display_order(DisplayOrder::PricingMode as usize)
.value_parser(value_parser!(PricingMode))
}

pub fn get(matches: &ArgMatches) -> &str {
matches
.get_one::<String>(ARG_NAME)
.map(String::as_str)
.unwrap_or_default()
#[derive(Debug, Clone, Copy)]
pub(super) enum PricingMode {
Classic,
Reserved,
Fixed,
}

impl PricingMode {
const CLASSIC: &'static str = "classic";
const RESERVED: &'static str = "reserved";
const FIXED: &'static str = "fixed";
}

impl ValueEnum for PricingMode {
fn value_variants<'a>() -> &'a [Self] {
&[Self::Classic, Self::Reserved, Self::Fixed]
}

fn to_possible_value(&self) -> Option<PossibleValue> {
Some(match self {
Self::Classic => PossibleValue::new(PricingMode::CLASSIC),
Self::Reserved => PossibleValue::new(PricingMode::RESERVED),
Self::Fixed => PossibleValue::new(PricingMode::FIXED),
})
}
}

impl FromStr for PricingMode {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
PricingMode::CLASSIC => Ok(Self::Classic),
PricingMode::RESERVED => Ok(Self::Reserved),
PricingMode::FIXED => Ok(Self::Fixed),
_ => Err(format!("'{}' is not a valid pricing option", s)),
}
}
}

pub fn get(matches: &ArgMatches) -> Option<&PricingMode> {
matches.get_one(ARG_NAME)
}
}

Expand Down

0 comments on commit 3adc5f3

Please sign in to comment.