Skip to main content

weavatrix_scan/selection/
construction.rs

1use super::{
2    Error, Path, RepositoryMatcher, Result, ScanOptions, SelectionMatcher, directory_info, fs,
3};
4
5impl SelectionMatcher {
6    /// Builds a matcher with [`ScanOptions::default`].
7    ///
8    /// # Errors
9    ///
10    /// Returns an error when the root or required ignore configuration cannot
11    /// be read.
12    pub fn new(root: impl AsRef<Path>) -> Result<Self> {
13        Self::with_options(root, &ScanOptions::default())
14    }
15
16    /// Builds a matcher from the same options used by [`crate::Scanner`].
17    ///
18    /// # Errors
19    ///
20    /// Returns an error when the root or required ignore configuration cannot
21    /// be read.
22    pub fn with_options(root: impl AsRef<Path>, options: &ScanOptions) -> Result<Self> {
23        let repository = RepositoryMatcher::with_options(root, options)?;
24        let root_file_system = if options.walk.same_file_system {
25            let metadata = fs::metadata(repository.root())
26                .map_err(|source| Error::io(repository.root(), source))?;
27            Some(
28                directory_info(repository.root(), &metadata)
29                    .map_err(|source| Error::io(repository.root(), source))?
30                    .file_system,
31            )
32        } else {
33            None
34        };
35        Ok(Self {
36            repository,
37            options: options.clone(),
38            root_file_system,
39        })
40    }
41}