use std::{
collections::BTreeSet,
path::{Path, PathBuf},
time::Duration,
};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AutoreloadConfig {
pub enabled: bool,
pub poll_interval: Duration,
}
impl Default for AutoreloadConfig {
fn default() -> Self {
Self {
enabled: true,
poll_interval: Duration::from_secs(1),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FileWatcherStub {
pub config: AutoreloadConfig,
watched_paths: BTreeSet<PathBuf>,
}
impl FileWatcherStub {
#[must_use]
pub fn new(config: AutoreloadConfig) -> Self {
Self {
config,
watched_paths: BTreeSet::new(),
}
}
pub fn watch(&mut self, path: impl AsRef<Path>) -> bool {
self.watched_paths.insert(path.as_ref().to_path_buf())
}
#[must_use]
pub fn watched_paths(&self) -> Vec<PathBuf> {
self.watched_paths.iter().cloned().collect()
}
#[must_use]
pub fn poll_changes(&self) -> Vec<PathBuf> {
Vec::new()
}
}
#[cfg(test)]
mod tests {
use std::time::Duration;
use super::{AutoreloadConfig, FileWatcherStub};
#[test]
fn autoreload_config_defaults_to_enabled_one_second_polling() {
let config = AutoreloadConfig::default();
assert!(config.enabled);
assert_eq!(config.poll_interval, Duration::from_secs(1));
}
#[test]
fn file_watcher_stub_tracks_unique_paths() {
let mut watcher = FileWatcherStub::new(AutoreloadConfig::default());
assert!(watcher.watch("src/lib.rs"));
assert!(!watcher.watch("src/lib.rs"));
assert_eq!(
watcher.watched_paths(),
vec![std::path::PathBuf::from("src/lib.rs")]
);
}
#[test]
fn file_watcher_stub_reports_no_changes() {
let watcher = FileWatcherStub::new(AutoreloadConfig::default());
assert!(watcher.poll_changes().is_empty());
}
}