use super::Settings;
use std::path::{Path, PathBuf};
impl Settings {
pub(super) fn sync_indexed_path_cache(&mut self) {
self.indexed_paths_cache = self.indexing.indexed_paths.clone();
}
pub fn add_indexed_path(&mut self, path: PathBuf) -> Result<(), String> {
let canonical_path = path
.canonicalize()
.map_err(|e| format!("Invalid path: {e}"))?;
let mut has_descendants = false;
for existing in &self.indexed_paths_cache {
if *existing == canonical_path {
return Err(format!("Path already indexed: {}", path.display()));
}
if canonical_path.starts_with(existing) {
return Err(format!(
"Path already indexed: {} (covered by {})",
path.display(),
existing.display()
));
}
if existing.starts_with(&canonical_path) {
has_descendants = true;
}
}
if has_descendants {
self.indexing
.indexed_paths
.retain(|existing| !existing.starts_with(&canonical_path));
self.indexed_paths_cache
.retain(|existing| !existing.starts_with(&canonical_path));
}
self.indexing.indexed_paths.push(canonical_path.clone());
self.indexed_paths_cache.push(canonical_path);
Ok(())
}
pub fn remove_indexed_path(&mut self, path: &Path) -> Result<(), String> {
let canonical_path = path
.canonicalize()
.map_err(|e| format!("Invalid path: {e}"))?;
let original_len = self.indexing.indexed_paths.len();
self.indexing.indexed_paths.retain(|p| p != &canonical_path);
self.indexed_paths_cache.retain(|p| p != &canonical_path);
if self.indexing.indexed_paths.len() == original_len {
return Err(format!(
"Path not found in indexed paths: {}",
path.display()
));
}
Ok(())
}
pub fn get_indexed_paths(&self) -> Vec<PathBuf> {
self.indexing.indexed_paths.clone()
}
}