sift-core 0.2.0

Indexed regex search over codebases (library + grep-like CLI)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
pub mod meta;
mod snapshot;
pub mod store;
pub mod trigram;

use std::collections::HashSet;
use std::path::{Path, PathBuf};

use serde::{Deserialize, Serialize};

use crate::search::filter::VisibilityConfig;
use crate::search::output::mode::CandidateCoverage;

pub use trigram::TrigramIndexError;

/// How an index query plan resolves candidates.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
pub enum PlanMode {
    /// The query was narrowed using trigram candidates from the index.
    #[default]
    IndexedCandidates,
    /// No trigrams were usable — all indexed files must be scanned.
    FullScan,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct QueryPlanOutput {
    pub pattern: String,
    pub mode: PlanMode,
}

/// Whether the index was built from a directory or a single file.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
pub enum CorpusKind {
    /// Built from a directory path — all discovered files were indexed.
    #[default]
    Directory,
    /// Built from a single file path — only that file was indexed.
    SingleFile,
}

/// Configuration for building or updating an index over a corpus.
pub struct IndexConfig<'a> {
    pub corpus: CorpusSpec<'a>,
    pub visibility: VisibilityConfig,
}

/// Description of a corpus to index.
pub struct CorpusSpec<'a> {
    pub root: &'a Path,
    pub kind: CorpusKind,
    pub follow_links: bool,
    pub include_paths: &'a [PathBuf],
    pub exclude_paths: &'a [PathBuf],
}

/// Errors specific to the index registry layer.
#[derive(Debug, thiserror::Error)]
pub enum IndexError {
    #[error("invalid index layout: {path}")]
    InvalidLayout { path: PathBuf },

    #[error(transparent)]
    Trigram(#[from] TrigramIndexError),

    #[error("IO error inspecting index path {path}: {source}")]
    Io {
        path: PathBuf,
        source: std::io::Error,
    },

    #[error("unknown index kind: {0}")]
    UnknownIndexKind(String),

    #[error("invalid snapshot manifest at {path}: {source}")]
    InvalidManifest {
        path: PathBuf,
        source: serde_json::Error,
    },
}

/// Tag identifying an index kind for lifecycle dispatch (build, open, update).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum IndexKind {
    Trigram,
}

impl IndexKind {
    pub const ALL: &[Self] = &[Self::Trigram];

    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Trigram => "trigram",
        }
    }

    pub(crate) fn build(self, config: &IndexConfig<'_>, output_dir: &Path) -> crate::Result<()> {
        match self {
            Self::Trigram => {
                trigram::TrigramIndex::build(config, output_dir)?;
                Ok(())
            }
        }
    }

    pub(crate) fn open_from_dir(
        self,
        index_dir: &Path,
        root: &Path,
        corpus_kind: CorpusKind,
    ) -> crate::Result<Index> {
        match self {
            Self::Trigram => Ok(Index::Trigram(trigram::TrigramIndex::open(
                index_dir,
                root,
                corpus_kind,
            )?)),
        }
    }

    /// Returns `true` if a new index was written.
    pub(crate) fn update(
        self,
        snapshot_dir: &Path,
        config: &IndexConfig<'_>,
        output_dir: &Path,
    ) -> crate::Result<bool> {
        let existing_dir = snapshot_dir.join(self.as_str());
        if !existing_dir.exists() {
            self.build(config, output_dir)?;
            return Ok(true);
        }
        let root = config
            .corpus
            .root
            .canonicalize()
            .unwrap_or_else(|_| config.corpus.root.to_path_buf());
        match self {
            Self::Trigram => {
                let existing =
                    trigram::TrigramIndex::open(&existing_dir, &root, config.corpus.kind)?;
                Ok(existing.update(config, output_dir)?.is_some())
            }
        }
    }
}

impl std::fmt::Display for IndexKind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

impl std::str::FromStr for IndexKind {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "trigram" => Ok(Self::Trigram),
            other => Err(format!("unknown index kind: {other}")),
        }
    }
}

/// An opened index instance, used for search-time dispatch.
pub enum Index {
    Trigram(trigram::TrigramIndex),
}

impl Index {
    #[must_use]
    pub fn root(&self) -> &Path {
        match self {
            Self::Trigram(idx) => idx.root(),
        }
    }

    #[must_use]
    pub const fn corpus_kind(&self) -> CorpusKind {
        match self {
            Self::Trigram(idx) => idx.corpus_kind(),
        }
    }

    #[must_use]
    pub fn candidates(&self, query: &crate::query::QuerySpec<'_>) -> Vec<crate::Candidate> {
        match self {
            Self::Trigram(idx) => idx.candidates(query),
        }
    }

    #[must_use]
    pub fn all_files(&self) -> Vec<crate::Candidate> {
        match self {
            Self::Trigram(idx) => idx.all_files(),
        }
    }
}

/// Registry of opened indexes read from a snapshot store.
pub struct Indexes {
    inner: Vec<Index>,
    root: PathBuf,
}

impl Indexes {
    /// Create an Indexes registry from a single index and its root.
    ///
    /// Useful for testing and benchmarking.
    #[must_use]
    pub fn from_single(index: Index, root: PathBuf) -> Self {
        Self {
            inner: vec![index],
            root,
        }
    }

