path-rs 0.1.0

Cross-platform path expansion, normalization, traversal, searching, and caching
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
//! Generic recursive directory discovery (no VCS or product-specific roots).
//!
//! Enabled by the `listing` feature. Callers supply predicates for domain rules
//! (for example, detecting a project marker directory).

use crate::error::PathError;
use crate::identity::{PathIdentityOptions, path_identity_key};
use crate::inspect::{DirectoryInspection, inspect_directory};
use crate::internal::validation::reject_nul_path;
use crate::metadata::{SortMode, TraversalErrorPolicy};
use std::collections::HashSet;
use std::path::{Path, PathBuf};
use walkdir::WalkDir;

/// Options for recursive directory discovery.
///
/// Skip rules are **caller-configured**. This crate does not hardcode
/// `node_modules`, `.git`, `target`, etc.
///
/// # Depth model
///
/// Same as [`crate::listing::ListOptions`]:
///
/// - **Depth 0** — the walk root
/// - **Depth 1** — immediate children
/// - `recursive: false` limits the walk to depth 0 only (the root)
/// - `recursive: true` with `max_depth: Some(n)` uses walkdir `max_depth = n`
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DiscoveryOptions {
    /// Recurse into subdirectories.
    pub recursive: bool,
    /// Follow symbolic links (best-effort cycle detection via walkdir).
    pub follow_symlinks: bool,
    /// Maximum walk depth relative to the root (`None` = unlimited when recursive).
    ///
    /// Depth `0` is the root itself; `1` is immediate children.
    /// Ignored when `recursive` is false (only the root is considered).
    pub max_depth: Option<usize>,
    /// Maximum number of directories returned.
    pub max_entries: Option<usize>,
    /// Directory **names** to skip (exact component match). Never applied to the root itself.
    pub skip_directory_names: Vec<String>,
    /// Relative path prefixes (using `/` separators) to skip under the root.
    pub skip_relative_prefixes: Vec<String>,
    /// Glob patterns matched against relative paths (requires `search` / `globset`).
    #[cfg(feature = "search")]
    pub skip_globs: Vec<String>,
    /// Error handling policy.
    pub error_policy: TraversalErrorPolicy,
    /// Deduplicate results by identity key.
    pub deduplicate: bool,
    /// Identity options used when `deduplicate` is true.
    pub identity: PathIdentityOptions,
    /// Sort mode for the returned list.
    pub sort: SortMode,
}

impl Default for DiscoveryOptions {
    fn default() -> Self {
        Self {
            recursive: true,
            follow_symlinks: false,
            max_depth: None,
            max_entries: None,
            skip_directory_names: Vec::new(),
            skip_relative_prefixes: Vec::new(),
            #[cfg(feature = "search")]
            skip_globs: Vec::new(),
            error_policy: TraversalErrorPolicy::FailFast,
            deduplicate: true,
            identity: PathIdentityOptions::default(),
            sort: SortMode::Path,
        }
    }
}

impl DiscoveryOptions {
    /// Create default options (`Self::default()`).
    pub fn new() -> Self {
        Self::default()
    }

    /// Enable or disable recursion.
    pub fn recursive(mut self, recursive: bool) -> Self {
        self.recursive = recursive;
        self
    }

    /// Follow symlinks when walking.
    pub fn follow_symlinks(mut self, follow: bool) -> Self {
        self.follow_symlinks = follow;
        self
    }

    /// Skip exact directory names (e.g. `"node_modules"`).
    pub fn skip_names(mut self, names: impl IntoIterator<Item = impl Into<String>>) -> Self {
        self.skip_directory_names
            .extend(names.into_iter().map(Into::into));
        self
    }

    /// Skip relative path prefixes under the root.
    pub fn skip_relative_prefixes(
        mut self,
        prefixes: impl IntoIterator<Item = impl Into<String>>,
    ) -> Self {
        self.skip_relative_prefixes
            .extend(prefixes.into_iter().map(Into::into));
        self
    }

    /// Set maximum depth (see module / type docs).
    pub fn max_depth(mut self, depth: Option<usize>) -> Self {
        self.max_depth = depth;
        self
    }

    /// Set maximum number of returned directories.
    pub fn max_entries(mut self, max: Option<usize>) -> Self {
        self.max_entries = max;
        self
    }

    /// Set error policy.
    pub fn error_policy(mut self, policy: TraversalErrorPolicy) -> Self {
        self.error_policy = policy;
        self
    }

    /// Enable or disable identity-key deduplication.
    pub fn deduplicate(mut self, deduplicate: bool) -> Self {
        self.deduplicate = deduplicate;
        self
    }

