From 6e4842a71a72c18ba66ce318e08acc8b1066a4fe Mon Sep 17 00:00:00 2001 From: aleidk Date: Thu, 28 Mar 2024 12:41:39 -0300 Subject: [PATCH] feat: Add basic directory walker functionality Walk a directory recursively finding all *.mp3 and *.flac files, exclue common hidden files and folders. --- Cargo.toml | 4 ++-- src/main.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 52e829d..be46c38 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,6 +3,6 @@ name = "juno" version = "0.1.0" edition = "2021" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - [dependencies] +clap = { version = "4.5.4", features = ["derive"] } +ignore = "0.4.22" diff --git a/src/main.rs b/src/main.rs index e7a11a9..4e0839d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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, +} + +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"); }