From bddfce703c74d392b4ca20f6cf82651f4699aef6 Mon Sep 17 00:00:00 2001 From: kkawula <57270771+kkawula@users.noreply.github.com> Date: Thu, 9 Jan 2025 22:24:06 +0100 Subject: [PATCH] Remove macro :((((((((((((((((((((((((((((((((( --- crates/sncast/src/helpers/version.rs | 38 ++++--------------- .../src/starknet_commands/account/deploy.rs | 8 ++-- .../sncast/src/starknet_commands/declare.rs | 10 ++--- crates/sncast/src/starknet_commands/deploy.rs | 9 ++--- crates/sncast/src/starknet_commands/invoke.rs | 10 ++--- .../src/starknet_commands/multicall/run.rs | 5 ++- 6 files changed, 23 insertions(+), 57 deletions(-) diff --git a/crates/sncast/src/helpers/version.rs b/crates/sncast/src/helpers/version.rs index 19fc1ae629..954770db49 100644 --- a/crates/sncast/src/helpers/version.rs +++ b/crates/sncast/src/helpers/version.rs @@ -1,34 +1,10 @@ -#[macro_export] -macro_rules! generate_version_parser { - ($enum_name:ident, $variant1:ident, $variant2:ident ) => { - use shared::print::print_as_warning; - use anyhow::Error; +use anyhow::Error; +use clap::ValueEnum; +use shared::print::print_as_warning; - impl std::str::FromStr for $enum_name { - type Err = String; +const DEPRECATION_MESSAGE: &str = "The '--version' flag is deprecated and will be removed in the future. Version 3 will become the only type of transaction available."; - fn from_str(s: &str) -> Result { - let var1 = stringify!($variant1).to_lowercase(); - let var2 = stringify!($variant2).to_lowercase(); - - match s.to_lowercase().as_str() { - v if v == var1 => Ok($enum_name::$variant1), - v if v == var2 => Ok($enum_name::$variant2), - _ => Err(format!( - "Invalid value '{}'. Possible values: {}, {}", - s, var1, var2 - )), - } - } - } - - pub fn parse_version(s: &str) -> Result<$enum_name, String> { - let deprecation_message = "The '--version' flag is deprecated and will be removed in the future. Version 3 will become the only type of transaction available."; - print_as_warning(&Error::msg(deprecation_message)); - - let parsed_enum = s.parse::<$enum_name>()?; - - Ok(parsed_enum) - } - }; +pub fn parse_version(s: &str) -> Result { + print_as_warning(&Error::msg(DEPRECATION_MESSAGE)); + T::from_str(s, true) } diff --git a/crates/sncast/src/starknet_commands/account/deploy.rs b/crates/sncast/src/starknet_commands/account/deploy.rs index e331723d78..77df40ac56 100644 --- a/crates/sncast/src/starknet_commands/account/deploy.rs +++ b/crates/sncast/src/starknet_commands/account/deploy.rs @@ -8,9 +8,11 @@ use sncast::helpers::constants::{BRAAVOS_BASE_ACCOUNT_CLASS_HASH, KEYSTORE_PASSW use sncast::helpers::error::token_not_supported_for_deployment; use sncast::helpers::fee::{FeeArgs, FeeSettings, FeeToken, PayableTransaction}; use sncast::helpers::rpc::RpcArgs; +use sncast::helpers::version::parse_version; use sncast::response::structs::InvokeResponse; + use sncast::{ - apply_optional, chain_id_to_network_name, check_account_file_exists, generate_version_parser, + apply_optional, chain_id_to_network_name, check_account_file_exists, get_account_data_from_accounts_file, get_account_data_from_keystore, get_keystore_password, handle_rpc_error, handle_wait_for_tx, impl_payable_transaction, AccountType, WaitForTx, }; @@ -38,7 +40,7 @@ pub struct Deploy { pub fee_args: FeeArgs, /// Version of the account deployment (can be inferred from fee token) - #[clap(short, long, value_parser = parse_version)] + #[clap(short, long, value_parser = parse_version::)] pub version: Option, #[clap(flatten)] @@ -51,8 +53,6 @@ pub enum AccountDeployVersion { V3, } -generate_version_parser!(AccountDeployVersion, V1, V3); - impl_payable_transaction!(Deploy, token_not_supported_for_deployment, AccountDeployVersion::V1 => FeeToken::Eth, AccountDeployVersion::V3 => FeeToken::Strk diff --git a/crates/sncast/src/starknet_commands/declare.rs b/crates/sncast/src/starknet_commands/declare.rs index f9ab8ed41f..05570dd965 100644 --- a/crates/sncast/src/starknet_commands/declare.rs +++ b/crates/sncast/src/starknet_commands/declare.rs @@ -6,14 +6,12 @@ use scarb_api::StarknetContractArtifacts; use sncast::helpers::error::token_not_supported_for_declaration; use sncast::helpers::fee::{FeeArgs, FeeSettings, FeeToken, PayableTransaction}; use sncast::helpers::rpc::RpcArgs; +use sncast::helpers::version::parse_version; use sncast::response::errors::StarknetCommandError; use sncast::response::structs::{ AlreadyDeclaredResponse, DeclareResponse, DeclareTransactionResponse, }; -use sncast::{ - apply_optional, generate_version_parser, handle_wait_for_tx, impl_payable_transaction, - ErrorData, WaitForTx, -}; +use sncast::{apply_optional, handle_wait_for_tx, impl_payable_transaction, ErrorData, WaitForTx}; use starknet::accounts::AccountError::Provider; use starknet::accounts::{ConnectedAccount, DeclarationV2, DeclarationV3}; use starknet::core::types::{DeclareTransactionResult, StarknetError}; @@ -47,7 +45,7 @@ pub struct Declare { pub package: Option, /// Version of the declaration (can be inferred from fee token) - #[clap(short, long, value_parser = parse_version)] + #[clap(short, long, value_parser = parse_version::)] pub version: Option, #[clap(flatten)] @@ -60,8 +58,6 @@ pub enum DeclareVersion { V3, } -generate_version_parser!(DeclareVersion, V2, V3); - impl_payable_transaction!(Declare, token_not_supported_for_declaration, DeclareVersion::V2 => FeeToken::Eth, DeclareVersion::V3 => FeeToken::Strk diff --git a/crates/sncast/src/starknet_commands/deploy.rs b/crates/sncast/src/starknet_commands/deploy.rs index f5c2d15bda..e36c009ebf 100644 --- a/crates/sncast/src/starknet_commands/deploy.rs +++ b/crates/sncast/src/starknet_commands/deploy.rs @@ -4,11 +4,10 @@ use conversions::IntoConv; use sncast::helpers::error::token_not_supported_for_deployment; use sncast::helpers::fee::{FeeArgs, FeeSettings, FeeToken, PayableTransaction}; use sncast::helpers::rpc::RpcArgs; +use sncast::helpers::version::parse_version; use sncast::response::errors::StarknetCommandError; use sncast::response::structs::DeployResponse; -use sncast::{ - extract_or_generate_salt, generate_version_parser, impl_payable_transaction, udc_uniqueness, -}; +use sncast::{extract_or_generate_salt, impl_payable_transaction, udc_uniqueness}; use sncast::{handle_wait_for_tx, WaitForTx}; use starknet::accounts::AccountError::Provider; use starknet::accounts::{Account, ConnectedAccount, SingleOwnerAccount}; @@ -45,7 +44,7 @@ pub struct Deploy { pub nonce: Option, /// Version of the deployment (can be inferred from fee token) - #[clap(short, long, value_parser = parse_version)] + #[clap(short, long, value_parser = parse_version::)] pub version: Option, #[clap(flatten)] @@ -70,8 +69,6 @@ pub enum DeployVersion { V3, } -generate_version_parser!(DeployVersion, V1, V3); - impl_payable_transaction!(Deploy, token_not_supported_for_deployment, DeployVersion::V1 => FeeToken::Eth, DeployVersion::V3 => FeeToken::Strk diff --git a/crates/sncast/src/starknet_commands/invoke.rs b/crates/sncast/src/starknet_commands/invoke.rs index 85f00053d5..5bc1dbc6d9 100644 --- a/crates/sncast/src/starknet_commands/invoke.rs +++ b/crates/sncast/src/starknet_commands/invoke.rs @@ -5,12 +5,10 @@ use conversions::IntoConv; use sncast::helpers::error::token_not_supported_for_invoke; use sncast::helpers::fee::{FeeArgs, FeeSettings, FeeToken, PayableTransaction}; use sncast::helpers::rpc::RpcArgs; +use sncast::helpers::version::parse_version; use sncast::response::errors::StarknetCommandError; use sncast::response::structs::InvokeResponse; -use sncast::{ - apply_optional, generate_version_parser, handle_wait_for_tx, impl_payable_transaction, - WaitForTx, -}; +use sncast::{apply_optional, handle_wait_for_tx, impl_payable_transaction, WaitForTx}; use starknet::accounts::AccountError::Provider; use starknet::accounts::{Account, ConnectedAccount, ExecutionV1, ExecutionV3, SingleOwnerAccount}; use starknet::core::types::{Call, InvokeTransactionResult}; @@ -41,7 +39,7 @@ pub struct Invoke { pub nonce: Option, /// Version of invoke (can be inferred from fee token) - #[clap(short, long, value_parser = parse_version)] + #[clap(short, long, value_parser = parse_version::)] pub version: Option, #[clap(flatten)] @@ -54,8 +52,6 @@ pub enum InvokeVersion { V3, } -generate_version_parser!(InvokeVersion, V1, V3); - impl_payable_transaction!(Invoke, token_not_supported_for_invoke, InvokeVersion::V1 => FeeToken::Eth, InvokeVersion::V3 => FeeToken::Strk diff --git a/crates/sncast/src/starknet_commands/multicall/run.rs b/crates/sncast/src/starknet_commands/multicall/run.rs index e2cc19c224..ce313e986a 100644 --- a/crates/sncast/src/starknet_commands/multicall/run.rs +++ b/crates/sncast/src/starknet_commands/multicall/run.rs @@ -1,4 +1,4 @@ -use crate::starknet_commands::invoke::{execute_calls, parse_version, InvokeVersion}; +use crate::starknet_commands::invoke::{execute_calls, InvokeVersion}; use anyhow::anyhow; use anyhow::{Context, Result}; use camino::Utf8PathBuf; @@ -8,6 +8,7 @@ use sncast::helpers::constants::UDC_ADDRESS; use sncast::helpers::error::token_not_supported_for_invoke; use sncast::helpers::fee::{FeeArgs, FeeToken, PayableTransaction}; use sncast::helpers::rpc::RpcArgs; +use sncast::helpers::version::parse_version; use sncast::response::errors::handle_starknet_command_error; use sncast::response::structs::InvokeResponse; use sncast::{extract_or_generate_salt, impl_payable_transaction, udc_uniqueness, WaitForTx}; @@ -31,7 +32,7 @@ pub struct Run { pub fee_args: FeeArgs, /// Version of invoke (can be inferred from fee token) - #[clap(short, long, value_parser = parse_version)] + #[clap(short, long, value_parser = parse_version::)] pub version: Option, #[clap(flatten)]