Prep fs pathset for future

This commit is contained in:
Félix Saparelli 2021-10-15 01:38:21 +13:00
parent 14b0364135
commit 8f61ac31da
No known key found for this signature in database
GPG Key ID: B948C4BAE44FC474
1 changed files with 36 additions and 6 deletions

View File

@ -4,7 +4,7 @@ use std::{
collections::{HashMap, HashSet}, collections::{HashMap, HashSet},
fs::metadata, fs::metadata,
mem::take, mem::take,
path::PathBuf, path::{Path, PathBuf},
sync::{Arc, Mutex}, sync::{Arc, Mutex},
time::Duration, time::Duration,
}; };
@ -55,10 +55,40 @@ impl Watcher {
#[derive(Clone, Debug, Default)] #[derive(Clone, Debug, Default)]
#[non_exhaustive] #[non_exhaustive]
pub struct WorkingData { pub struct WorkingData {
pub pathset: Vec<PathBuf>, pub pathset: Vec<WatchedPath>,
pub watcher: Watcher, pub watcher: Watcher,
} }
/// A path to watch.
///
/// This is currently only a wrapper around a [`PathBuf`], but may be augmented in the future.
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct WatchedPath(PathBuf);
impl From<PathBuf> for WatchedPath {
fn from(path: PathBuf) -> Self {
Self(path)
}
}
impl From<&Path> for WatchedPath {
fn from(path: &Path) -> Self {
Self(path.into())
}
}
impl From<WatchedPath> for PathBuf {
fn from(path: WatchedPath) -> Self {
path.0
}
}
impl AsRef<Path> for WatchedPath {
fn as_ref(&self) -> &Path {
self.0.as_ref()
}
}
/// Launch the filesystem event worker. /// Launch the filesystem event worker.
/// ///
/// While you can run several, you should only have one. /// While you can run several, you should only have one.
@ -165,7 +195,7 @@ pub async fn worker(
for path in to_drop { for path in to_drop {
trace!(?path, "removing path from the watcher"); trace!(?path, "removing path from the watcher");
if let Err(err) = w.unwatch(&path) { if let Err(err) = w.unwatch(path.as_ref()) {
error!(?err, "notify unwatch() error"); error!(?err, "notify unwatch() error");
for e in notify_multi_path_errors(watcher_type, path, err, true) { for e in notify_multi_path_errors(watcher_type, path, err, true) {
errors.send(e).await?; errors.send(e).await?;
@ -177,7 +207,7 @@ pub async fn worker(
for path in to_watch { for path in to_watch {
trace!(?path, "adding path to the watcher"); trace!(?path, "adding path to the watcher");
if let Err(err) = w.watch(&path, notify::RecursiveMode::Recursive) { if let Err(err) = w.watch(path.as_ref(), notify::RecursiveMode::Recursive) {
error!(?err, "notify watch() error"); error!(?err, "notify watch() error");
for e in notify_multi_path_errors(watcher_type, path, err, false) { for e in notify_multi_path_errors(watcher_type, path, err, false) {
errors.send(e).await?; errors.send(e).await?;
@ -196,13 +226,13 @@ pub async fn worker(
fn notify_multi_path_errors( fn notify_multi_path_errors(
kind: Watcher, kind: Watcher,
path: PathBuf, path: WatchedPath,
mut err: notify::Error, mut err: notify::Error,
rm: bool, rm: bool,
) -> Vec<RuntimeError> { ) -> Vec<RuntimeError> {
let mut paths = take(&mut err.paths); let mut paths = take(&mut err.paths);
if paths.is_empty() { if paths.is_empty() {
paths.push(path); paths.push(path.into());
} }
let generic = err.to_string(); let generic = err.to_string();