hoard/filters/
mod.rs

1//! Provides filters for determining whether a path should be backed up or not.
2
3use crate::hoard::PileConfig;
4use crate::paths::{RelativePath, SystemPath};
5
6pub(crate) mod ignore;
7
8/// The [`Filter`] trait provides a common interface for all filters.
9pub trait Filter: Sized {
10    /// Creates a new instance of something that implements [`Filter`].
11    ///
12    /// # Errors
13    ///
14    /// Any errors that may occur while creating the new filter.
15    fn new(pile_config: &PileConfig) -> Self;
16    /// Whether or not the file should be kept (backed up).
17    fn keep(&self, prefix: &SystemPath, path: &RelativePath) -> bool;
18}
19
20/// A wrapper for all implmented filters.
21#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
22pub struct Filters {
23    ignore: ignore::IgnoreFilter,
24}
25
26impl Filter for Filters {
27    #[tracing::instrument]
28    fn new(pile_config: &PileConfig) -> Self {
29        let ignore = ignore::IgnoreFilter::new(pile_config);
30        Self { ignore }
31    }
32
33    #[tracing::instrument(name = "run_filters")]
34    fn keep(&self, prefix: &SystemPath, path: &RelativePath) -> bool {
35        self.ignore.keep(prefix, path)
36    }
37}
38
39#[cfg(test)]
40mod tests {
41    use super::*;
42
43    #[test]
44    fn test_filters_derives() {
45        let config = PileConfig {
46            ignore: vec![glob::Pattern::new("valid/**").unwrap()],
47            ..PileConfig::default()
48        };
49        let filters = Filters::new(&config);
50        assert!(format!("{filters:?}").contains("Filters"));
51        assert_eq!(filters.clone().ignore, filters.ignore);
52    }
53}