diff --git a/src/scanner.rs b/src/scanner.rs index d5703d5..9419bcf 100644 --- a/src/scanner.rs +++ b/src/scanner.rs @@ -3,12 +3,13 @@ use crate::{fileinfo::FileInfo, params::Params}; use anyhow::Result; use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; use std::sync::{Arc, Mutex}; -use std::{fs, path::PathBuf, time::Duration}; +use std::{fs, path::Path, time::Duration}; use globwalk::{GlobWalker, GlobWalkerBuilder}; pub struct Scanner { - pub directory: Option, + pub directory: Box, + pub app_args: Arc, pub filetypes: Option, pub min_depth: Option, pub max_depth: Option, @@ -18,43 +19,17 @@ pub struct Scanner { } impl Scanner { - pub fn new() -> Self { - Self { - directory: None, - filetypes: None, - min_depth: None, - max_depth: None, - min_size: None, - follow_links: true, - progress: false, - } - } - - pub fn build(app_args: Arc) -> Result { - let mut scanner = Scanner::new(); - scanner.directory = Some(app_args.get_directory()?); - scanner.progress = app_args.progress; - - if let Some(min_size) = app_args.get_min_size() { - scanner.min_size = Some(min_size); - } - - if app_args.get_types().is_some() { - scanner.filetypes = app_args.get_types(); - } - - if app_args.min_depth.is_some() { - scanner.min_depth = app_args.min_depth; - } - - if app_args.max_depth.is_some() { - scanner.max_depth = app_args.max_depth; - } - - // TODO: Add option to disable follow links from cli app args - // scanner.follow_links = false; - - Ok(scanner) + pub fn new(app_args: Arc) -> Result { + Ok(Self { + directory: app_args.get_directory()?.into_boxed_path(), + filetypes: app_args.get_types(), + min_depth: app_args.min_depth, + max_depth: app_args.max_depth, + min_size: app_args.get_min_size(), + follow_links: app_args.follow_links, + progress: app_args.progress, + app_args, + }) } fn scan_patterns(&self) -> Result { @@ -64,15 +39,6 @@ impl Scanner { }) } - fn scan_dir(&self) -> Result { - let scan_dir = match &self.directory { - Some(path) => path.clone(), - None => std::env::current_dir()?, - }; - - Ok(fs::canonicalize(scan_dir)?) - } - fn attach_link_opts(&self, walker: GlobWalkerBuilder) -> Result { Ok(walker.follow_links(self.follow_links)) } @@ -92,7 +58,7 @@ impl Scanner { } fn build_walker(&self) -> Result { let walker = Ok(GlobWalkerBuilder::from_patterns( - self.scan_dir()?, + self.directory.clone(), &[self.scan_patterns()?], )) .and_then(|walker| self.attach_walker_min_depth(walker)) diff --git a/src/server.rs b/src/server.rs index ae2c4ea..989786d 100644 --- a/src/server.rs +++ b/src/server.rs @@ -56,7 +56,7 @@ impl Server { let progbarbox_sc_clone = progbarbox.clone(); self.threadpool.execute(move || { - Scanner::build(app_args_clone_for_sc) + Scanner::new(app_args_clone_for_sc) .unwrap() .scan(file_queue_clone_sc, progbarbox_sc_clone) .unwrap();