acp/
watch.rs

1//! @acp:module "File Watcher"
2//! @acp:summary "Watches for file changes and triggers incremental updates"
3//! @acp:domain cli
4//! @acp:layer service
5//!
6//! Watches for file changes and updates cache/vars incrementally.
7
8use std::path::Path;
9use std::sync::mpsc;
10
11use console::style;
12use notify::{Config, RecommendedWatcher, RecursiveMode, Watcher};
13
14use crate::config::Config as AcpConfig;
15use crate::error::Result;
16
17/// File watcher for incremental updates
18pub struct FileWatcher {
19    _config: AcpConfig,
20}
21
22impl FileWatcher {
23    pub fn new(config: AcpConfig) -> Self {
24        Self { _config: config }
25    }
26
27    /// Start watching for changes
28    pub fn watch<P: AsRef<Path>>(&self, root: P) -> Result<()> {
29        let (tx, rx) = mpsc::channel();
30
31        let mut watcher = RecommendedWatcher::new(tx, Config::default())
32            .map_err(|e| crate::error::AcpError::Other(e.to_string()))?;
33
34        watcher
35            .watch(root.as_ref(), RecursiveMode::Recursive)
36            .map_err(|e| crate::error::AcpError::Other(e.to_string()))?;
37
38        println!("Watching for changes...");
39
40        loop {
41            match rx.recv() {
42                Ok(event) => {
43                    match event {
44                        Ok(event) => {
45                            println!("Change detected: {:?}", event);
46                            // TODO: Incremental update based on event.kind
47                        }
48                        Err(e) => {
49                            eprintln!("{} Watch error: {}", style("✗").red(), e);
50                        }
51                    }
52                }
53                Err(e) => {
54                    eprintln!("{} Channel error: {}", style("✗").red(), e);
55                    break;
56                }
57            }
58        }
59
60        Ok(())
61    }
62}