Projects STRLCPY deduplicator Commits 850c274a
🤬
  • refactor: use globwalk builder pattern

  • Loading...
  • beeb committed 1 year ago
    850c274a
    1 parent 31d35aae
Revision indexing in progress... (symbol navigation in revisions will be accurate after indexed)
  • ■ ■ ■ ■
    src/output.rs
    skipped 11 lines
    12 12  use unicode_segmentation::UnicodeSegmentation;
    13 13   
    14 14  fn format_path(path: &str, opts: &Params) -> Result<String> {
    15  - let display_path = path.replace(&opts.get_directory()?, "");
     15 + let display_path = path.replace(opts.get_directory()?.to_string_lossy().as_ref(), "");
    16 16   let display_range = if display_path.chars().count() > 32 {
    17 17   display_path
    18 18   .graphemes(true)
    skipped 157 lines
  • ■ ■ ■ ■ ■ ■
    src/params.rs
     1 +use std::{fs, path::PathBuf};
     2 + 
    1 3  use anyhow::{anyhow, Result};
    2 4  use clap::{Parser, ValueHint};
    3  -use std::{fs, path::PathBuf};
     5 +use globwalk::{GlobWalker, GlobWalkerBuilder};
    4 6   
    5 7  #[derive(Parser, Debug)]
    6 8  #[command(author, version, about, long_about = None)]
    skipped 23 lines
    30 32   }
    31 33   }
    32 34   
    33  - pub fn get_directory(&self) -> Result<String> {
    34  - let dir_pathbuf: PathBuf = self
    35  - .dir
    36  - .as_ref()
    37  - .unwrap_or(&std::env::current_dir()?)
    38  - .as_os_str()
    39  - .into();
    40  - 
    41  - let dir = fs::canonicalize(dir_pathbuf)?
    42  - .as_os_str()
    43  - .to_str()
    44  - .ok_or_else(|| anyhow!("Invalid directory"))?
    45  - .to_string();
    46  - 
     35 + pub fn get_directory(&self) -> Result<PathBuf> {
     36 + let current_dir = std::env::current_dir()?;
     37 + let dir_path = self.dir.as_ref().unwrap_or(&current_dir).as_path();
     38 + let dir = fs::canonicalize(dir_path)?;
    47 39   Ok(dir)
    48 40   }
    49 41   
    50  - pub fn get_glob_patterns(&self) -> PathBuf {
    51  - match self.types.as_ref() {
    52  - Some(filetypes) => vec![
    53  - self.get_directory().unwrap(),
    54  - String::from("**"),
    55  - format!("*.{{{filetypes}}}"),
    56  - ]
    57  - .iter()
    58  - .collect::<PathBuf>(),
    59  - None => vec![self.get_directory().unwrap().as_str(), "**", "*"]
    60  - .iter()
    61  - .collect::<PathBuf>(),
    62  - }
     42 + pub fn get_glob_walker(&self) -> Result<GlobWalker> {
     43 + let pattern: String = match self.types.as_ref() {
     44 + Some(filetypes) => format!("**/*{{{filetypes}}}"),
     45 + None => "**/*".to_string(),
     46 + };
     47 + // TODO: add params for maximum depth and following symlinks, then pass them to this builder
     48 + GlobWalkerBuilder::from_patterns(self.get_directory()?, &[pattern])
     49 + .build()
     50 + .map_err(|e| anyhow!(e))
    63 51   }
    64 52  }
    65 53   
  • ■ ■ ■ ■ ■
    src/scanner.rs
    skipped 38 lines
    39 39  }
    40 40   
    41 41  fn scan(app_opts: &Params) -> Result<Vec<File>> {
    42  - let glob_patterns = app_opts.get_glob_patterns().display().to_string();
    43  - let glob_iter = globwalk::glob(glob_patterns)?;
    44  - let files = glob_iter
     42 + let walker = app_opts.get_glob_walker()?;
     43 + let files = walker
    45 44   .filter_map(Result::ok)
    46 45   .map(|file| file.into_path())
    47 46   .filter(|fpath| fpath.is_file())
    skipped 80 lines
Please wait...
Page is in error, reload to recover