From fbfaeeae15460dc073557548a60de7d68bcbe190 Mon Sep 17 00:00:00 2001 From: Nickiel12 Date: Sun, 11 Dec 2022 21:36:22 -0800 Subject: [PATCH] feat: Added MusicScanner struct for scan ops --- src/file_operations.rs | 73 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/src/file_operations.rs b/src/file_operations.rs index f2bfa01..8b6e4e0 100644 --- a/src/file_operations.rs +++ b/src/file_operations.rs @@ -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, +} + +impl MusicScanner { + pub fn new(root: String) -> Self { + MusicScanner { + dirs: vec![root.into()], + } + } +} + +impl Iterator for MusicScanner { + type Item = Vec; + + /// 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 { + 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 ///