use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant};
use crate::config::FreshnessConfig;
#[derive(Debug)]
pub struct Debouncer {
window: Duration,
deadlines: HashMap<PathBuf, Instant>,
}
impl Debouncer {
#[must_use]
pub fn new(window: Duration) -> Self {
Self {
window,
deadlines: HashMap::new(),
}
}
pub fn record(&mut self, path: PathBuf, at: Instant) {
self.deadlines.insert(path, at + self.window);
}
pub fn flush_ready(&mut self, now: Instant) -> Vec<PathBuf> {
let mut ready: Vec<PathBuf> = self
.deadlines
.iter()
.filter(|(_, &deadline)| deadline <= now)
.map(|(path, _)| path.clone())
.collect();
for path in &ready {
self.deadlines.remove(path);
}
ready.sort();
ready
}
#[must_use]
pub fn next_deadline(&self) -> Option<Instant> {
self.deadlines.values().copied().min()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.deadlines.is_empty()
}
}
#[derive(Debug, Default, PartialEq, Eq)]
pub struct TierBatch {
pub tree_sitter: Vec<PathBuf>,
pub heavy: Vec<PathBuf>,
}
impl TierBatch {
#[must_use]
pub fn is_empty(&self) -> bool {
self.tree_sitter.is_empty() && self.heavy.is_empty()
}
}
#[derive(Debug)]
pub struct TieredScheduler {
fast: Debouncer,
heavy: Debouncer,
}
impl TieredScheduler {
#[must_use]
pub fn new(fast_window: Duration, heavy_window: Duration) -> Self {
Self {
fast: Debouncer::new(fast_window),
heavy: Debouncer::new(heavy_window),
}
}
#[must_use]
pub fn from_config(freshness: &FreshnessConfig) -> Self {
Self::new(
Duration::from_millis(freshness.debounce_ms),
Duration::from_millis(freshness.heavy_debounce_ms),
)
}
pub fn record(&mut self, path: PathBuf, at: Instant) {
self.fast.record(path.clone(), at);
self.heavy.record(path, at);
}
pub fn poll(&mut self, now: Instant) -> TierBatch {
TierBatch {
tree_sitter: self.fast.flush_ready(now),
heavy: self.heavy.flush_ready(now),
}
}
#[must_use]
pub fn next_deadline(&self) -> Option<Instant> {
match (self.fast.next_deadline(), self.heavy.next_deadline()) {
(Some(a), Some(b)) => Some(a.min(b)),
(a, b) => a.or(b),
}
}
#[must_use]
pub fn is_idle(&self) -> bool {
self.fast.is_empty() && self.heavy.is_empty()
}
}
#[must_use]
pub fn is_watch_relevant(path: &Path) -> bool {
if path.extension().is_none_or(|ext| ext != "rs") {
return false;
}
!path.components().any(|c| {
let s = c.as_os_str();
s == "target" || s == ".git"
})
}
#[cfg(test)]
mod tests {
use super::*;
fn p(s: &str) -> PathBuf {
PathBuf::from(s)
}
#[test]
fn debouncer_holds_until_window_elapses() {
let t0 = Instant::now();
let mut d = Debouncer::new(Duration::from_millis(300));
d.record(p("a.rs"), t0);
assert!(d.flush_ready(t0 + Duration::from_millis(299)).is_empty());
assert_eq!(
d.flush_ready(t0 + Duration::from_millis(300)),
vec![p("a.rs")]
);
assert!(d.flush_ready(t0 + Duration::from_secs(10)).is_empty());
}
#[test]
fn debouncer_coalesces_a_burst() {
let t0 = Instant::now();
let mut d = Debouncer::new(Duration::from_millis(300));
d.record(p("a.rs"), t0);
d.record(p("a.rs"), t0 + Duration::from_millis(100));
d.record(p("a.rs"), t0 + Duration::from_millis(250));
assert!(d.flush_ready(t0 + Duration::from_millis(300)).is_empty());
assert_eq!(
d.flush_ready(t0 + Duration::from_millis(550)),
vec![p("a.rs")]
);
}
#[test]
fn debouncer_next_deadline_tracks_earliest() {
let t0 = Instant::now();
let mut d = Debouncer::new(Duration::from_millis(100));
d.record(p("late.rs"), t0 + Duration::from_millis(50));
d.record(p("early.rs"), t0);
assert_eq!(d.next_deadline(), Some(t0 + Duration::from_millis(100)));
}
#[test]
fn tiered_scheduler_fires_fast_before_heavy() {
let t0 = Instant::now();
let mut s = TieredScheduler::new(Duration::from_millis(300), Duration::from_millis(1500));
s.record(p("x.rs"), t0);
let batch = s.poll(t0 + Duration::from_millis(400));
assert_eq!(batch.tree_sitter, vec![p("x.rs")]);
assert!(batch.heavy.is_empty());
let batch = s.poll(t0 + Duration::from_millis(1600));
assert!(batch.tree_sitter.is_empty());
assert_eq!(batch.heavy, vec![p("x.rs")]);
}
#[test]
fn tiered_scheduler_defers_heavy_across_a_burst() {
let t0 = Instant::now();
let mut s = TieredScheduler::new(Duration::from_millis(300), Duration::from_millis(1000));
s.record(p("x.rs"), t0);
s.record(p("x.rs"), t0 + Duration::from_millis(500));
s.record(p("x.rs"), t0 + Duration::from_millis(1000));
let batch = s.poll(t0 + Duration::from_millis(1200));
assert!(batch.heavy.is_empty(), "heavy must defer during a burst");
let batch = s.poll(t0 + Duration::from_millis(2100));
assert_eq!(batch.heavy, vec![p("x.rs")]);
}
#[test]
fn relevance_filter_accepts_only_source_rust() {
assert!(is_watch_relevant(Path::new("src/lib.rs")));
assert!(!is_watch_relevant(Path::new("README.md")));
assert!(!is_watch_relevant(Path::new("target/debug/build.rs")));
assert!(!is_watch_relevant(Path::new(".git/HEAD")));
}
}