1use crate::hoard::PileConfig;
4use crate::paths::{RelativePath, SystemPath};
5
6pub(crate) mod ignore;
7
8pub trait Filter: Sized {
10 fn new(pile_config: &PileConfig) -> Self;
16 fn keep(&self, prefix: &SystemPath, path: &RelativePath) -> bool;
18}
19
20#[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}