feat: Added MusicScanner struct for scan ops

This commit is contained in:
Nickiel12 2022-12-11 21:36:22 -08:00
parent 407e61976b
commit fbfaeeae15

View file

@ -4,6 +4,79 @@ use scan_dir::ScanDir;
const SUPPORTED_FILETYPES: [&str; 1] = ["mp3"];
/// The object that iteratively and recursively scans the directories
///
/// The way to instantiate this struct is to call new with the root directory.
///
/// # Examples
/// ```rust
/// let music_scanner = MusicScanner::new("/home/urs/Music/")
/// ```
pub struct MusicScanner {
dirs: Vec<PathBuf>,
}
impl MusicScanner {
pub fn new(root: String) -> Self {
MusicScanner {
dirs: vec![root.into()],
}
}
}
impl Iterator for MusicScanner {
type Item = Vec<PathBuf>;
/// This function pops the top most of MusicScanner.dirs internal Vec, and scans a directory
/// It returns a Vector of found file paths while updating the internal directory list
///
/// # Examples
/// ```rust
/// let music_scanner = MusicScanner::new("/home/usr/Music");
/// let files = music_scanner.next();
/// println!(files);
/// >>> "/home/usr/Music/file_1.mp3"
/// >>> "/home/usr/Music/file_2.mp3"
/// ```
///
fn next(&mut self) -> Option<Self::Item> {
let mut files = vec![];
let target = match self.dirs.pop() {
Some(val) => val,
None => {
return None;
}
};
// scan the currect dir for other directories for later scanning
ScanDir::dirs()
.read(target.clone(), |iter| {
for (entry, _name) in iter {
self.dirs.push(entry.path());
}
})
.unwrap();
// scan the current dir for normal files
// TODO: Need to add filters once list of supported files is created
ScanDir::files()
.read(target, |iter| {
for (entry, _name) in iter {
files.push(entry.path());
}
})
.unwrap();
// return the found files
if files.len() > 0 {
Some(files)
} else {
None
}
}
}
/// Recursivly scans the given root directory for files
/// WIP Function! All it does is print the directories
///