fd_lib/
lib.rs

1#![allow(unused)]
2use std::path::{Path, PathBuf};
3
4use anyhow::Result;
5use cli::Opts;
6use compose::{
7    build_pattern_regex, build_regex, construct_config, ensure_search_pattern_is_not_a_path,
8    ensure_use_hidden_option_for_leading_dot_pattern, set_working_dir,
9};
10use dir_entry::DirEntry;
11use regex::bytes::Regex;
12mod compose;
13mod config;
14mod dir_entry;
15mod error;
16mod exec;
17mod exit_codes;
18mod filesystem;
19mod filetypes;
20mod fmt;
21mod output;
22mod regex_helper;
23mod walk;
24
25pub mod cli;
26pub mod filter;
27
28pub fn scan(opts: Opts) -> Result<Vec<PathBuf>> {
29    set_working_dir(&opts).expect("Failed to set working directory");
30    let search_paths = opts.search_paths().expect("Failed to get search paths");
31    ensure_search_pattern_is_not_a_path(&opts)
32        .expect("Failed to ensure search pattern is not a path");
33    let pattern = &opts.pattern;
34    let exprs = &opts.exprs;
35    let empty = Vec::new();
36
37    let pattern_regexps = exprs
38        .as_ref()
39        .unwrap_or(&empty)
40        .iter()
41        .chain([pattern])
42        .map(|pat| build_pattern_regex(pat, &opts))
43        .collect::<Result<Vec<String>>>()
44        .expect("Failed to build pattern regex");
45
46    let config = construct_config(opts, &pattern_regexps).expect("Failed to construct config");
47
48    ensure_use_hidden_option_for_leading_dot_pattern(&config, &pattern_regexps)
49        .expect("Failed to ensure use hidden option for leading dot pattern");
50
51    let regexps = pattern_regexps
52        .into_iter()
53        .map(|pat| build_regex(pat, &config))
54        .collect::<Result<Vec<Regex>>>()
55        .expect("Failed to build regex");
56
57    let result = walk::scan_and_collect(&search_paths, regexps, config);
58    match result {
59        Ok(entries) => Ok(entries
60            .into_iter()
61            .map(|arg0: dir_entry::DirEntry| DirEntry::path(&arg0).to_path_buf())
62            .collect()),
63        Err(err) => Err(err),
64    }
65}