    /// Identity options used when deduplicating.
    pub fn identity(mut self, identity: PathIdentityOptions) -> Self {
        self.identity = identity;
        self
    }

    /// Sort mode for the returned list.
    pub fn sort(mut self, sort: SortMode) -> Self {
        self.sort = sort;
        self
    }
}

/// Control flow for visitor-based traversal.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum VisitControl {
    /// Continue walking.
    Continue,
    /// Do not descend into the current directory's children.
    SkipChildren,
    /// Stop the walk entirely.
    Stop,
}

/// Callback interface for directory walks.
pub trait DirectoryVisitor {
    /// Called for each directory entry.
    fn visit_directory(&mut self, path: &Path, inspection: &DirectoryInspection) -> VisitControl;

    /// Called when an error is encountered for `path`.
    fn visit_error(&mut self, path: &Path, error: &PathError) -> VisitControl {
        let _ = (path, error);
        VisitControl::Continue
    }
}

/// Adapter so closures can be used with [`visit_directories`].
///
/// Errors always continue (use a custom type for fail-fast via the visitor).
impl<F> DirectoryVisitor for F
where
    F: FnMut(&Path, &DirectoryInspection) -> VisitControl,
{
    fn visit_directory(&mut self, path: &Path, inspection: &DirectoryInspection) -> VisitControl {
        self(path, inspection)
    }
}

/// Discover directories under `root` (all directories, subject to skip rules).
///
/// # Filesystem access
///
/// **Yes.** Does not follow symlinks by default. Does not use a cache.
/// Does not spawn threads.
pub fn discover_directories(
    root: impl AsRef<Path>,
    options: &DiscoveryOptions,
) -> Result<Vec<PathBuf>, PathError> {
    discover_where(root, options, |_, inspection| inspection.is_directory)
}

/// Discover directories for which `predicate` returns true.
///
/// The predicate receives the directory path and a [`DirectoryInspection`].
/// Skip rules are applied before the predicate (except the walk root is never
/// skipped by name rules).
///
/// # Filesystem access
///
/// **Yes.**
pub fn discover_where(
    root: impl AsRef<Path>,
    options: &DiscoveryOptions,
    mut predicate: impl FnMut(&Path, &DirectoryInspection) -> bool,
) -> Result<Vec<PathBuf>, PathError> {
    let root = root.as_ref();
    reject_nul_path(root)?;
    let root_inspection = inspect_directory(root)?;
    if !root_inspection.exists || !root_inspection.is_directory {
        return Err(PathError::invalid(format!(
            "discovery root is not a directory: {}",
            root.to_string_lossy()
        )));
    }

    #[cfg(feature = "search")]
    let skip_globset = build_skip_globset(&options.skip_globs)?;

    let max_depth = if options.recursive {
        options.max_depth.unwrap_or(usize::MAX)
    } else {
        0
    };

    let walker = WalkDir::new(root)
        .min_depth(0)
        .max_depth(max_depth)
        .follow_links(options.follow_symlinks);

    let mut out = Vec::new();
    let mut seen_keys = HashSet::new();
    let mut skip_prefixes: HashSet<PathBuf> = HashSet::new();

    for item in walker {
        if let Some(max) = options.max_entries {
            if out.len() >= max {
                break;
            }
        }

        let entry = match item {
            Ok(e) => e,
            Err(err) => {
                let path = err
                    .path()
                    .map(Path::to_path_buf)
                    .unwrap_or_else(|| root.to_path_buf());
                let pe = PathError::traversal(format!("{} ({})", err, path.display()));
                match options.error_policy {
                    TraversalErrorPolicy::FailFast => return Err(pe),
                    TraversalErrorPolicy::SkipErrors => continue,
                }
            }
        };

        let path = entry.path();
        let file_type = entry.file_type();
        if !(file_type.is_dir() || (options.follow_symlinks && file_type.is_symlink())) {
            continue;
        }

        // Skip children of previously skipped trees.
        if skip_prefixes
            .iter()
            .any(|p| path.starts_with(p) && path != p)
        {
            continue;
        }

        let is_root = path == root;
        let name = entry.file_name().to_string_lossy();

        // Never skip the root merely because its name matches a skip rule.
        if !is_root {
            if options
                .skip_directory_names
                .iter()
                .any(|n| n.as_str() == name)
            {
                skip_prefixes.insert(path.to_path_buf());
                continue;
            }

            if let Ok(rel) = path.strip_prefix(root) {
                let rel_str = rel.to_string_lossy().replace('\\', "/");
                if options
                    .skip_relative_prefixes
                    .iter()
                    .any(|p| rel_str == *p || rel_str.starts_with(&format!("{p}/")))
                {
                    skip_prefixes.insert(path.to_path_buf());
                    continue;
                }

                #[cfg(feature = "search")]
                if let Some(gs) = &skip_globset {
                    if gs.is_match(rel_str.as_str()) || gs.is_match(name.as_ref()) {
                        skip_prefixes.insert(path.to_path_buf());
                        continue;
                    }
                }
            }
        }

        let inspection = match inspect_directory(path) {
            Ok(i) => i,
            Err(e) => match options.error_policy {
                TraversalErrorPolicy::FailFast => return Err(e),
                TraversalErrorPolicy::SkipErrors => continue,
            },
        };

        if !inspection.is_directory {
            continue;
        }

        if !predicate(path, &inspection) {
            continue;
        }

        if options.deduplicate {
            let key = path_identity_key(path, options.identity)?;
            if !seen_keys.insert(key) {
                continue;
            }
        }

        out.push(path.to_path_buf());
    }

    sort_paths(&mut out, options.sort);
    Ok(out)
}

