Skip to main content

cargo_plot/core/path_store/
store.rs

1use std::collections::HashSet;
2use std::path::Path;
3use walkdir::WalkDir;
4
5// use std::fs;
6// use std::path::Path;
7
8// [ENG]: Container for scanned paths and their searchable pool.
9// [POL]: Kontener na zeskanowane ścieżki i ich przeszukiwalną pulę.
10#[derive(Debug)]
11pub struct PathStore {
12    pub list: Vec<String>,
13}
14impl PathStore {
15    /// [POL]: Skanuje katalog rekurencyjnie i zwraca znormalizowane ścieżki (prefix "./", separator "/", suffix "/" dla folderów).
16    /// [ENG]: Scans the directory recursively and returns normalised paths (prefix "./", separator "/", suffix "/" for directories).
17    pub fn scan<P: AsRef<Path>>(dir_path: P) -> Self {
18        let mut list = Vec::new();
19        let entry_path = dir_path.as_ref();
20
21        for entry in WalkDir::new(entry_path).into_iter().filter_map(|e| e.ok()) {
22            // [POL]: Pominięcie katalogu głównego (głębokość 0).
23            // [ENG]: Skip the root directory (depth 0).
24            if entry.depth() == 0 {
25                continue;
26            }
27
28            // [POL]: Pominięcie dowiązań symbolicznych i punktów reparse.
29            // [ENG]: Skip symbolic links and reparse points.
30            if entry.path_is_symlink() {
31                continue;
32            }
33
34            if let Ok(rel_path) = entry.path().strip_prefix(entry_path) {
35                // [POL]: Normalizacja separatorów systemowych do formatu uniwersalnego.
36                // [ENG]: Normalisation of system separators to a universal format.
37                let relative_str = rel_path.to_string_lossy().replace('\\', "/");
38                let mut final_path = format!("./{}", relative_str);
39
40                if entry.file_type().is_dir() {
41                    final_path.push('/');
42                }
43
44                list.push(final_path);
45            }
46        }
47
48        Self { list }
49    }
50
51    // [ENG]: Creates a temporary pool of references for the matcher.
52    // [POL]: Tworzy tymczasową pulę referencji (paths_pool) dla matchera.
53    pub fn get_index(&self) -> HashSet<&str> {
54        self.list.iter().map(|s| s.as_str()).collect()
55    }
56}