Refactor codebase for better testability and maintainability #2

Merged
bnied merged 2 commits from feedback into main 2025-09-08 14:04:18 +00:00
bnied commented 2025-09-07 18:01:28 +00:00 (Migrated from github.com)

Major restructuring based on code review feedback to improve testability, separation of concerns, and adherence to Rust conventions.

Code Organization:

  • Split monolithic main.rs (260 lines) into 6 focused modules
  • main.rs: CLI argument handling and orchestration (194 lines)
  • file_info.rs: FileInfo struct with accessor methods
  • formatter.rs: Display formatting with Display trait implementation
  • directory.rs: Directory reading and traversal logic
  • sorting.rs: Sorting strategies and configuration
  • utils.rs: Utility functions for formatting and colors

Key Improvements:

  • FileInfo now stores file_name as a field (calculated once)
  • Created FileInfoFormatter with proper Display trait implementation
  • Moved run() method to impl Args for better encapsulation
  • Decoupled display logic from Args for easier testing
  • Added unit tests for formatter functionality
  • Better error handling that collects and reports all errors

Rust Convention Compliance:

  • Fixed all clippy warnings (including pedantic mode)
  • Proper lifetime elision and format string interpolation
  • Using slices instead of Vec references where appropriate
  • All code formatted with rustfmt
  • Comprehensive documentation with block comments

Additional Features:

  • Support for multiple path arguments
  • Case-insensitive sorting by default
  • Platform-independent block size calculation

Testing:

  • Added tempfile as dev-dependency
  • Implemented unit tests for display formatting
  • Display logic can now be tested independently of CLI args
Major restructuring based on code review feedback to improve testability, separation of concerns, and adherence to Rust conventions. Code Organization: - Split monolithic main.rs (260 lines) into 6 focused modules - main.rs: CLI argument handling and orchestration (194 lines) - file_info.rs: FileInfo struct with accessor methods - formatter.rs: Display formatting with Display trait implementation - directory.rs: Directory reading and traversal logic - sorting.rs: Sorting strategies and configuration - utils.rs: Utility functions for formatting and colors Key Improvements: - FileInfo now stores file_name as a field (calculated once) - Created FileInfoFormatter with proper Display trait implementation - Moved run() method to impl Args for better encapsulation - Decoupled display logic from Args for easier testing - Added unit tests for formatter functionality - Better error handling that collects and reports all errors Rust Convention Compliance: - Fixed all clippy warnings (including pedantic mode) - Proper lifetime elision and format string interpolation - Using slices instead of Vec references where appropriate - All code formatted with rustfmt - Comprehensive documentation with block comments Additional Features: - Support for multiple path arguments - Case-insensitive sorting by default - Platform-independent block size calculation Testing: - Added tempfile as dev-dependency - Implemented unit tests for display formatting - Display logic can now be tested independently of CLI args
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
spaceduck/ls-rust!2
No description provided.