Simple interface to iterate over files or subdirs of a directory
Features:
- Ensure that file names are decodable to utf-8 (or error/warning is propagated)
- Ignore hidden entries (by default)
- Ignore common text editor and revision control backup files
- Select only files or only directories
- Simpler but detailed enough error handling
For example you can read all subdirectories this way:
use ScanDir;
dirs.read.unwrap
Compare it to stdlib way:
use read_dir;
for entry_res in read_dir.unwrap
Well, it looks almost fine until you want to turn unwrap's into correct error reporting.
Here is a list of non-hidden rust files:
use ScanDir;
let files: = files.read.unwrap;
And when you want to to return an from the closure, you need to unify the errors somehow. For example:
use io;
use PathBuf;
use File;
use ScanDir;
let result = files.read.map_err.and_then;
println!;
Let's see what's happen here:
- The return value of
read()isResult<Result<(), io::Error>, MyError> map_err(MyError::Scan)turns scan_dir error into our wrapper typeand_then(|val| val)unifies the error, producingResult<(), MyError>
Note in the last example you might convert error in the and_then clause,
but we want to know exact file name where error occured so we store it
in the error inside the lambda.