use destructuring for args

This commit is contained in:
Robin Appelman 2019-03-28 18:53:51 +01:00
commit 24cf14f50c

View file

@ -26,48 +26,50 @@ impl From<redis::RedisError> for WatchError {
}
}
fn watch(path: String, redis_connect: String, redis_list: String) -> Result<(), WatchError> {
fn watch(path: &str, redis_connect: &str, redis_list: &str) -> Result<(), WatchError> {
let (tx, rx) = channel();
let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_secs(2))?;
let client = Client::open(redis_connect.as_ref())?;
let client = Client::open(redis_connect)?;
let con = client.get_connection()?;
watcher.watch(path, RecursiveMode::Recursive)?;
loop {
match rx.recv() {
Ok(event) => push_event(event, &con, &redis_list)?,
Ok(event) => push_event(event, &con, redis_list)?,
Err(e) => println!("watch error: {:?}", e),
}
}
}
fn push_event(event: DebouncedEvent, con: &Connection, list: &String) -> RedisResult<()> {
fn push_event(event: DebouncedEvent, con: &Connection, list: &str) -> RedisResult<()> {
match format_event(event) {
Some(formatted_event) => {
println!("{}", formatted_event);
return con.lpush(list, formatted_event);
},
None => Ok(())
}
None => Ok(()),
}
}
fn format_event(event: DebouncedEvent) -> Option<String> {
match event {
DebouncedEvent::Write(path) |
DebouncedEvent::Create(path) |
DebouncedEvent::Chmod(path) => Some(format!("write|{}", path.to_str()?)),
DebouncedEvent::Rename(from, to) => Some(format!("rename|{}|{}", from.to_str()?, to.to_str()?)),
DebouncedEvent::Write(path)
| DebouncedEvent::Create(path)
| DebouncedEvent::Chmod(path) => Some(format!("write|{}", path.to_str()?)),
DebouncedEvent::Rename(from, to) => {
Some(format!("rename|{}|{}", from.to_str()?, to.to_str()?))
}
DebouncedEvent::Remove(path) => Some(format!("remove|{}", path.to_str()?)),
_ => None
_ => None,
}
}
fn main() {
let args: Vec<_> = env::args().collect();
if args.len() == 4 {
if let Err(e) = watch(args[1].to_owned(), args[2].to_owned(), args[3].to_owned()) {
if let [_, path, redis, list] = args.as_slice() {
if let Err(e) = watch(path, redis, list) {
println!("error: {:?}", e)
}
} else {