Skip to content

Commit

Permalink
Refactor: Refactoring level.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
allaboutevemirolive committed Oct 7, 2024
1 parent 5f86da0 commit 7702271
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/cli/arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl TreeArgs {

fn apply_level_settings(&self, tr: &mut TreeCtxt, matches: &clap::ArgMatches) {
if let Some(level) = matches.get_one::<usize>(options::miscellaneous::LEVEL) {
tr.level.with_cap(*level as i32);
tr.level.set_capacity(*level as u32);
}
}

Expand Down
106 changes: 70 additions & 36 deletions src/tree/level.rs
Original file line number Diff line number Diff line change
@@ -1,44 +1,63 @@
// TODO: Inform user if directories's depth potentially exceed more than capacity
#[allow(dead_code)]
#[derive(Debug, Clone, Copy)]
use std::cmp::min;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Level {
lvl: i32,
cap: i32,
level: u32,
capacity: u32,
}

impl Default for Level {
fn default() -> Self {
Level {
lvl: 1,
cap: 10_000,
}
Self::new(1, 10_000)
}
}

impl Level {
#[allow(dead_code)]
pub fn with_lvl_and_cap(lvl: i32, cap: i32) -> Self {
Level { lvl, cap }
pub fn new(level: u32, capacity: u32) -> Self {
Self { level, capacity }
}

pub fn level(&self) -> u32 {
self.level
}

pub fn with_cap(&mut self, num: i32) {
self.cap = num;
pub fn capacity(&self) -> u32 {
self.capacity
}

pub fn _modify_capacity(&mut self, num: i32) {
self.lvl = num;
pub fn set_capacity(&mut self, capacity: u32) {
self.capacity = capacity;
}

pub fn add_one(&mut self) {
self.lvl += 1;
pub fn set_level(&mut self, level: u32) {
self.level = min(level, self.capacity);
}

pub fn subtract_one(&mut self) {
self.lvl -= 1;
pub fn increment(&mut self) {
// if self.can_descend_further() {
self.level += 1;
// }
}

pub fn decrement(&mut self) {
// if self.level > 0 {
self.level -= 1;
// }
}

pub fn can_descend_further(&self) -> bool {
self.lvl < self.cap
self.level < self.capacity
}

pub fn depth_warning(&self) -> Option<String> {
if self.level >= self.capacity * 9 / 10 {
Some(format!(
"Warning: Current depth ({}) is approaching capacity ({})",
self.level, self.capacity
))
} else {
None
}
}
}

Expand All @@ -47,26 +66,41 @@ mod tests {
use super::*;

#[test]
fn test_plus_one() {
let mut level = Level::with_lvl_and_cap(0, 5000);
level.add_one();
assert_eq!(level.lvl, 1);
fn test_default() {
let level = Level::default();
assert_eq!(level.level(), 1);
assert_eq!(level.capacity(), 10_000);
}

#[test]
fn test_minus_one() {
let mut level = Level::with_lvl_and_cap(3, 5000);
level.subtract_one();
assert_eq!(level.lvl, 2);
fn test_new() {
let level = Level::new(5, 100);
assert_eq!(level.level(), 5);
assert_eq!(level.capacity(), 100);
}

#[test]
fn test_lvl_not_exceed_cap() {
let mut level = Level::with_lvl_and_cap(1, 5000);
while level.lvl < level.cap {
level.add_one();
}
assert_eq!(level.lvl, level.cap);
assert_ne!(level.lvl, 5001)
fn test_increment_and_decrement() {
let mut level = Level::new(5, 10);
level.increment();
assert_eq!(level.level(), 6);
level.decrement();
assert_eq!(level.level(), 5);
}

#[test]
fn test_can_descend_further() {
let level = Level::new(5, 10);
assert!(level.can_descend_further());
let level = Level::new(10, 10);
assert!(!level.can_descend_further());
}

#[test]
fn test_depth_warning() {
let level = Level::new(90, 100);
assert!(level.depth_warning().is_some());
let level = Level::new(50, 100);
assert!(level.depth_warning().is_none());
}
}
4 changes: 2 additions & 2 deletions src/walk/tr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,13 @@ impl<'tr, 'a> TreeCtxt<'tr, 'a> {
}

fn descend_into_directory(&mut self, visitor: &walk::visit::Visitor) -> anyhow::Result<()> {
self.level.add_one();
self.level.increment();
if let Some(path) = visitor.absolute_path() {
if self.walk_dir(path.to_path_buf()).is_err() {
self.dir_stats.err_dirs_add_one();
}
}
self.level.subtract_one();
self.level.decrement();
Ok(())
}

Expand Down

0 comments on commit 7702271

Please sign in to comment.