Skip to main content

weavatrix_scan/scanner/
api.rs

1use super::{
2    ParallelRuntime, PathBuf, Result, ScanCache, ScanOptions, ScanReport, Scanner, compact,
3    scan_repository_with_runtime, watch_update,
4};
5use crate::watch::WatchPlan;
6
7impl Scanner {
8    #[must_use]
9    pub fn new(root: impl Into<PathBuf>) -> Self {
10        Self {
11            root: root.into(),
12            options: ScanOptions::default(),
13            runtime: ParallelRuntime::global(),
14        }
15    }
16
17    #[must_use]
18    pub fn options(mut self, options: ScanOptions) -> Self {
19        self.options = options;
20        self
21    }
22
23    /// Selects the executor used by parallel discovery.
24    #[must_use]
25    pub fn runtime(mut self, runtime: ParallelRuntime) -> Self {
26        self.runtime = runtime;
27        self
28    }
29
30    /// Scans the configured repository root.
31    ///
32    /// # Errors
33    ///
34    /// Returns an error when the root cannot be canonicalized/read, or when a
35    /// local error occurs under `ErrorPolicy::Abort`.
36    pub fn scan(self) -> Result<ScanReport> {
37        scan_repository_with_runtime(&self.root, &self.options, None, &self.runtime)
38    }
39
40    /// Scans into a compact manifest that stores the canonical root once.
41    ///
42    /// This is the preferred report for very large repositories when callers
43    /// do not require an owned absolute path on every file record.
44    ///
45    /// # Errors
46    ///
47    /// Returns the same errors as [`Self::scan`].
48    pub fn scan_compact(self) -> Result<crate::CompactScanReport> {
49        compact::scan_repository_compact_with_runtime(&self.root, &self.options, &self.runtime)
50    }
51
52    /// Scans while reusing strong hashes from an older persistent report when
53    /// file identity, size and timestamps are unchanged.
54    ///
55    /// # Errors
56    ///
57    /// Returns the same errors as [`Self::scan`]. Reports from another root or
58    /// reports without file-version evidence are scanned without cache reuse.
59    pub fn scan_incremental(self, previous: &ScanReport) -> Result<ScanReport> {
60        let cache = previous.to_cache();
61        scan_repository_with_runtime(&self.root, &self.options, Some(&cache), &self.runtime)
62    }
63
64    /// Scans while reusing a compact, versioned local cache.
65    ///
66    /// Incompatible versions and caches belonging to another canonical root
67    /// are safely ignored.
68    ///
69    /// # Errors
70    ///
71    /// Returns the same errors as [`Self::scan`].
72    pub fn scan_cached(self, cache: &ScanCache) -> Result<ScanReport> {
73        scan_repository_with_runtime(&self.root, &self.options, Some(cache), &self.runtime)
74    }
75
76    /// Applies a watcher plan without traversing unchanged directories.
77    ///
78    /// Safe file-only plans re-match and inspect only changed paths, remove
79    /// deleted paths, merge unchanged manifest evidence, and recompute the
80    /// revision. Plans that can affect selection or directory structure fall
81    /// back to a complete scan.
82    ///
83    /// # Errors
84    ///
85    /// Returns the same errors as [`Self::scan`].
86    pub fn scan_watch_plan(self, previous: &ScanReport, plan: &WatchPlan) -> Result<ScanReport> {
87        watch_update::scan_watch_plan(&self.root, &self.options, previous, plan, &self.runtime)
88    }
89}