Skip to content

Commit

Permalink
Refactor: Introduce helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
allaboutevemirolive committed Oct 7, 2024
1 parent 55560b5 commit f0facc5
Showing 1 changed file with 33 additions and 32 deletions.
65 changes: 33 additions & 32 deletions src/config/inspect.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
use crate::report::stats::DirectoryStats;

use std::fs;
use std::fs::DirEntry;
use std::fs::{self, DirEntry};
use std::path::PathBuf;

pub type FnReadDir = fn(PathBuf, &mut DirectoryStats) -> anyhow::Result<Vec<DirEntry>>;

/// Helper function to filter hidden entries
#[inline(always)]
fn is_not_hidden(entry: &DirEntry, dir_stats: &mut DirectoryStats) -> bool {
if entry.file_name().to_string_lossy().starts_with('.') {
dir_stats.hidden_add_one();
false
} else {
true
}
}

/// Helper function to check if an entry is a directory
#[inline(always)]
fn is_directory(entry: &DirEntry) -> bool {
entry.metadata().map(|m| m.is_dir()).unwrap_or(false)
}

/// Read all directory entries
pub fn read_all_entries(
path: PathBuf,
_dir_stats: &mut DirectoryStats,
Expand All @@ -15,67 +31,52 @@ pub fn read_all_entries(
Ok(entries)
}

/// Read visible (non-hidden) directory entries
pub fn read_visible_entries(
path: PathBuf,
dir_stats: &mut DirectoryStats,
) -> anyhow::Result<Vec<DirEntry>> {
let entries = fs::read_dir(path)?
.filter_map(|entry_result| {
entry_result.ok().and_then(|entry| {
if !entry.file_name().to_string_lossy().starts_with('.') {
Some(entry)
} else {
dir_stats.hidden_add_one();
None
}
})
entry_result
.ok()
.filter(|entry| is_not_hidden(entry, dir_stats))
})
.collect();

Ok(entries)
}

/// Read visible (non-hidden) folders only
pub fn read_visible_folders(
path: PathBuf,
dir_stats: &mut DirectoryStats,
) -> anyhow::Result<Vec<DirEntry>> {
let entries = fs::read_dir(path)?
.filter_map(|entry_result| {
entry_result.ok().and_then(|entry| {
let metadata = entry.metadata().ok()?;
if metadata.is_dir() && !entry.file_name().to_string_lossy().starts_with('.') {
Some(entry)
} else {
dir_stats.hidden_add_one();
None
}
})
entry_result
.ok()
.filter(|entry| is_directory(entry) && is_not_hidden(entry, dir_stats))
})
.collect();

Ok(entries)
}

/// Read all folders (including hidden)
#[allow(dead_code)]
pub fn read_all_folders(path: PathBuf) -> anyhow::Result<Vec<DirEntry>> {
let entries = fs::read_dir(path)?
.filter_map(|entry_result| {
entry_result.ok().and_then(|entry| {
let metadata = entry.metadata().ok()?;
if metadata.is_dir() {
Some(entry)
} else {
None
}
})
})
.filter_map(|entry_result| entry_result.ok().filter(|entry| is_directory(entry)))
.collect();

Ok(entries)
}

#[cfg(test)]
mod tests {

use std::fs;
// cargo test test_test -- --nocapture

#[test]
fn test_relative_path() {
let current_dir = std::env::current_dir().expect("Failed to get current directory");
Expand Down

0 comments on commit f0facc5

Please sign in to comment.