bomboni_fs crate provides utilities for recursively visiting files in a directory tree with support for filtering by file extensions.
Functions
visit_files
Recursively visits files in a directory with specified extensions and executes a callback for each matching file.dir- The directory path to start traversing fromextensions- A slice of file extensions to match (without the dot, e.g.,"rs","txt")cb- A mutable callback function that receives a reference to each matchingDirEntry
Result<(), E> where E is your custom error typeErrors:Returns an error if:- Directory reading fails
- The callback returns an error
visit_files_contents
Recursively visits files in a directory and reads their contents, executing a callback with both the file entry and its contents.dir- The directory path to start traversing fromextensions- A slice of file extensions to match (without the dot, e.g.,"rs","txt")cb- A mutable callback function that receives a reference to each matchingDirEntryand the file’s contents as aString
Result<(), E> where E is your custom error typeErrors:Returns an error if:- Directory reading fails
- File reading fails
- The callback returns an error
Usage Examples
Collecting File Paths
Processing Multiple File Types
Analyzing File Contents
Searching for Patterns
Error Handling with Custom Error Types
Building File Trees
Implementation Notes
- Both functions use recursive directory traversal
- Symbolic links are followed during traversal
- File extensions are matched case-sensitively
- The extension check excludes the leading dot (use
"rs", not".rs") visit_files_contentsusesvisit_filesinternally and adds file reading- Files are opened with read-only permissions
- The entire file content is read into memory as a UTF-8 string
Best Practices
-
Extension Matching: Provide extensions without the dot prefix:
-
Error Handling: Implement proper error types with
From<io::Error>: -
Memory Considerations: Be cautious when using
visit_files_contentswith large files as it reads entire files into memory -
Performance: For better performance with many files, consider using
visit_filesand reading files selectively