feat: Added MusicScanner struct for scan ops
This commit is contained in:
parent
407e61976b
commit
fbfaeeae15
1 changed files with 73 additions and 0 deletions
|
@ -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
|
||||
///
|
||||
|
|
Loading…
Reference in a new issue