    /// Open all indexes found under `sift_dir`.
    ///
    /// # Errors
    ///
    /// Returns [`IndexError::InvalidManifest`] if a snapshot manifest is
    /// malformed, or [`IndexError::Trigram`] if a trigram index is malformed.
    ///
    /// Returns an empty registry if no current snapshot exists (walk fallback).
    pub fn open(sift_dir: &Path) -> Result<Self, IndexError> {
        let store = store::IndexStore::open(sift_dir).map_err(|e| match e {
            crate::Error::Index(ie) => ie,
            crate::Error::Io(io) => IndexError::Io {
                path: sift_dir.to_path_buf(),
                source: io,
            },
            _ => IndexError::Io {
                path: sift_dir.to_path_buf(),
                source: std::io::Error::other(e.to_string()),
            },
        })?;

        let inner = store.open_current().map_err(|e| match e {
            crate::Error::Index(ie) => ie,
            crate::Error::Io(io) => IndexError::Io {
                path: sift_dir.to_path_buf(),
                source: io,
            },
            _ => IndexError::Io {
                path: sift_dir.to_path_buf(),
                source: std::io::Error::other(e.to_string()),
            },
        })?;

        let root = meta::StoreMeta::read(sift_dir)
            .ok()
            .map(|m| m.root)
            .unwrap_or_default();

        Ok(Self { inner, root })
    }

    #[must_use]
    pub const fn is_empty(&self) -> bool {
        self.inner.is_empty()
    }

    #[must_use]
    pub fn root(&self) -> &Path {
        &self.root
    }

    /// Resolve candidates for a query across all registered indexes.
    ///
    /// Conservative sets from each [`SearchIndex`] are intersected using
    /// `Candidate::rel_path` equality (no hashing), so distinct paths are never
    /// merged by accident.
    #[must_use]
    pub fn resolve_candidates(&self, query: &crate::query::QuerySpec<'_>) -> Vec<crate::Candidate> {
        let mut iter = self.inner.iter();
        let Some(first) = iter.next() else {
            return Vec::new();
        };

        let mut candidates = first.candidates(query);

        for index in iter {
            let next: HashSet<PathBuf> = index
                .candidates(query)
                .into_iter()
                .map(|c| c.rel_path().to_path_buf())
                .collect();
            candidates.retain(|c| next.contains(c.rel_path()));
            if candidates.is_empty() {
                break;
            }
        }

        candidates
    }

    /// Resolve candidates for a query, selecting narrowed or complete coverage.
    #[must_use]
    pub fn candidates(
        &self,
        query: &crate::query::QuerySpec<'_>,
        coverage: CandidateCoverage,
    ) -> Vec<crate::Candidate> {
        match coverage {
            CandidateCoverage::Narrowed => self.resolve_candidates(query),
            CandidateCoverage::Complete => self.resolve_all_files(),
        }
    }

    /// Return all indexed files across all registered indexes.
    #[must_use]
    pub fn resolve_all_files(&self) -> Vec<crate::Candidate> {
        let mut iter = self.inner.iter();
        let Some(first) = iter.next() else {
            return Vec::new();
        };

        let mut files = first.all_files();

        for index in iter {
            let next: HashSet<PathBuf> = index
                .all_files()
                .into_iter()
                .map(|c| c.rel_path().to_path_buf())
                .collect();
            files.retain(|c| next.contains(c.rel_path()));
            if files.is_empty() {
                break;
            }
        }

        files
    }

    #[must_use]
    pub fn first(&self) -> Option<&Index> {
        self.inner.first()
    }

    /// Returns the corpus kind if all indexes agree, or `None` for mixed/empty.
    #[must_use]
    pub fn corpus_kind(&self) -> Option<CorpusKind> {
        let kind = self.inner.first()?.corpus_kind();
        if self.inner.iter().any(|idx| idx.corpus_kind() != kind) {
            return None;
        }
        Some(kind)
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct FileId(usize);

impl FileId {
    #[must_use]
    pub const fn new(value: usize) -> Self {
        Self(value)
    }

    #[must_use]
    pub const fn get(self) -> usize {
        self.0
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct IndexId(usize);

impl IndexId {
    #[must_use]
    pub const fn new(value: usize) -> Self {
        Self(value)
    }

    #[must_use]
    pub const fn get(self) -> usize {
        self.0
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::TempDir;

    #[test]
    fn file_id_new_and_get() {
        let id = FileId::new(42);
        assert_eq!(id.get(), 42);
    }

    #[test]
    fn index_id_new_and_get() {
        let id = IndexId::new(7);
        assert_eq!(id.get(), 7);
    }

    #[test]
    fn indexes_open_empty_when_no_current_file() {
        let tmp = TempDir::new().expect("create temp dir");
        let sift_dir = tmp.path().join(".sift");
        fs::create_dir_all(&sift_dir).expect("create sift dir");
        let indexes = Indexes::open(&sift_dir).expect("open indexes");
        assert!(indexes.is_empty());
        assert!(indexes.root().as_os_str().is_empty());
    }

    #[test]
    fn indexes_first_returns_none_when_empty() {
        let tmp = TempDir::new().expect("create temp dir");
        let sift_dir = tmp.path().join(".sift");
        fs::create_dir_all(&sift_dir).expect("create sift dir");
        let indexes = Indexes::open(&sift_dir).expect("open indexes");
        assert!(indexes.first().is_none());
    }
}