diff --git a/crates/mbr-core/src/display/display_options.rs b/crates/mbr-core/src/display/display_options.rs index 101b9e3..223b10c 100644 --- a/crates/mbr-core/src/display/display_options.rs +++ b/crates/mbr-core/src/display/display_options.rs @@ -93,16 +93,16 @@ impl DisplayOptions { /// Set options from environment variables pub fn from_env() -> Self { let mut options = Self { - no_color: crate::utils::input::EnvConfigReader::read_no_color(), - no_fullscreen: crate::utils::input::EnvConfigReader::read_no_fullscreen(), + no_color: crate::utils::validation::EnvConfigReader::read_no_color(), + no_fullscreen: crate::utils::validation::EnvConfigReader::read_no_fullscreen(), ..Default::default() }; - if let Some(page_size) = crate::utils::input::EnvConfigReader::read_page_size() { + if let Some(page_size) = crate::utils::validation::EnvConfigReader::read_page_size() { options.page_size = page_size; } - if let Some(memory_mb) = crate::utils::input::EnvConfigReader::read_max_memory() { + if let Some(memory_mb) = crate::utils::validation::EnvConfigReader::read_max_memory() { options.max_memory_mb = memory_mb; } diff --git a/crates/mbr-core/src/utils/data.rs b/crates/mbr-core/src/utils/data.rs index bb38bb0..0288b6b 100644 --- a/crates/mbr-core/src/utils/data.rs +++ b/crates/mbr-core/src/utils/data.rs @@ -1,6 +1,28 @@ use crate::api::models::{QueryData, QueryResult}; use crate::error::AppError; +// === Formatting Utilities === + +/// Format bytes into human-readable string (B, KB, MB, GB, TB) +pub fn format_bytes(bytes: u64) -> String { + const UNITS: &[&str] = &["B", "KB", "MB", "GB", "TB"]; + let mut size = bytes as f64; + let mut unit_index = 0; + + while size >= 1024.0 && unit_index < UNITS.len() - 1 { + size /= 1024.0; + unit_index += 1; + } + + if unit_index == 0 { + format!("{} {}", bytes, UNITS[unit_index]) + } else { + format!("{:.1} {}", size, UNITS[unit_index]) + } +} + +// === Offset Management === + /// Manages offset for --offset option pagination #[derive(Debug, Clone)] pub struct OffsetManager { @@ -135,4 +157,14 @@ mod tests { .is_err() ); } + + #[test] + fn test_format_bytes() { + assert_eq!(format_bytes(0), "0 B"); + assert_eq!(format_bytes(512), "512 B"); + assert_eq!(format_bytes(1024), "1.0 KB"); + assert_eq!(format_bytes(1536), "1.5 KB"); + assert_eq!(format_bytes(1048576), "1.0 MB"); + assert_eq!(format_bytes(1073741824), "1.0 GB"); + } } diff --git a/crates/mbr-core/src/utils/helpers.rs b/crates/mbr-core/src/utils/helpers.rs deleted file mode 100644 index 1e6d7ba..0000000 --- a/crates/mbr-core/src/utils/helpers.rs +++ /dev/null @@ -1,31 +0,0 @@ -pub fn format_bytes(bytes: u64) -> String { - const UNITS: &[&str] = &["B", "KB", "MB", "GB", "TB"]; - let mut size = bytes as f64; - let mut unit_index = 0; - - while size >= 1024.0 && unit_index < UNITS.len() - 1 { - size /= 1024.0; - unit_index += 1; - } - - if unit_index == 0 { - format!("{} {}", bytes, UNITS[unit_index]) - } else { - format!("{:.1} {}", size, UNITS[unit_index]) - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_format_bytes() { - assert_eq!(format_bytes(0), "0 B"); - assert_eq!(format_bytes(512), "512 B"); - assert_eq!(format_bytes(1024), "1.0 KB"); - assert_eq!(format_bytes(1536), "1.5 KB"); - assert_eq!(format_bytes(1048576), "1.0 MB"); - assert_eq!(format_bytes(1073741824), "1.0 GB"); - } -} diff --git a/crates/mbr-core/src/utils/input.rs b/crates/mbr-core/src/utils/input.rs deleted file mode 100644 index ff2bf95..0000000 --- a/crates/mbr-core/src/utils/input.rs +++ /dev/null @@ -1,85 +0,0 @@ -//! Input processing and configuration utilities -//! -//! This module provides utilities for processing user input, environment variables, -//! and configuration values that are used across the application. - -/// Environment variable configuration reader -pub struct EnvConfigReader; - -impl EnvConfigReader { - /// Read NO_COLOR environment variable - pub fn read_no_color() -> bool { - std::env::var("NO_COLOR").is_ok() - } - - /// Read MBR_PAGE_SIZE environment variable - pub fn read_page_size() -> Option { - std::env::var("MBR_PAGE_SIZE") - .ok() - .and_then(|s| s.parse::().ok()) - } - - /// Read MBR_NO_FULLSCREEN environment variable - pub fn read_no_fullscreen() -> bool { - std::env::var("MBR_NO_FULLSCREEN").is_ok() - } - - /// Read MBR_MAX_MEMORY environment variable - pub fn read_max_memory() -> Option { - std::env::var("MBR_MAX_MEMORY") - .ok() - .and_then(|s| s.parse::().ok()) - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_read_no_color_default() { - // Without setting environment variable, should return false - // Note: This test might be affected by actual environment - let _result = EnvConfigReader::read_no_color(); - // Function exists and returns a boolean value - } - - #[test] - fn test_read_page_size_with_invalid_value() { - // Test with invalid value handling - unsafe { - std::env::set_var("MBR_PAGE_SIZE", "invalid"); - } - let result = EnvConfigReader::read_page_size(); - assert!(result.is_none()); - unsafe { - std::env::remove_var("MBR_PAGE_SIZE"); - } - } - - #[test] - fn test_read_page_size_with_valid_value() { - // Test with valid value - unsafe { - std::env::set_var("MBR_PAGE_SIZE", "50"); - } - let result = EnvConfigReader::read_page_size(); - assert_eq!(result, Some(50)); - unsafe { - std::env::remove_var("MBR_PAGE_SIZE"); - } - } - - #[test] - fn test_read_max_memory_with_valid_value() { - // Test with valid memory value - unsafe { - std::env::set_var("MBR_MAX_MEMORY", "1024"); - } - let result = EnvConfigReader::read_max_memory(); - assert_eq!(result, Some(1024)); - unsafe { - std::env::remove_var("MBR_MAX_MEMORY"); - } - } -} diff --git a/crates/mbr-core/src/utils/mod.rs b/crates/mbr-core/src/utils/mod.rs index 5076a4c..3a8dd38 100644 --- a/crates/mbr-core/src/utils/mod.rs +++ b/crates/mbr-core/src/utils/mod.rs @@ -1,10 +1,14 @@ pub mod data; pub mod error_helpers; pub mod file; -pub mod helpers; -pub mod input; pub mod logging; pub mod memory; pub mod retry; pub mod text; pub mod validation; + +// Re-exports for backward compatibility (deprecated, will be removed) +#[deprecated(note = "Use utils::data::format_bytes instead")] +pub use data::format_bytes; +#[deprecated(note = "Use utils::validation::EnvConfigReader instead")] +pub use validation::EnvConfigReader; diff --git a/crates/mbr-core/src/utils/validation.rs b/crates/mbr-core/src/utils/validation.rs index da70368..601a541 100644 --- a/crates/mbr-core/src/utils/validation.rs +++ b/crates/mbr-core/src/utils/validation.rs @@ -1,10 +1,45 @@ -//! Input validation and sanitization utilities +//! Input validation, sanitization, and environment configuration utilities //! -//! This module provides utilities for validating and sanitizing user input, -//! configuration values, and API parameters. +//! This module provides utilities for: +//! - Validating and sanitizing user input +//! - Reading environment variables for configuration +//! - API parameter validation use crate::error::CliError; +// === Environment Configuration === + +/// Environment variable configuration reader +pub struct EnvConfigReader; + +impl EnvConfigReader { + /// Read NO_COLOR environment variable + pub fn read_no_color() -> bool { + std::env::var("NO_COLOR").is_ok() + } + + /// Read MBR_PAGE_SIZE environment variable + pub fn read_page_size() -> Option { + std::env::var("MBR_PAGE_SIZE") + .ok() + .and_then(|s| s.parse::().ok()) + } + + /// Read MBR_NO_FULLSCREEN environment variable + pub fn read_no_fullscreen() -> bool { + std::env::var("MBR_NO_FULLSCREEN").is_ok() + } + + /// Read MBR_MAX_MEMORY environment variable + pub fn read_max_memory() -> Option { + std::env::var("MBR_MAX_MEMORY") + .ok() + .and_then(|s| s.parse::().ok()) + } +} + +// === Input Validation === + /// Validate that a URL is properly formatted pub fn validate_url(url: &str) -> crate::Result<()> { if url.is_empty() { @@ -122,4 +157,53 @@ mod tests { assert!(validate_email("user@domain").is_err()); assert!(validate_email("user@domain@com").is_err()); } + + // === EnvConfigReader Tests === + + #[test] + fn test_read_no_color_default() { + // Without setting environment variable, should return false + // Note: This test might be affected by actual environment + let _result = EnvConfigReader::read_no_color(); + // Function exists and returns a boolean value + } + + #[test] + fn test_read_page_size_with_invalid_value() { + // Test with invalid value handling + unsafe { + std::env::set_var("MBR_PAGE_SIZE", "invalid"); + } + let result = EnvConfigReader::read_page_size(); + assert!(result.is_none()); + unsafe { + std::env::remove_var("MBR_PAGE_SIZE"); + } + } + + #[test] + fn test_read_page_size_with_valid_value() { + // Test with valid value + unsafe { + std::env::set_var("MBR_PAGE_SIZE", "50"); + } + let result = EnvConfigReader::read_page_size(); + assert_eq!(result, Some(50)); + unsafe { + std::env::remove_var("MBR_PAGE_SIZE"); + } + } + + #[test] + fn test_read_max_memory_with_valid_value() { + // Test with valid memory value + unsafe { + std::env::set_var("MBR_MAX_MEMORY", "1024"); + } + let result = EnvConfigReader::read_max_memory(); + assert_eq!(result, Some(1024)); + unsafe { + std::env::remove_var("MBR_MAX_MEMORY"); + } + } }