mirror of
https://github.com/icewind1991/notify-redis.git
synced 2026-06-03 18:24:12 +02:00
use destructuring for args
This commit is contained in:
parent
6c25409d0d
commit
24cf14f50c
1 changed files with 15 additions and 13 deletions
28
src/main.rs
28
src/main.rs
|
|
@ -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 (tx, rx) = channel();
|
||||||
|
|
||||||
let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_secs(2))?;
|
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()?;
|
let con = client.get_connection()?;
|
||||||
|
|
||||||
watcher.watch(path, RecursiveMode::Recursive)?;
|
watcher.watch(path, RecursiveMode::Recursive)?;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
match rx.recv() {
|
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),
|
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) {
|
match format_event(event) {
|
||||||
Some(formatted_event) => {
|
Some(formatted_event) => {
|
||||||
println!("{}", formatted_event);
|
println!("{}", formatted_event);
|
||||||
return con.lpush(list, formatted_event);
|
return con.lpush(list, formatted_event);
|
||||||
},
|
}
|
||||||
None => Ok(())
|
None => Ok(()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn format_event(event: DebouncedEvent) -> Option<String> {
|
fn format_event(event: DebouncedEvent) -> Option<String> {
|
||||||
match event {
|
match event {
|
||||||
DebouncedEvent::Write(path) |
|
DebouncedEvent::Write(path)
|
||||||
DebouncedEvent::Create(path) |
|
| DebouncedEvent::Create(path)
|
||||||
DebouncedEvent::Chmod(path) => Some(format!("write|{}", path.to_str()?)),
|
| DebouncedEvent::Chmod(path) => Some(format!("write|{}", path.to_str()?)),
|
||||||
DebouncedEvent::Rename(from, to) => Some(format!("rename|{}|{}", from.to_str()?, to.to_str()?)),
|
DebouncedEvent::Rename(from, to) => {
|
||||||
|
Some(format!("rename|{}|{}", from.to_str()?, to.to_str()?))
|
||||||
|
}
|
||||||
DebouncedEvent::Remove(path) => Some(format!("remove|{}", path.to_str()?)),
|
DebouncedEvent::Remove(path) => Some(format!("remove|{}", path.to_str()?)),
|
||||||
_ => None
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let args: Vec<_> = env::args().collect();
|
let args: Vec<_> = env::args().collect();
|
||||||
if args.len() == 4 {
|
if let [_, path, redis, list] = args.as_slice() {
|
||||||
if let Err(e) = watch(args[1].to_owned(), args[2].to_owned(), args[3].to_owned()) {
|
if let Err(e) = watch(path, redis, list) {
|
||||||
println!("error: {:?}", e)
|
println!("error: {:?}", e)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue