Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions crates/mbr-core/src/display/display_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
32 changes: 32 additions & 0 deletions crates/mbr-core/src/utils/data.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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");
}
}
31 changes: 0 additions & 31 deletions crates/mbr-core/src/utils/helpers.rs

This file was deleted.

85 changes: 0 additions & 85 deletions crates/mbr-core/src/utils/input.rs

This file was deleted.

8 changes: 6 additions & 2 deletions crates/mbr-core/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
90 changes: 87 additions & 3 deletions crates/mbr-core/src/utils/validation.rs
Original file line number Diff line number Diff line change
@@ -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<usize> {
std::env::var("MBR_PAGE_SIZE")
.ok()
.and_then(|s| s.parse::<usize>().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<usize> {
std::env::var("MBR_MAX_MEMORY")
.ok()
.and_then(|s| s.parse::<usize>().ok())
}
}

// === Input Validation ===

/// Validate that a URL is properly formatted
pub fn validate_url(url: &str) -> crate::Result<()> {
if url.is_empty() {
Expand Down Expand Up @@ -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");
}
}
}