Skip to content

build(deps): migrate usages of once_cell crate to standard library equivalents#8030

Merged
likhinbopanna merged 5 commits intomainfrom
migrate-once-cell-to-std
May 19, 2025
Merged

build(deps): migrate usages of once_cell crate to standard library equivalents#8030
likhinbopanna merged 5 commits intomainfrom
migrate-once-cell-to-std

Conversation

@SanchithHegde
Copy link
Member

@SanchithHegde SanchithHegde commented May 14, 2025

Type of Change

  • Bugfix
  • New feature
  • Enhancement
  • Refactoring
  • Dependency updates
  • Documentation
  • CI/CD

Description

This PR updates the usages of the once_cell crate and its types with their standard library equivalents. This PR replaces:

However, this is not a complete migration, since once_cell::sync::OnceCell::get_or_try_init() cannot be replaced with std::sync::OnceLock::get_or_try_init() as it is not available in the stable toolchain yet, as of Rust 1.86. Due to this reason, once_cell is still kept as a dependency in the common_utils, external_services and router crates, only where the get_or_try_init() method is being used.

P.S.: This code was mostly written by AI, which I've also reviewed along the way.

Motivation and Context

This PR just replaces usages of one of our dependencies to the standard library equivalents.

How did you test it?

  1. Performed sanity testing via Postman. The expected result is that there are no behavior changes due to this PR.

  2. I have run unit tests on the euclid and common_utils crates, all of them (from these two crates) passed. Some of these unit tests make use of the static variables that were migrated in this PR:

    • fn test_validate_email() {
      let result = validate_email("abc@example.com");
      assert!(result.is_ok());
      let result = validate_email("abc+123@example.com");
      assert!(result.is_ok());
      let result = validate_email("");
      assert!(result.is_err());
      }
      • This test exercises the EMAIL_REGEX static variable.
    • fn test_conflicting_assertion_detection() {
      let program_str = r#"
      default: ["stripe", "adyen"]
      stripe_first: ["stripe", "adyen"]
      {
      payment_method = wallet {
      amount > 500 & capture_method = automatic
      amount < 500 & payment_method = card
      }
      }
      "#;
      let (_, program) = ast::parser::program::<DummyOutput>(program_str).expect("Program");
      let analysis_result = analyze(program, None);
      if let Err(types::AnalysisError {
      error_type: types::AnalysisErrorType::ConflictingAssertions { key, values },
      ..
      }) = analysis_result
      {
      assert!(
      matches!(key.kind, dir::DirKeyKind::PaymentMethod),
      "Key should be payment_method"
      );
      let values: Vec<dir::DirValue> = values.into_iter().map(|v| v.value).collect();
      assert_eq!(values.len(), 2, "There should be 2 conflicting conditions");
      assert!(
      values.contains(&dirval!(PaymentMethod = Wallet)),
      "Condition should include payment_method = wallet"
      );
      assert!(
      values.contains(&dirval!(PaymentMethod = Card)),
      "Condition should include payment_method = card"
      );
      } else {
      panic!("Did not receive conflicting assertions error");
      }
      }
      #[test]
      fn test_exhaustive_negation_detection() {
      let program_str = r#"
      default: ["stripe"]
      rule_1: ["adyen"]
      {
      payment_method /= wallet {
      capture_method = manual & payment_method /= card {
      authentication_type = three_ds & payment_method /= pay_later {
      amount > 1000 & payment_method /= bank_redirect {
      payment_method /= crypto
      & payment_method /= bank_debit
      & payment_method /= bank_transfer
      & payment_method /= upi
      & payment_method /= reward
      & payment_method /= voucher
      & payment_method /= gift_card
      & payment_method /= card_redirect
      & payment_method /= real_time_payment
      }
      }
      }
      }
      }
      "#;
      let (_, program) = ast::parser::program::<DummyOutput>(program_str).expect("Program");
      let analysis_result = analyze(program, None);
      if let Err(types::AnalysisError {
      error_type: types::AnalysisErrorType::ExhaustiveNegation { key, .. },
      ..
      }) = analysis_result
      {
      assert!(
      matches!(key.kind, dir::DirKeyKind::PaymentMethod),
      "Expected key to be payment_method"
      );
      } else {
      panic!("Expected exhaustive negation error");
      }
      }
      #[test]
      fn test_negated_assertions_detection() {
      let program_str = r#"
      default: ["stripe"]
      rule_1: ["adyen"]
      {
      payment_method = wallet {
      amount > 500 {
      capture_method = automatic
      }
      amount < 501 {
      payment_method /= wallet
      }
      }
      }
      "#;
      let (_, program) = ast::parser::program::<DummyOutput>(program_str).expect("Program");
      let analysis_result = analyze(program, None);
      if let Err(types::AnalysisError {
      error_type: types::AnalysisErrorType::NegatedAssertion { value, .. },
      ..
      }) = analysis_result
      {
      assert_eq!(
      value,
      dirval!(PaymentMethod = Wallet),
      "Expected to catch payment_method = wallet as conflict"
      );
      } else {
      panic!("Expected negated assertion error");
      }
      }
      • These tests call analyze without providing a specific graph, thus using ANALYSIS_GRAPH.

    Screenshot of unit test run:

    Screenshot of unit test run

  3. I have confirmed that cache system works correctly by adding a log line in case of in-memory cache hits (this log line was added only for testing and is not included in the PR). The in-memory caches also make use of static variables and LazyLock, so their correct functioning indicates that the migration hasn't broken the cache system.

    Screenshot of log lines indicating in-memory cache hits

Checklist

  • I formatted the code cargo +nightly fmt --all
  • I addressed lints thrown by cargo clippy
  • I reviewed the submitted code
  • I added unit tests for my changes where possible

@SanchithHegde SanchithHegde added this to the May 2025 Release milestone May 14, 2025
@SanchithHegde SanchithHegde self-assigned this May 14, 2025
@SanchithHegde SanchithHegde requested review from a team as code owners May 14, 2025 15:03
@SanchithHegde SanchithHegde added A-dependencies Area: Dependencies S-waiting-on-review Status: This PR has been implemented and needs to be reviewed C-refactor Category: Refactor labels May 14, 2025
@semanticdiff-com
Copy link

semanticdiff-com bot commented May 14, 2025

Review changes with  SemanticDiff

Changed Files
File Status
  cypress-tests/cypress/e2e/configs/Payment/Noon.js  100% smaller
  cypress-tests/cypress/support/commands.js  100% smaller
  crates/router/src/connector/utils.rs  96% smaller
  crates/hyperswitch_connectors/src/utils.rs  95% smaller
  crates/router/src/core/user_role.rs  92% smaller
  crates/hyperswitch_domain_models/src/consts.rs  90% smaller
  crates/router/src/utils/currency.rs  77% smaller
  crates/cards/src/validate.rs  66% smaller
  crates/storage_impl/src/redis/cache.rs  54% smaller
  crates/router_env/src/metrics.rs  39% smaller
  crates/euclid_wasm/src/lib.rs  5% smaller
  Cargo.lock Unsupported file format
  crates/analytics/Cargo.toml Unsupported file format
  crates/analytics/src/metrics/request.rs  0% smaller
  crates/cards/Cargo.toml Unsupported file format
  crates/common_utils/src/validation.rs  0% smaller
  crates/diesel_models/src/lib.rs  0% smaller
  crates/drainer/Cargo.toml Unsupported file format
  crates/euclid/Cargo.toml Unsupported file format
  crates/euclid/src/dssa/analyzer.rs  0% smaller
  crates/euclid/src/dssa/truth.rs  0% smaller
  crates/euclid_wasm/Cargo.toml Unsupported file format
  crates/hyperswitch_connectors/Cargo.toml Unsupported file format
  crates/hyperswitch_domain_models/src/type_encryption.rs  0% smaller
  crates/hyperswitch_interfaces/Cargo.toml Unsupported file format
  crates/router/src/services/authorization/roles/predefined_roles.rs  0% smaller
  crates/router/src/types/domain/user.rs  0% smaller
  crates/router/src/types/domain/user/user_authentication_method.rs  0% smaller
  crates/router_env/Cargo.toml Unsupported file format
  crates/router_env/src/lib.rs  0% smaller
  crates/router_env/src/logger/formatter.rs  0% smaller
  crates/router_env/tests/logger.rs  0% smaller
  crates/scheduler/Cargo.toml Unsupported file format
  crates/storage_impl/Cargo.toml Unsupported file format

@hyperswitch-bot hyperswitch-bot bot requested a review from a team as a code owner May 15, 2025 15:57
Copy link
Member

@pixincreate pixincreate left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cypress, LGTM.

@likhinbopanna likhinbopanna added this pull request to the merge queue May 19, 2025
Merged via the queue into main with commit 673cf24 May 19, 2025
19 of 24 checks passed
@likhinbopanna likhinbopanna deleted the migrate-once-cell-to-std branch May 19, 2025 11:07
@SanchithHegde SanchithHegde removed the S-waiting-on-review Status: This PR has been implemented and needs to be reviewed label May 19, 2025
pixincreate added a commit that referenced this pull request May 20, 2025
…ss-typos

* 'main' of github.com:juspay/hyperswitch:
  chore(version): 2025.05.20.0
  feat(core): add all_keys_required in confirm and psync payload (#7998)
  fix(connector): [CASHTOCODE] Added supported countries and currencies (#8060)
  feat(connector): [CYBERSOURCE] add SEK currency for cybersource (#8048)
  build(deps): migrate usages of `once_cell` crate to standard library equivalents (#8030)
  feat(core): [Network Tokenization] pre network tokenization (#6873)
  feat(router): add open router integration for debit routing (#7907)
  chore(cypress): Update creds for stripe (#8069)
  chore(version): 2025.05.19.0
iemyashasvi pushed a commit that referenced this pull request May 22, 2025
…equivalents (#8030)

Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-dependencies Area: Dependencies C-refactor Category: Refactor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants