Use rustfmt to fix outstanding style issues

This commit is contained in:
Matt Green 2016-10-23 20:12:48 -04:00
parent 6a812d31b4
commit d10b790c35
6 changed files with 110 additions and 103 deletions

View File

@ -33,48 +33,48 @@ pub fn get_args() -> Args {
.multiple(true) .multiple(true)
.required(true)) .required(true))
.arg(Arg::with_name("extensions") .arg(Arg::with_name("extensions")
.help("Comma-separated list of file extensions to watch (js,css,html)") .help("Comma-separated list of file extensions to watch (js,css,html)")
.short("e") .short("e")
.long("exts") .long("exts")
.takes_value(true)) .takes_value(true))
.arg(Arg::with_name("clear") .arg(Arg::with_name("clear")
.help("Clear screen before executing command") .help("Clear screen before executing command")
.short("c") .short("c")
.long("clear")) .long("clear"))
.arg(Arg::with_name("restart") .arg(Arg::with_name("restart")
.help("Restart the process if it's still running") .help("Restart the process if it's still running")
.short("r") .short("r")
.long("restart")) .long("restart"))
.arg(Arg::with_name("debug") .arg(Arg::with_name("debug")
.help("Print debugging messages to stderr") .help("Print debugging messages to stderr")
.short("d") .short("d")
.long("debug")) .long("debug"))
.arg(Arg::with_name("filter") .arg(Arg::with_name("filter")
.help("Ignore all modifications except those matching the pattern") .help("Ignore all modifications except those matching the pattern")
.short("f") .short("f")
.long("filter") .long("filter")
.number_of_values(1) .number_of_values(1)
.multiple(true) .multiple(true)
.takes_value(true) .takes_value(true)
.value_name("pattern")) .value_name("pattern"))
.arg(Arg::with_name("ignore") .arg(Arg::with_name("ignore")
.help("Ignore modifications to paths matching the pattern") .help("Ignore modifications to paths matching the pattern")
.short("i") .short("i")
.long("ignore") .long("ignore")
.number_of_values(1) .number_of_values(1)
.multiple(true) .multiple(true)
.takes_value(true) .takes_value(true)
.value_name("pattern")) .value_name("pattern"))
.arg(Arg::with_name("no-vcs-ignore") .arg(Arg::with_name("no-vcs-ignore")
.help("Skip auto-loading of .gitignore files for filtering") .help("Skip auto-loading of .gitignore files for filtering")
.long("no-vcs-ignore")) .long("no-vcs-ignore"))
.arg(Arg::with_name("run-initially") .arg(Arg::with_name("run-initially")
.help("Run command initially, before first file change") .help("Run command initially, before first file change")
.long("run-initially")) .long("run-initially"))
.arg(Arg::with_name("poll") .arg(Arg::with_name("poll")
.help("Forces polling mode") .help("Forces polling mode")
.long("force-poll") .long("force-poll")
.value_name("interval")) .value_name("interval"))
.get_matches(); .get_matches();
let cmd = values_t!(args.values_of("command"), String).unwrap().join(" "); let cmd = values_t!(args.values_of("command"), String).unwrap().join(" ");
@ -83,10 +83,9 @@ pub fn get_args() -> Args {
if let Some(extensions) = args.values_of("extensions") { if let Some(extensions) = args.values_of("extensions") {
for exts in extensions { for exts in extensions {
filters.extend(exts filters.extend(exts.split(",")
.split(",") .filter(|ext| !ext.is_empty())
.filter(|ext| !ext.is_empty()) .map(|ext| format!("*.{}", ext.replace(".", ""))));
.map(|ext| format!("*.{}", ext.replace(".", ""))));
} }
} }
@ -102,7 +101,9 @@ pub fn get_args() -> Args {
ignores.extend(values_t!(args.values_of("ignore"), String).unwrap_or(vec![])); ignores.extend(values_t!(args.values_of("ignore"), String).unwrap_or(vec![]));
let poll_interval = if args.occurrences_of("poll") > 0 { let poll_interval = if args.occurrences_of("poll") > 0 {
value_t!(args.value_of("poll"), u32).unwrap_or_else(|e| e.exit()) value_t!(args.value_of("poll"), u32).unwrap_or_else(|e| e.exit())
} else { 1000 }; } else {
1000
};
Args { Args {
cmd: cmd, cmd: cmd,
@ -115,6 +116,6 @@ pub fn get_args() -> Args {
run_initially: args.is_present("run-initially"), run_initially: args.is_present("run-initially"),
no_vcs_ignore: args.is_present("no-vcs-ignore"), no_vcs_ignore: args.is_present("no-vcs-ignore"),
poll: args.occurrences_of("poll") > 0, poll: args.occurrences_of("poll") > 0,
poll_interval: poll_interval poll_interval: poll_interval,
} }
} }

View File

@ -8,7 +8,7 @@ use std::path::{Path, PathBuf};
// Immutable, ordered set of Patterns // Immutable, ordered set of Patterns
// Used to implement whitelisting // Used to implement whitelisting
pub struct PatternSet { pub struct PatternSet {
patterns: Vec<Pattern> patterns: Vec<Pattern>,
} }
// Represents a single gitignore rule // Represents a single gitignore rule
@ -23,7 +23,7 @@ struct Pattern {
whitelist: bool, whitelist: bool,
#[allow(dead_code)] #[allow(dead_code)]
directory: bool, directory: bool,
anchored: bool anchored: bool,
} }
#[derive(Debug)] #[derive(Debug)]
@ -39,8 +39,7 @@ pub fn parse(path: &Path) -> Result<PatternSet, Error> {
// If we've opened the file, we'll have at least one other path component // If we've opened the file, we'll have at least one other path component
let root = path.parent().unwrap(); let root = path.parent().unwrap();
let patterns = try!(contents let patterns = try!(contents.lines()
.lines()
.filter(|l| !l.is_empty()) .filter(|l| !l.is_empty())
.filter(|l| !l.starts_with("#")) .filter(|l| !l.starts_with("#"))
.map(|l| Pattern::new(l, root)) .map(|l| Pattern::new(l, root))
@ -51,9 +50,7 @@ pub fn parse(path: &Path) -> Result<PatternSet, Error> {
impl PatternSet { impl PatternSet {
fn new(patterns: Vec<Pattern>) -> PatternSet { fn new(patterns: Vec<Pattern>) -> PatternSet {
PatternSet { PatternSet { patterns: patterns }
patterns: patterns
}
} }
// Apply the patterns to the path one-by-one // Apply the patterns to the path one-by-one
@ -70,8 +67,7 @@ impl PatternSet {
if matched { if matched {
if pattern.whitelist { if pattern.whitelist {
excluded = false; excluded = false;
} } else {
else {
excluded = true; excluded = true;
// We can stop running rules in this case // We can stop running rules in this case
@ -93,17 +89,23 @@ impl Pattern {
let whitelisted = if normalized.starts_with('!') { let whitelisted = if normalized.starts_with('!') {
normalized.remove(0); normalized.remove(0);
true true
} else { false }; } else {
false
};
let anchored = if normalized.starts_with('/') { let anchored = if normalized.starts_with('/') {
normalized.remove(0); normalized.remove(0);
true true
} else { false }; } else {
false
};
let directory = if normalized.ends_with('/') { let directory = if normalized.ends_with('/') {
normalized.pop(); normalized.pop();
true true
} else { false }; } else {
false
};
if normalized.starts_with("\\#") || normalized.starts_with("\\!") { if normalized.starts_with("\\#") || normalized.starts_with("\\!") {
normalized.remove(0); normalized.remove(0);
@ -117,7 +119,7 @@ impl Pattern {
root: root.to_path_buf(), root: root.to_path_buf(),
whitelist: whitelisted, whitelist: whitelisted,
directory: directory, directory: directory,
anchored: anchored anchored: anchored,
}) })
} }
@ -125,12 +127,12 @@ impl Pattern {
let options = glob::MatchOptions { let options = glob::MatchOptions {
case_sensitive: false, case_sensitive: false,
require_literal_separator: true, require_literal_separator: true,
require_literal_leading_dot: false require_literal_leading_dot: false,
}; };
let stripped_path = match path.strip_prefix(&self.root) { let stripped_path = match path.strip_prefix(&self.root) {
Ok(p) => p, Ok(p) => p,
Err(_) => return false Err(_) => return false,
}; };
let mut result = false; let mut result = false;
@ -138,16 +140,13 @@ impl Pattern {
if self.anchored { if self.anchored {
let first_component = stripped_path.iter().next(); let first_component = stripped_path.iter().next();
result = match first_component { result = match first_component {
Some(s) => self.pattern.matches_path_with(Path::new(&s), &options), Some(s) => self.pattern.matches_path_with(Path::new(&s), &options),
None => false None => false,
} }
} } else if !self.str.contains('/') {
else if !self.str.contains('/') { result = stripped_path.iter()
result = stripped_path.iter().any(|c| { .any(|c| self.pattern.matches_path_with(Path::new(c), &options));
self.pattern.matches_path_with(Path::new(c), &options) } else if self.pattern.matches_path_with(stripped_path, &options) {
});
}
else if self.pattern.matches_path_with(stripped_path, &options) {
result = true; result = true;
} }
@ -167,17 +166,17 @@ impl From<io::Error> for Error {
} }
} }
//fn main() { // fn main() {
//let cwd = env::current_dir().unwrap(); // let cwd = env::current_dir().unwrap();
//let gitignore_file = cwd.join(".gitignore"); // let gitignore_file = cwd.join(".gitignore");
//let file = File::new(&gitignore_file).unwrap(); // let file = File::new(&gitignore_file).unwrap();
//for arg in env::args().skip(1) { // for arg in env::args().skip(1) {
//let path = cwd.join(&arg); // let path = cwd.join(&arg);
//let matches = file.is_excluded(&path); // let matches = file.is_excluded(&path);
//println!("File: {}, Excluded: {}", arg, matches); // println!("File: {}, Excluded: {}", arg, matches);
//} // }
//} // }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
@ -290,4 +289,3 @@ mod tests {
assert!(!set.is_excluded(&base_dir().join("target").join("foo.txt"))); assert!(!set.is_excluded(&base_dir().join("target").join("foo.txt")));
} }
} }

View File

@ -1,15 +1,21 @@
#![feature(process_exec)] #![feature(process_exec)]
#[macro_use] extern crate clap; #[macro_use]
extern crate clap;
extern crate env_logger; extern crate env_logger;
extern crate libc; extern crate libc;
#[macro_use] extern crate log; #[macro_use]
#[macro_use] extern crate lazy_static; extern crate log;
#[macro_use]
extern crate lazy_static;
extern crate notify; extern crate notify;
#[cfg(unix)] extern crate nix; #[cfg(unix)]
#[cfg(windows)] extern crate winapi; extern crate nix;
#[cfg(windows)] extern crate kernel32; #[cfg(windows)]
extern crate winapi;
#[cfg(windows)]
extern crate kernel32;
mod args; mod args;
mod gitignore; mod gitignore;
@ -51,11 +57,10 @@ fn init_logger(debug: bool) {
let level = if debug { let level = if debug {
log::LogLevelFilter::Debug log::LogLevelFilter::Debug
} else { } else {
log::LogLevelFilter::Warn log::LogLevelFilter::Warn
}; };
log_builder log_builder.format(|r| format!("*** {}", r.args()))
.format(|r| format!("*** {}", r.args()))
.filter(None, level); .filter(None, level);
log_builder.init().expect("unable to initialize logger"); log_builder.init().expect("unable to initialize logger");
} }
@ -79,7 +84,8 @@ fn main() {
} }
} }
let mut filter = NotificationFilter::new(&cwd, gitignore_file).expect("unable to create notification filter"); let mut filter = NotificationFilter::new(&cwd, gitignore_file)
.expect("unable to create notification filter");
for f in args.filters { for f in args.filters {
filter.add_filter(&f).expect("bad filter"); filter.add_filter(&f).expect("bad filter");
@ -99,8 +105,8 @@ fn main() {
for path in args.paths { for path in args.paths {
match Path::new(&path).canonicalize() { match Path::new(&path).canonicalize() {
Ok(canonicalized) => watcher.watch(canonicalized).expect("unable to watch path"), Ok(canonicalized) => watcher.watch(canonicalized).expect("unable to watch path"),
Err(_) => { Err(_) => {
println!("invalid path: {}", path); println!("invalid path: {}", path);
return; return;
} }
@ -144,7 +150,7 @@ fn wait(rx: &Receiver<Event>, filter: &NotificationFilter) -> Result<Event, Recv
// Drain rx buffer and drop them // Drain rx buffer and drop them
while let Ok(_) = rx.try_recv() { while let Ok(_) = rx.try_recv() {
// nothing to do here // nothing to do here
} }
return Ok(e); return Ok(e);

View File

@ -2,32 +2,34 @@ extern crate glob;
use gitignore; use gitignore;
use std::io; use std::io;
use std::path::{Path,PathBuf}; use std::path::{Path, PathBuf};
use self::glob::{Pattern,PatternError}; use self::glob::{Pattern, PatternError};
pub struct NotificationFilter { pub struct NotificationFilter {
cwd: PathBuf, 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 NotificationError {
BadPattern(PatternError), BadPattern(PatternError),
Io(io::Error) Io(io::Error),
} }
impl NotificationFilter { impl NotificationFilter {
pub fn new(current_dir: &Path, ignore_file: Option<gitignore::PatternSet>) -> Result<NotificationFilter, io::Error> { pub fn new(current_dir: &Path,
ignore_file: Option<gitignore::PatternSet>)
-> Result<NotificationFilter, io::Error> {
let canonicalized = try!(current_dir.canonicalize()); let canonicalized = try!(current_dir.canonicalize());
Ok(NotificationFilter { Ok(NotificationFilter {
cwd: canonicalized, cwd: canonicalized,
filters: vec![], filters: vec![],
ignores: vec![], ignores: vec![],
ignore_file: ignore_file ignore_file: ignore_file,
}) })
} }

View File

@ -11,7 +11,7 @@ impl Runner {
Runner { Runner {
process: None, process: None,
restart: restart, restart: restart,
cls: clear cls: clear,
} }
} }
@ -38,7 +38,7 @@ impl Runner {
fn kill(&mut self) { fn kill(&mut self) {
use libc; use libc;
extern { extern "C" {
fn killpg(pgrp: libc::pid_t, sig: libc::c_int) -> libc::c_int; fn killpg(pgrp: libc::pid_t, sig: libc::c_int) -> libc::c_int;
} }
@ -79,8 +79,10 @@ impl Runner {
debug!("Executing: {}", cmd); debug!("Executing: {}", cmd);
command command.before_exec(|| unsafe {
.before_exec(|| unsafe { libc::setpgid(0, 0); Ok(()) }) libc::setpgid(0, 0);
Ok(())
})
.spawn() .spawn()
.ok() .ok()
} }
@ -109,7 +111,7 @@ impl Runner {
#[cfg(target_family = "unix")] #[cfg(target_family = "unix")]
fn wait(&mut self) { fn wait(&mut self) {
use nix::sys::wait::{waitpid}; use nix::sys::wait::waitpid;
if let Some(ref mut child) = self.process { if let Some(ref mut child) = self.process {
debug!("Waiting for child process (pid: {})", child.id()); debug!("Waiting for child process (pid: {})", child.id());

View File

@ -10,7 +10,7 @@ use notify::{PollWatcher, RecommendedWatcher};
/// (e.g. polymorphically). This has the nice side effect of separating out /// (e.g. polymorphically). This has the nice side effect of separating out
/// all coupling to the notify crate into this module. /// all coupling to the notify crate into this module.
pub struct Watcher { pub struct Watcher {
watcher_impl: WatcherImpl watcher_impl: WatcherImpl,
} }
pub use notify::Event; pub use notify::Event;
@ -18,7 +18,7 @@ pub use notify::Error;
enum WatcherImpl { enum WatcherImpl {
Recommended(RecommendedWatcher), Recommended(RecommendedWatcher),
Poll(PollWatcher) Poll(PollWatcher),
} }
impl Watcher { impl Watcher {
@ -31,9 +31,7 @@ impl Watcher {
WatcherImpl::Recommended(try!(RecommendedWatcher::new(tx))) WatcherImpl::Recommended(try!(RecommendedWatcher::new(tx)))
}; };
Ok(self::Watcher { Ok(self::Watcher { watcher_impl: imp })
watcher_impl: imp
})
} }
pub fn is_polling(&self) -> bool { pub fn is_polling(&self) -> bool {
@ -49,7 +47,7 @@ impl Watcher {
match self.watcher_impl { match self.watcher_impl {
WatcherImpl::Recommended(ref mut w) => w.watch(path), WatcherImpl::Recommended(ref mut w) => w.watch(path),
WatcherImpl::Poll(ref mut w) => w.watch(path) WatcherImpl::Poll(ref mut w) => w.watch(path),
} }
} }
} }