From 4b1b6e7648d4177755dabecb7a3879cb651d050e Mon Sep 17 00:00:00 2001 From: Nickiel12 Date: Sat, 10 Dec 2022 22:05:40 -0800 Subject: [PATCH] feat: Added file scanning Created a sub module for file system oprations Create proof of concept function to recursivly scan directories for files --- src/file_operations.rs | 38 ++++++++++++++++++++++++++++++++++++++ src/main.rs | 18 +++--------------- 2 files changed, 41 insertions(+), 15 deletions(-) create mode 100644 src/file_operations.rs diff --git a/src/file_operations.rs b/src/file_operations.rs new file mode 100644 index 0000000..e98444f --- /dev/null +++ b/src/file_operations.rs @@ -0,0 +1,38 @@ +use std::path::PathBuf; + +use scan_dir::ScanDir; + +const SUPPORTED_FILETYPES: [&str; 1] = ["mp3"]; + +pub fn scan_music_dir(root: String) -> Result<(), ()> { + let mut directories = Vec::::new(); + directories.push(root.into()); + + while directories.len() != 0 { + let target = match directories.pop() { + Some(val) => val, + None => { + panic!("Whoa man this ai't right"); + } + }; + + ScanDir::dirs() + .read(target.clone(), |iter| { + for (entry, name) in iter { + directories.push(entry.path()); + println!("I found a director {:?} at path {:?}", name, entry.path()); + } + }) + .unwrap(); + + ScanDir::files() + .read(target, |iter| { + for (entry, name) in iter { + println!("found file {:?} at path {:?}", name, entry.path()); + } + }) + .unwrap(); + } + + Ok(()) +} diff --git a/src/main.rs b/src/main.rs index 518c21e..8930335 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,8 @@ use clap::Parser; use dirs_next; use scan_dir::ScanDir; +pub mod file_operations; + #[derive(Parser, Debug)] #[command(author, version, about, long_about=None)] struct Cli { @@ -46,21 +48,7 @@ fn main() { println!("Music directory is: {:?}", dirs_next::audio_dir()); } - // ScanDir::dirs() - // and - // ScanDir::files() - //iter.filter(|&(_, ref name)| name.ends_with(".rs")) - // .map(|(ref entry, _)| entry.path()) - // .collect() + file_operations::scan_music_dir(music_dir).unwrap(); - let files = ScanDir::dirs() - .read(music_dir, |iter| { - for (entry, name) in iter { - println!("File {:?} has full path {:?}", name, entry.path()); - } - }) - .unwrap(); - - println!("Hello, world!"); println!("{:?}", cli); }