first batch of dependency updates

This commit is contained in:
2023-01-26 22:45:34 +01:00
parent daf2a02cd8
commit 5efa8b3465
5 changed files with 842 additions and 960 deletions

View File

@@ -1,18 +1,21 @@
use anyhow::{anyhow, bail, Result};
use notify::{DebouncedEvent, RecommendedWatcher, Watcher};
use std::{collections::BTreeSet, path::PathBuf, sync::mpsc, time::Duration};
use notify::{Event, EventKind, RecommendedWatcher, Watcher};
use std::{collections::BTreeSet, path::PathBuf, sync::mpsc};
pub struct FileWatcher {
watcher: RecommendedWatcher,
watched_files: BTreeSet<PathBuf>,
directories: BTreeSet<PathBuf>,
rx: mpsc::Receiver<DebouncedEvent>,
rx: mpsc::Receiver<Event>,
}
impl FileWatcher {
pub fn new() -> Result<FileWatcher> {
let (tx, rx) = mpsc::channel();
let watcher = notify::watcher(tx, Duration::from_millis(100))?;
let watcher = notify::recommended_watcher(move |res| match res {
Ok(event) => _ = tx.send(event),
Err(err) => eprintln!("Error watching for file changes: {err}"),
})?;
Ok(FileWatcher {
watcher,
watched_files: BTreeSet::new(),
@@ -36,16 +39,20 @@ impl FileWatcher {
}
pub fn poll_changed_file(&self) -> Result<Option<PathBuf>> {
let event = self.rx.try_recv();
match event {
Ok(DebouncedEvent::Create(path) | DebouncedEvent::Write(path)) => {
let handle = same_file::Handle::from_path(&path)?;
for file in &self.watched_files {
if handle == same_file::Handle::from_path(file)? {
return Ok(Some(path));
match self.rx.try_recv() {
Ok(event) => match event.kind {
EventKind::Create(_) | EventKind::Modify(_) => {
for path in event.paths {
let handle = same_file::Handle::from_path(&path)?;
for file in &self.watched_files {
if handle == same_file::Handle::from_path(file)? {
return Ok(Some(path));
}
}
}
}
}
_ => (),
},
Err(mpsc::TryRecvError::Disconnected) => bail!("File watcher disconnected"),
_ => (),
}