From 4e4b13f3e8a5d1ad94a59724704e6550c13e87db Mon Sep 17 00:00:00 2001 From: nutcas3 Date: Fri, 1 May 2026 00:43:54 +0300 Subject: [PATCH] refactor: Simplify validation parsing using let-chains in sync.rs and wrap unsafe env::set_var in tests - Replace nested if-let statements with let-chain pattern for VmRSS memory parsing in /proc/self/status - Replace nested if-let statements with let-chain pattern for Redis used_memory parsing - Wrap std::env::set_var calls in unsafe blocks in validation_tests.rs for MAX_BYTES_LIMIT and MAX_AMOUNT_LIMIT --- apps/charging-engine/src/monitoring/sync.rs | 20 ++++++++----------- .../charging-engine/tests/validation_tests.rs | 4 ++-- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/apps/charging-engine/src/monitoring/sync.rs b/apps/charging-engine/src/monitoring/sync.rs index 87d3f4e..fc6941f 100644 --- a/apps/charging-engine/src/monitoring/sync.rs +++ b/apps/charging-engine/src/monitoring/sync.rs @@ -221,12 +221,10 @@ impl crate::charging::ChargingEngine { // Try to read from /proc/self/status for Linux systems if let Ok(status) = fs::read_to_string("/proc/self/status") { for line in status.lines() { - if line.starts_with("VmRSS:") { - if let Some(memory_str) = line.split_whitespace().nth(1) { - if let Ok(memory_kb) = memory_str.parse::() { - return Ok(memory_kb * 1024); // Convert KB to bytes - } - } + if line.starts_with("VmRSS:") + && let Some(memory_str) = line.split_whitespace().nth(1) + && let Ok(memory_kb) = memory_str.parse::() { + return Ok(memory_kb * 1024); // Convert KB to bytes } } } @@ -254,13 +252,11 @@ impl crate::charging::ChargingEngine { // Parse memory usage from Redis info for line in info.lines() { - if line.starts_with("used_memory:") { - if let Some(memory_str) = line.split(':').nth(1) { - if let Ok(memory_bytes) = memory_str.parse::() { - return Ok(memory_bytes); - } + if line.starts_with("used_memory:") + && let Some(memory_str) = line.split(':').nth(1) + && let Ok(memory_bytes) = memory_str.parse::() { + return Ok(memory_bytes); } - } } // Fallback to estimate diff --git a/apps/charging-engine/tests/validation_tests.rs b/apps/charging-engine/tests/validation_tests.rs index 26fca74..ff32e65 100644 --- a/apps/charging-engine/tests/validation_tests.rs +++ b/apps/charging-engine/tests/validation_tests.rs @@ -12,7 +12,7 @@ fn test_validate_bytes_zero() { #[test] fn test_validate_bytes_exceeds_limit() { - std::env::set_var("MAX_BYTES_LIMIT", "1000000000000"); + unsafe { std::env::set_var("MAX_BYTES_LIMIT", "1000000000000"); } let result = validate_bytes(2_000_000_000_000); // 2TB assert!(result.is_err()); match result.unwrap_err() { @@ -75,7 +75,7 @@ fn test_validate_amount_negative() { #[test] fn test_validate_amount_exceeds_limit() { - std::env::set_var("MAX_AMOUNT_LIMIT", "10000"); + unsafe { std::env::set_var("MAX_AMOUNT_LIMIT", "10000"); } let result = validate_amount(20_000.0); assert!(result.is_err()); match result.unwrap_err() {