refactor: move file explorer logic to it's own module
This commit is contained in:
parent
6e4842a71a
commit
3439bdc37a
2 changed files with 32 additions and 30 deletions
26
src/file_explorer.rs
Normal file
26
src/file_explorer.rs
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
use ignore::types::TypesBuilder;
|
||||||
|
use ignore::WalkBuilder;
|
||||||
|
use std::{io, path::PathBuf};
|
||||||
|
|
||||||
|
pub fn walk_dir(path: &PathBuf) -> io::Result<Vec<PathBuf>> {
|
||||||
|
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: Vec<PathBuf> = WalkBuilder::new(path)
|
||||||
|
.types(types_builder.build().unwrap())
|
||||||
|
.build()
|
||||||
|
.filter_map(|entry| entry.ok())
|
||||||
|
.filter(|entry| !entry.path().is_dir())
|
||||||
|
.map(|entry| entry.path().to_path_buf())
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
Ok(entries)
|
||||||
|
}
|
||||||
36
src/main.rs
36
src/main.rs
|
|
@ -1,9 +1,9 @@
|
||||||
use ignore::types::TypesBuilder;
|
use std::{env, path::PathBuf};
|
||||||
use ignore::WalkBuilder;
|
|
||||||
use std::{env, io, path::PathBuf};
|
|
||||||
|
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
|
|
||||||
|
mod file_explorer;
|
||||||
|
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
#[command(version, about, long_about = None)]
|
#[command(version, about, long_about = None)]
|
||||||
struct Args {
|
struct Args {
|
||||||
|
|
@ -11,37 +11,13 @@ struct Args {
|
||||||
path: Option<PathBuf>,
|
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() {
|
fn main() {
|
||||||
let cli = Args::parse();
|
let cli = Args::parse();
|
||||||
let path = cli
|
let path = cli
|
||||||
.path
|
.path
|
||||||
.unwrap_or(env::current_dir().expect("Current directory is not available."));
|
.unwrap_or(env::current_dir().expect("Current directory is not available."));
|
||||||
|
|
||||||
walk_dir(&path).expect("error");
|
let files = file_explorer::walk_dir(&path).expect("error");
|
||||||
|
|
||||||
|
eprintln!("DEBUGPRINT[4]: main.rs:20: files={:#?}", files);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue