1use anyhow::{Context, Result};
4
5use crate::content_store::ContentReader;
6use crate::trigram::TrigramIndex;
7
8pub fn find_file_id(content_reader: &ContentReader, target_path: &str) -> Option<u32> {
10 for file_id in 0..content_reader.file_count() {
11 if let Some(path) = content_reader.get_file_path(file_id as u32) {
12 if path.to_string_lossy() == target_path {
13 return Some(file_id as u32);
14 }
15 }
16 }
17 None
18}
19
20pub fn rebuild_trigram_index(content_reader: &ContentReader) -> Result<TrigramIndex> {
22 log::debug!("Rebuilding trigram index from {} files", content_reader.file_count());
23 let mut trigram_index = TrigramIndex::new();
24
25 for file_id in 0..content_reader.file_count() {
26 let file_path = content_reader.get_file_path(file_id as u32)
27 .context("Invalid file_id")?
28 .to_path_buf();
29 let content = content_reader.get_file_content(file_id as u32)?;
30
31 let idx = trigram_index.add_file(file_path);
32 trigram_index.index_file(idx, content);
33 }
34
35 trigram_index.finalize();
36 log::debug!("Trigram index rebuilt with {} trigrams", trigram_index.trigram_count());
37
38 Ok(trigram_index)
39}
40
41pub fn normalize_glob_pattern(pattern: &str) -> String {
48 if pattern.starts_with('.') || pattern.starts_with('/') || pattern.starts_with('*') {
49 pattern.to_string()
50 } else {
51 format!("./{}", pattern)
52 }
53}