ricecoder_refactoring/providers/
lsp_watcher.rs1use crate::error::Result;
7use super::lsp::LspProviderRegistry;
8use std::sync::Arc;
9use std::time::Duration;
10
11#[allow(dead_code)]
18pub struct LspWatcher {
19 registry: Arc<LspProviderRegistry>,
20 check_interval: Duration,
21 running: std::sync::Arc<std::sync::Mutex<bool>>,
22}
23
24impl LspWatcher {
25 pub fn new(registry: Arc<LspProviderRegistry>) -> Self {
27 Self {
28 registry,
29 check_interval: Duration::from_secs(5),
30 running: Arc::new(std::sync::Mutex::new(false)),
31 }
32 }
33
34 pub fn with_interval(registry: Arc<LspProviderRegistry>, interval: Duration) -> Self {
36 Self {
37 registry,
38 check_interval: interval,
39 running: Arc::new(std::sync::Mutex::new(false)),
40 }
41 }
42
43 pub async fn start(&self) -> Result<()> {
49 let mut running = self.running.lock().map_err(|_| {
50 crate::error::RefactoringError::Other("Failed to acquire lock on watcher state".to_string())
51 })?;
52
53 if *running {
54 return Err(crate::error::RefactoringError::Other(
55 "Watcher is already running".to_string(),
56 ));
57 }
58
59 *running = true;
60
61 Ok(())
64 }
65
66 pub async fn stop(&self) -> Result<()> {
68 let mut running = self.running.lock().map_err(|_| {
69 crate::error::RefactoringError::Other("Failed to acquire lock on watcher state".to_string())
70 })?;
71
72 *running = false;
73 Ok(())
74 }
75
76 pub fn is_running(&self) -> Result<bool> {
78 let running = self.running.lock().map_err(|_| {
79 crate::error::RefactoringError::Other("Failed to acquire lock on watcher state".to_string())
80 })?;
81 Ok(*running)
82 }
83
84 pub async fn check_availability(&self) -> Result<()> {
88 Ok(())
95 }
96
97 pub async fn check_configuration(&self) -> Result<()> {
101 Ok(())
109 }
110}
111
112#[allow(dead_code)]
117pub struct ConfigurationWatcher {
118 config_dir: std::path::PathBuf,
119 check_interval: Duration,
120 running: std::sync::Arc<std::sync::Mutex<bool>>,
121}
122
123impl ConfigurationWatcher {
124 pub fn new(config_dir: std::path::PathBuf) -> Self {
126 Self {
127 config_dir,
128 check_interval: Duration::from_secs(5),
129 running: Arc::new(std::sync::Mutex::new(false)),
130 }
131 }
132
133 pub fn with_interval(config_dir: std::path::PathBuf, interval: Duration) -> Self {
135 Self {
136 config_dir,
137 check_interval: interval,
138 running: Arc::new(std::sync::Mutex::new(false)),
139 }
140 }
141
142 pub async fn start(&self) -> Result<()> {
144 let mut running = self.running.lock().map_err(|_| {
145 crate::error::RefactoringError::Other(
146 "Failed to acquire lock on configuration watcher state".to_string(),
147 )
148 })?;
149
150 if *running {
151 return Err(crate::error::RefactoringError::Other(
152 "Configuration watcher is already running".to_string(),
153 ));
154 }
155
156 *running = true;
157 Ok(())
158 }
159
160 pub async fn stop(&self) -> Result<()> {
162 let mut running = self.running.lock().map_err(|_| {
163 crate::error::RefactoringError::Other(
164 "Failed to acquire lock on configuration watcher state".to_string(),
165 )
166 })?;
167
168 *running = false;
169 Ok(())
170 }
171
172 pub fn is_running(&self) -> Result<bool> {
174 let running = self.running.lock().map_err(|_| {
175 crate::error::RefactoringError::Other(
176 "Failed to acquire lock on configuration watcher state".to_string(),
177 )
178 })?;
179 Ok(*running)
180 }
181
182 pub async fn check_changes(&self) -> Result<()> {
184 Ok(())
192 }
193}
194
195#[cfg(test)]
196mod tests {
197 use super::*;
198
199 #[tokio::test]
200 async fn test_lsp_watcher_lifecycle() -> Result<()> {
201 let registry = Arc::new(super::super::lsp::LspProviderRegistry::new());
202 let watcher = LspWatcher::new(registry);
203
204 assert!(!watcher.is_running()?);
205
206 watcher.start().await?;
207 assert!(watcher.is_running()?);
208
209 watcher.stop().await?;
210 assert!(!watcher.is_running()?);
211
212 Ok(())
213 }
214
215 #[tokio::test]
216 async fn test_configuration_watcher_lifecycle() -> Result<()> {
217 let config_dir = std::path::PathBuf::from("/tmp");
218 let watcher = ConfigurationWatcher::new(config_dir);
219
220 assert!(!watcher.is_running()?);
221
222 watcher.start().await?;
223 assert!(watcher.is_running()?);
224
225 watcher.stop().await?;
226 assert!(!watcher.is_running()?);
227
228 Ok(())
229 }
230
231 #[tokio::test]
232 async fn test_watcher_cannot_start_twice() -> Result<()> {
233 let registry = Arc::new(super::super::lsp::LspProviderRegistry::new());
234 let watcher = LspWatcher::new(registry);
235
236 watcher.start().await?;
237 let result = watcher.start().await;
238
239 assert!(result.is_err());
240
241 watcher.stop().await?;
242 Ok(())
243 }
244}