Skip to content

Commit

Permalink
refactor: use strum::EnumString implementation for connector name c…
Browse files Browse the repository at this point in the history
…onversions (#1052)

Co-authored-by: Sanchith Hegde <[email protected]>
  • Loading branch information
nachiketkanore and SanchithHegde authored May 19, 2023
1 parent cf00059 commit 2809425
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 53 deletions.
5 changes: 0 additions & 5 deletions crates/api_models/src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,6 @@ pub enum MandateStatus {
Clone,
Copy,
Debug,
Default,
Eq,
PartialEq,
ToSchema,
Expand All @@ -586,16 +585,13 @@ pub enum Connector {
Aci,
Adyen,
Airwallex,
Applepay,
Authorizedotnet,
Bitpay,
Bluesnap,
Braintree,
Checkout,
Coinbase,
Cybersource,
#[default]
Dummy,
Iatapay,
#[cfg(feature = "dummy_connector")]
#[serde(rename = "dummyconnector1")]
Expand Down Expand Up @@ -632,7 +628,6 @@ pub enum Connector {
Worldline,
Worldpay,
Zen,
Signifyd,
}

impl Connector {
Expand Down
128 changes: 86 additions & 42 deletions crates/router/src/types/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,50 +199,94 @@ impl ConnectorData {
_connectors: &Connectors,
connector_name: &str,
) -> CustomResult<BoxedConnector, errors::ApiErrorResponse> {
match connector_name {
"aci" => Ok(Box::new(&connector::Aci)),
"adyen" => Ok(Box::new(&connector::Adyen)),
"airwallex" => Ok(Box::new(&connector::Airwallex)),
"authorizedotnet" => Ok(Box::new(&connector::Authorizedotnet)),
"bambora" => Ok(Box::new(&connector::Bambora)),
"bitpay" => Ok(Box::new(&connector::Bitpay)),
"bluesnap" => Ok(Box::new(&connector::Bluesnap)),
"braintree" => Ok(Box::new(&connector::Braintree)),
"checkout" => Ok(Box::new(&connector::Checkout)),
"coinbase" => Ok(Box::new(&connector::Coinbase)),
"cybersource" => Ok(Box::new(&connector::Cybersource)),
"dlocal" => Ok(Box::new(&connector::Dlocal)),
#[cfg(feature = "dummy_connector")]
"dummyconnector1" => Ok(Box::new(&connector::DummyConnector::<1>)),
#[cfg(feature = "dummy_connector")]
"dummyconnector2" => Ok(Box::new(&connector::DummyConnector::<2>)),
#[cfg(feature = "dummy_connector")]
"dummyconnector3" => Ok(Box::new(&connector::DummyConnector::<3>)),
"fiserv" => Ok(Box::new(&connector::Fiserv)),
"forte" => Ok(Box::new(&connector::Forte)),
"globalpay" => Ok(Box::new(&connector::Globalpay)),
"iatapay" => Ok(Box::new(&connector::Iatapay)),
"klarna" => Ok(Box::new(&connector::Klarna)),
"mollie" => Ok(Box::new(&connector::Mollie)),
"nmi" => Ok(Box::new(&connector::Nmi)),
// "noon" => Ok(Box::new(&connector::Noon)), added as template code for future usage
"nuvei" => Ok(Box::new(&connector::Nuvei)),
"opennode" => Ok(Box::new(&connector::Opennode)),
// "payeezy" => Ok(Box::new(&connector::Payeezy)), As psync and rsync are not supported by this connector, it is added as template code for future usage
"payu" => Ok(Box::new(&connector::Payu)),
"rapyd" => Ok(Box::new(&connector::Rapyd)),
"shift4" => Ok(Box::new(&connector::Shift4)),
"stripe" => Ok(Box::new(&connector::Stripe)),
"worldline" => Ok(Box::new(&connector::Worldline)),
"worldpay" => Ok(Box::new(&connector::Worldpay)),
"multisafepay" => Ok(Box::new(&connector::Multisafepay)),
"nexinets" => Ok(Box::new(&connector::Nexinets)),
"paypal" => Ok(Box::new(&connector::Paypal)),
"trustpay" => Ok(Box::new(&connector::Trustpay)),
"zen" => Ok(Box::new(&connector::Zen)),
_ => Err(report!(errors::ConnectorError::InvalidConnectorName)
match enums::Connector::from_str(connector_name) {
Ok(name) => match name {
enums::Connector::Aci => Ok(Box::new(&connector::Aci)),
enums::Connector::Adyen => Ok(Box::new(&connector::Adyen)),
enums::Connector::Airwallex => Ok(Box::new(&connector::Airwallex)),
enums::Connector::Authorizedotnet => Ok(Box::new(&connector::Authorizedotnet)),
enums::Connector::Bambora => Ok(Box::new(&connector::Bambora)),
enums::Connector::Bitpay => Ok(Box::new(&connector::Bitpay)),
enums::Connector::Bluesnap => Ok(Box::new(&connector::Bluesnap)),
enums::Connector::Braintree => Ok(Box::new(&connector::Braintree)),
enums::Connector::Checkout => Ok(Box::new(&connector::Checkout)),
enums::Connector::Coinbase => Ok(Box::new(&connector::Coinbase)),
enums::Connector::Cybersource => Ok(Box::new(&connector::Cybersource)),
enums::Connector::Dlocal => Ok(Box::new(&connector::Dlocal)),
#[cfg(feature = "dummy_connector")]
enums::Connector::DummyConnector1 => Ok(Box::new(&connector::DummyConnector::<1>)),
#[cfg(feature = "dummy_connector")]
enums::Connector::DummyConnector2 => Ok(Box::new(&connector::DummyConnector::<2>)),
#[cfg(feature = "dummy_connector")]
enums::Connector::DummyConnector3 => Ok(Box::new(&connector::DummyConnector::<3>)),
enums::Connector::Fiserv => Ok(Box::new(&connector::Fiserv)),
enums::Connector::Forte => Ok(Box::new(&connector::Forte)),
enums::Connector::Globalpay => Ok(Box::new(&connector::Globalpay)),
enums::Connector::Iatapay => Ok(Box::new(&connector::Iatapay)),
enums::Connector::Klarna => Ok(Box::new(&connector::Klarna)),
enums::Connector::Mollie => Ok(Box::new(&connector::Mollie)),
enums::Connector::Nmi => Ok(Box::new(&connector::Nmi)),
// "noon" => Ok(Box::new(&connector::Noon)), added as template code for future usage
enums::Connector::Nuvei => Ok(Box::new(&connector::Nuvei)),
enums::Connector::Opennode => Ok(Box::new(&connector::Opennode)),
// "payeezy" => Ok(Box::new(&connector::Payeezy)), As psync and rsync are not supported by this connector, it is added as template code for future usage
enums::Connector::Payu => Ok(Box::new(&connector::Payu)),
enums::Connector::Rapyd => Ok(Box::new(&connector::Rapyd)),
enums::Connector::Shift4 => Ok(Box::new(&connector::Shift4)),
enums::Connector::Stripe => Ok(Box::new(&connector::Stripe)),
enums::Connector::Worldline => Ok(Box::new(&connector::Worldline)),
enums::Connector::Worldpay => Ok(Box::new(&connector::Worldpay)),
enums::Connector::Multisafepay => Ok(Box::new(&connector::Multisafepay)),
enums::Connector::Nexinets => Ok(Box::new(&connector::Nexinets)),
enums::Connector::Paypal => Ok(Box::new(&connector::Paypal)),
enums::Connector::Trustpay => Ok(Box::new(&connector::Trustpay)),
enums::Connector::Zen => Ok(Box::new(&connector::Zen)),
},
Err(_) => Err(report!(errors::ConnectorError::InvalidConnectorName)
.attach_printable(format!("invalid connector name: {connector_name}")))
.change_context(errors::ApiErrorResponse::InternalServerError),
}
}
}

#[cfg(test)]
mod test {
#![allow(clippy::unwrap_used)]
use super::*;

#[test]
fn test_convert_connector_parsing_success() {
let result = enums::Connector::from_str("aci");
assert!(result.is_ok());
assert_eq!(result.unwrap(), enums::Connector::Aci);

let result = enums::Connector::from_str("shift4");
assert!(result.is_ok());
assert_eq!(result.unwrap(), enums::Connector::Shift4);

let result = enums::Connector::from_str("authorizedotnet");
assert!(result.is_ok());
assert_eq!(result.unwrap(), enums::Connector::Authorizedotnet);
}

#[test]
fn test_convert_connector_parsing_fail_for_unknown_type() {
let result = enums::Connector::from_str("unknowntype");
assert!(result.is_err());

let result = enums::Connector::from_str("randomstring");
assert!(result.is_err());
}

#[test]
fn test_convert_connector_parsing_fail_for_camel_case() {
let result = enums::Connector::from_str("Paypal");
assert!(result.is_err());

let result = enums::Connector::from_str("Authorizedotnet");
assert!(result.is_err());

let result = enums::Connector::from_str("Opennode");
assert!(result.is_err());
}
}
9 changes: 5 additions & 4 deletions crates/router/src/types/storage/payment_attempt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub struct RoutingData {
impl crate::utils::storage_partitioning::KvStorePartition for PaymentAttempt {}

#[cfg(test)]
#[cfg(feature = "dummy_connector")]
mod tests {
#![allow(clippy::expect_used, clippy::unwrap_used)]
use tokio::sync::oneshot;
Expand All @@ -34,7 +35,7 @@ mod tests {

let payment_id = Uuid::new_v4().to_string();
let current_time = common_utils::date_time::now();
let connector = types::Connector::Dummy.to_string();
let connector = types::Connector::DummyConnector1.to_string();
let payment_attempt = PaymentAttemptNew {
payment_id: payment_id.clone(),
connector: Some(connector),
Expand Down Expand Up @@ -66,7 +67,7 @@ mod tests {
let payment_id = Uuid::new_v4().to_string();
let attempt_id = Uuid::new_v4().to_string();
let merchant_id = Uuid::new_v4().to_string();
let connector = types::Connector::Dummy.to_string();
let connector = types::Connector::DummyConnector1.to_string();

let payment_attempt = PaymentAttemptNew {
payment_id: payment_id.clone(),
Expand Down Expand Up @@ -105,11 +106,11 @@ mod tests {
async fn test_payment_attempt_mandate_field() {
use crate::configs::settings::Settings;
let conf = Settings::new().expect("invalid settings");
let uuid = uuid::Uuid::new_v4().to_string();
let uuid = Uuid::new_v4().to_string();
let tx: oneshot::Sender<()> = oneshot::channel().0;
let state = routes::AppState::with_storage(conf, StorageImpl::PostgresqlTest, tx).await;
let current_time = common_utils::date_time::now();
let connector = types::Connector::Dummy.to_string();
let connector = types::Connector::DummyConnector1.to_string();

let payment_attempt = PaymentAttemptNew {
payment_id: uuid.clone(),
Expand Down
2 changes: 1 addition & 1 deletion crates/router/tests/connectors/noon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl utils::Connector for NoonTest {
use router::connector::Noon;
types::api::ConnectorData {
connector: Box::new(&Noon),
connector_name: types::Connector::Dummy,
connector_name: types::Connector::DummyConnector1,
get_token: types::api::GetToken::Connector,
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/router/tests/connectors/payeezy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl utils::Connector for PayeezyTest {
use router::connector::Payeezy;
types::api::ConnectorData {
connector: Box::new(&Payeezy),
connector_name: types::Connector::Dummy,
connector_name: types::Connector::DummyConnector1,
get_token: types::api::GetToken::Connector,
}
}
Expand Down

0 comments on commit 2809425

Please sign in to comment.