nextsync/src/store/ignorer.rs

58 lines
1.6 KiB
Rust

use crate::store::nsignore::{get_nsignore_file, get_nsignore_rules, is_file_ignored};
use std::path::PathBuf;
use std::sync::OnceLock;
pub struct Ignorer {
/// Path of this level
path: PathBuf,
/// Should it use the nextsyncignore file
use_nsignore: bool,
/// Nsignore's rules
rules: OnceLock<Vec<String>>,
childs: Option<Vec<Box<Ignorer>>>,
}
impl Ignorer {
pub fn new(path: &PathBuf) -> Self {
Ignorer {
path: path.to_path_buf(),
use_nsignore: true,
rules: OnceLock::new(),
childs: None,
}
}
pub fn active_nsignore(&mut self, active: bool) {
self.use_nsignore = active;
}
fn get_rules(&mut self) -> &Vec<String> {
self.rules
.get_or_init(|| match get_nsignore_file(&self.path) {
Some(nsignore_path) => get_nsignore_rules(&nsignore_path),
None => Vec::new(),
})
}
fn is_config_file(&self, path: &PathBuf) -> bool {
path.ends_with(".nextsync")
}
/// Returns whether a file/dir is ignored or not
///
/// * `path`:
fn is_ignored(&mut self, path: &PathBuf) -> bool {
is_file_ignored(path.to_str().unwrap(), self.get_rules())
}
/// Returns whether a file/dir should be ignored by the program or not
///
/// This takes advantage of the `use_nsignore` variable set by
/// `active_nsignore`.
///
/// * `path`:
pub fn should_ignore(&mut self, path: &PathBuf) -> bool {
self.is_config_file(path) || (self.use_nsignore && self.is_ignored(path))
}
}