Skip to main content

driven/context/
indexer.rs

1//! Codebase indexer for binary format
2
3use crate::Result;
4use std::path::Path;
5
6/// Indexes a codebase into binary format for fast access
7#[derive(Debug, Default)]
8pub struct CodebaseIndexer {
9    /// Whether to use SIMD acceleration
10    use_simd: bool,
11}
12
13impl CodebaseIndexer {
14    /// Create a new indexer
15    pub fn new() -> Self {
16        Self { use_simd: false }
17    }
18
19    /// Enable SIMD acceleration
20    pub fn with_simd(mut self, enabled: bool) -> Self {
21        self.use_simd = enabled;
22        self
23    }
24
25    /// Index a project directory
26    pub fn index(&self, _path: &Path) -> Result<CodebaseIndex> {
27        // TODO: Implement binary indexing
28        Ok(CodebaseIndex::default())
29    }
30
31    /// Load an existing index
32    pub fn load(&self, path: &Path) -> Result<CodebaseIndex> {
33        let data = std::fs::read(path)?;
34        CodebaseIndex::from_bytes(&data)
35    }
36}
37
38/// Binary codebase index
39#[derive(Debug, Default)]
40pub struct CodebaseIndex {
41    /// Version of the index format
42    pub version: u32,
43    /// Number of files indexed
44    pub file_count: u32,
45    /// Raw index data
46    data: Vec<u8>,
47}
48
49impl CodebaseIndex {
50    /// Create from binary data
51    pub fn from_bytes(data: &[u8]) -> Result<Self> {
52        if data.len() < 8 {
53            return Ok(Self::default());
54        }
55
56        let version = u32::from_le_bytes([data[0], data[1], data[2], data[3]]);
57        let file_count = u32::from_le_bytes([data[4], data[5], data[6], data[7]]);
58
59        Ok(Self {
60            version,
61            file_count,
62            data: data[8..].to_vec(),
63        })
64    }
65
66    /// Serialize to binary
67    pub fn to_bytes(&self) -> Vec<u8> {
68        let mut bytes = Vec::with_capacity(8 + self.data.len());
69        bytes.extend_from_slice(&self.version.to_le_bytes());
70        bytes.extend_from_slice(&self.file_count.to_le_bytes());
71        bytes.extend_from_slice(&self.data);
72        bytes
73    }
74
75    /// Save index to file
76    pub fn save(&self, path: &Path) -> Result<()> {
77        std::fs::write(path, self.to_bytes())?;
78        Ok(())
79    }
80
81    /// Get the file count
82    pub fn file_count(&self) -> u32 {
83        self.file_count
84    }
85
86    /// Get the size of the index in bytes
87    pub fn size_bytes(&self) -> usize {
88        8 + self.data.len()
89    }
90}
91
92#[cfg(test)]
93mod tests {
94    use super::*;
95
96    #[test]
97    fn test_indexer_new() {
98        let indexer = CodebaseIndexer::new();
99        assert!(!indexer.use_simd);
100    }
101
102    #[test]
103    fn test_index_roundtrip() {
104        let index = CodebaseIndex {
105            version: 1,
106            file_count: 42,
107            data: vec![1, 2, 3, 4],
108        };
109
110        let bytes = index.to_bytes();
111        let loaded = CodebaseIndex::from_bytes(&bytes).unwrap();
112
113        assert_eq!(loaded.version, 1);
114        assert_eq!(loaded.file_count, 42);
115    }
116}