feat: Add basic directory walker functionality

Walk a directory recursively finding all *.mp3 and *.flac files, exclue
common hidden files and folders.
This commit is contained in:
Alexander Navarro 2024-03-28 12:41:39 -03:00
parent 48ba1f9386
commit 6e4842a71a
2 changed files with 48 additions and 4 deletions

View file

@ -1,3 +1,47 @@
fn main() {
println!("Hello, world!");
use ignore::types::TypesBuilder;
use ignore::WalkBuilder;
use std::{env, io, path::PathBuf};
use clap::Parser;
#[derive(Parser)]
#[command(version, about, long_about = None)]
struct Args {
#[arg(help = "Directory to scan for files")]
path: Option<PathBuf>,
}
fn walk_dir(path: &PathBuf) -> io::Result<()> {
let mut types_builder = TypesBuilder::new();
types_builder.add_defaults();
let accepted_filetypes = ["mp3", "flac"];
for filetype in accepted_filetypes {
let _ = types_builder.add("sound", format!("*.{}", filetype).as_str());
}
types_builder.select("sound");
let entries = WalkBuilder::new(path)
.types(types_builder.build().unwrap())
.build()
.filter_map(|entry| entry.ok())
.filter(|entry| !entry.path().is_dir());
for result in entries {
let path = result.path();
println!("{}", path.display());
}
Ok(())
}
fn main() {
let cli = Args::parse();
let path = cli
.path
.unwrap_or(env::current_dir().expect("Current directory is not available."));
walk_dir(&path).expect("error");
}