Make NotificationFilter immutable

This commit is contained in:
Matt Green 2016-10-27 08:27:16 -04:00
parent fdc31d4b02
commit 4370d41e71
2 changed files with 32 additions and 42 deletions

View File

@ -91,17 +91,9 @@ fn main() {
} }
} }
let mut filter = NotificationFilter::new(&cwd, gitignore_file) let filter = NotificationFilter::new(&cwd, args.filters, args.ignores, gitignore_file)
.expect("unable to create notification filter"); .expect("unable to create notification filter");
for f in args.filters {
filter.add_filter(&f).expect("bad filter");
}
for i in args.ignores {
filter.add_ignore(&i).expect("bad ignore pattern");
}
let (tx, rx) = channel(); let (tx, rx) = channel();
let mut watcher = Watcher::new(tx, args.poll, args.poll_interval) let mut watcher = Watcher::new(tx, args.poll, args.poll_interval)
.expect("unable to create watcher"); .expect("unable to create watcher");

View File

@ -7,54 +7,52 @@ use std::path::{Path, PathBuf};
use self::glob::{Pattern, PatternError}; use self::glob::{Pattern, PatternError};
pub struct NotificationFilter { pub struct NotificationFilter {
cwd: PathBuf,
filters: Vec<Pattern>, filters: Vec<Pattern>,
ignores: Vec<Pattern>, ignores: Vec<Pattern>,
ignore_file: Option<gitignore::PatternSet>, ignore_file: Option<gitignore::PatternSet>,
} }
#[derive(Debug)] #[derive(Debug)]
pub enum NotificationError { pub enum Error {
BadPattern(PatternError), BadPattern(PatternError),
Io(io::Error), Io(io::Error),
} }
impl NotificationFilter { impl NotificationFilter {
pub fn new(current_dir: &Path, pub fn new(current_dir: &Path,
filters: Vec<String>,
ignores: Vec<String>,
ignore_file: Option<gitignore::PatternSet>) ignore_file: Option<gitignore::PatternSet>)
-> Result<NotificationFilter, io::Error> { -> Result<NotificationFilter, Error> {
let canonicalized = try!(current_dir.canonicalize()); let cwd = try!(current_dir.canonicalize());
let compiled_filters = try!(filters.iter()
.map(|p| NotificationFilter::pattern_for(&cwd, p))
.collect());
for compiled_filter in &compiled_filters {
debug!("Adding filter: {}", compiled_filter);
}
let compiled_ignores = try!(ignores.iter()
.map(|p| NotificationFilter::pattern_for(&cwd, p))
.collect());
for compiled_ignore in &compiled_ignores {
debug!("Adding ignore: {}", compiled_ignore);
}
Ok(NotificationFilter { Ok(NotificationFilter {
cwd: canonicalized, filters: compiled_filters,
filters: vec![], ignores: compiled_ignores,
ignores: vec![],
ignore_file: ignore_file, ignore_file: ignore_file,
}) })
} }
pub fn add_filter(&mut self, pattern: &str) -> Result<(), NotificationError> { fn pattern_for(cwd: &PathBuf, p: &str) -> Result<Pattern, PatternError> {
let compiled = try!(self.pattern_for(pattern));
self.filters.push(compiled);
debug!("Adding filter: {}", pattern);
Ok(())
}
pub fn add_ignore(&mut self, pattern: &str) -> Result<(), NotificationError> {
let compiled = try!(self.pattern_for(pattern));
self.ignores.push(compiled);
debug!("Adding ignore: {}", pattern);
Ok(())
}
fn pattern_for(&self, p: &str) -> Result<Pattern, PatternError> {
let mut path = PathBuf::from(p); let mut path = PathBuf::from(p);
if path.is_relative() { if path.is_relative() {
path = self.cwd.join(path.as_path()); path = cwd.join(path.as_path());
} }
if let Ok(metadata) = path.metadata() { if let Ok(metadata) = path.metadata() {
@ -97,14 +95,14 @@ impl NotificationFilter {
} }
} }
impl From<io::Error> for NotificationError { impl From<io::Error> for Error {
fn from(err: io::Error) -> NotificationError { fn from(err: io::Error) -> Error {
NotificationError::Io(err) Error::Io(err)
} }
} }
impl From<PatternError> for NotificationError { impl From<PatternError> for Error {
fn from(err: PatternError) -> NotificationError { fn from(err: PatternError) -> Error {
NotificationError::BadPattern(err) Error::BadPattern(err)
} }
} }