/// Walk directories with a [`DirectoryVisitor`].
///
/// # Filesystem access
///
/// **Yes.**
pub fn visit_directories(
    root: impl AsRef<Path>,
    options: &DiscoveryOptions,
    visitor: &mut dyn DirectoryVisitor,
) -> Result<(), PathError> {
    let root = root.as_ref();
    reject_nul_path(root)?;

    let max_depth = if options.recursive {
        options.max_depth.unwrap_or(usize::MAX)
    } else {
        0
    };

    let walker = WalkDir::new(root)
        .min_depth(0)
        .max_depth(max_depth)
        .follow_links(options.follow_symlinks);

    let mut skip_prefixes: HashSet<PathBuf> = HashSet::new();

    for item in walker {
        let entry = match item {
            Ok(e) => e,
            Err(err) => {
                let path = err
                    .path()
                    .map(Path::to_path_buf)
                    .unwrap_or_else(|| root.to_path_buf());
                let pe = PathError::traversal(format!("{err}"));
                match visitor.visit_error(&path, &pe) {
                    VisitControl::Stop => return Ok(()),
                    VisitControl::SkipChildren | VisitControl::Continue => {
                        if matches!(options.error_policy, TraversalErrorPolicy::FailFast) {
                            return Err(pe);
                        }
                        continue;
                    }
                }
            }
        };

        let path = entry.path();
        if !entry.file_type().is_dir() {
            continue;
        }

        if skip_prefixes
            .iter()
            .any(|p| path.starts_with(p) && path != p)
        {
            continue;
        }

        let is_root = path == root;
        if !is_root {
            let name = entry.file_name().to_string_lossy();
            if options
                .skip_directory_names
                .iter()
                .any(|n| n.as_str() == name)
            {
                skip_prefixes.insert(path.to_path_buf());
                continue;
            }
        }

        let inspection = match inspect_directory(path) {
            Ok(i) => i,
            Err(e) => match visitor.visit_error(path, &e) {
                VisitControl::Stop => return Ok(()),
                _ if matches!(options.error_policy, TraversalErrorPolicy::FailFast) => {
                    return Err(e);
                }
                _ => continue,
            },
        };

        match visitor.visit_directory(path, &inspection) {
            VisitControl::Continue => {}
            VisitControl::SkipChildren => {
                skip_prefixes.insert(path.to_path_buf());
            }
            VisitControl::Stop => return Ok(()),
        }
    }

    Ok(())
}

fn sort_paths(paths: &mut [PathBuf], mode: SortMode) {
    match mode {
        SortMode::None => {}
        SortMode::Path | SortMode::DirsFirst => {
            paths.sort_by(|a, b| a.to_string_lossy().cmp(&b.to_string_lossy()));
        }
        SortMode::Name => {
            paths.sort_by(|a, b| {
                let an = a
                    .file_name()
                    .map(|n| n.to_string_lossy())
                    .unwrap_or_default();
                let bn = b
                    .file_name()
                    .map(|n| n.to_string_lossy())
                    .unwrap_or_default();
                an.cmp(&bn)
                    .then_with(|| a.to_string_lossy().cmp(&b.to_string_lossy()))
            });
        }
    }
}

#[cfg(feature = "search")]
fn build_skip_globset(patterns: &[String]) -> Result<Option<globset::GlobSet>, PathError> {
    if patterns.is_empty() {
        return Ok(None);
    }
    let mut builder = globset::GlobSetBuilder::new();
    for p in patterns {
        let g = globset::Glob::new(p).map_err(|e| PathError::InvalidGlob {
            message: e.to_string(),
        })?;
        builder.add(g);
    }
    let set = builder.build().map_err(|e| PathError::InvalidGlob {
        message: e.to_string(),
    })?;
    Ok(Some(set))
}