Skip to main content

greplm_core/
paths.rs

1use std::path::{Path, PathBuf};
2
3/// The name of the directory where greplm stores its index and cache.
4pub const DIR_NAME: &str = ".greplm";
5
6/// Filesystem layout for a single indexed project.
7#[derive(Debug, Clone)]
8pub struct Paths {
9    /// Project root (the directory being indexed).
10    pub root: PathBuf,
11    /// The `.greplm` directory.
12    pub base: PathBuf,
13}
14
15impl Paths {
16    pub fn new(root: impl AsRef<Path>) -> Self {
17        let root = root.as_ref().to_path_buf();
18        let base = root.join(DIR_NAME);
19        Self { root, base }
20    }
21
22    /// Directory holding immutable index segments.
23    pub fn segments_dir(&self) -> PathBuf {
24        self.base.join("segments")
25    }
26
27    pub fn config_file(&self) -> PathBuf {
28        self.base.join("config.toml")
29    }
30
31    pub fn meta_file(&self) -> PathBuf {
32        self.base.join("meta.json")
33    }
34
35    pub fn cache_file(&self) -> PathBuf {
36        self.base.join("cache.redb")
37    }
38
39    /// Append-only log of per-query token-savings records.
40    pub fn savings_file(&self) -> PathBuf {
41        self.base.join("savings.jsonl")
42    }
43
44    pub fn gitignore_file(&self) -> PathBuf {
45        self.base.join(".gitignore")
46    }
47
48    pub fn fst_file(&self, seg: u64) -> PathBuf {
49        self.segments_dir().join(format!("seg-{seg:06}.fst"))
50    }
51
52    pub fn post_file(&self, seg: u64) -> PathBuf {
53        self.segments_dir().join(format!("seg-{seg:06}.post"))
54    }
55
56    pub fn docs_file(&self, seg: u64) -> PathBuf {
57        self.segments_dir().join(format!("seg-{seg:06}.docs"))
58    }
59
60    pub fn syms_file(&self, seg: u64) -> PathBuf {
61        self.segments_dir().join(format!("seg-{seg:06}.syms"))
62    }
63
64    pub fn refs_file(&self, seg: u64) -> PathBuf {
65        self.segments_dir().join(format!("seg-{seg:06}.refs"))
66    }
67
68    pub fn live_file(&self, seg: u64) -> PathBuf {
69        self.segments_dir().join(format!("seg-{seg:06}.live"))
70    }
71
72    /// True if an index directory exists.
73    pub fn exists(&self) -> bool {
74        self.base.is_dir()
75    }
76}