Skip to main content

elph_core/memz/
paths.rs

1use std::path::{Path, PathBuf};
2
3use super::types::MemzConfig;
4use super::{EmbedFn, MemoryStore};
5
6/// Default data directory name for a standalone memz store.
7pub const DEFAULT_DATA_DIR: &str = ".memz";
8
9/// Database file name inside the data directory.
10pub const DB_FILE_NAME: &str = "memory.db";
11
12/// Environment variable for the memz data directory.
13pub const ENV_DATA_DIR: &str = "MEMZ_DIR";
14
15/// Resolved filesystem paths for a memz store. Agnostic — no MCP/CLI/hook side effects.
16#[derive(Debug, Clone, PartialEq, Eq)]
17pub struct MemzPaths {
18    pub data_dir: PathBuf,
19}
20
21impl MemzPaths {
22    pub fn new(data_dir: impl AsRef<Path>) -> Self {
23        Self {
24            data_dir: data_dir.as_ref().to_path_buf(),
25        }
26    }
27
28    /// Project-local default: `./.memz`
29    pub fn project_local() -> Self {
30        Self::new(DEFAULT_DATA_DIR)
31    }
32
33    /// Resolve from `MEMZ_DIR`, else `.memz`.
34    pub fn from_env() -> Self {
35        let dir = std::env::var(ENV_DATA_DIR).unwrap_or_else(|_| DEFAULT_DATA_DIR.to_string());
36        Self::new(dir)
37    }
38
39    pub fn db_path(&self) -> PathBuf {
40        self.data_dir.join(DB_FILE_NAME)
41    }
42
43    pub fn db_path_string(&self) -> String {
44        self.db_path().to_string_lossy().into_owned()
45    }
46
47    pub fn exists(&self) -> bool {
48        self.db_path().is_file()
49    }
50
51    /// Build a [`MemzConfig`] for this location.
52    pub fn config(&self, session_id: impl Into<String>) -> MemzConfig {
53        MemzConfig::new(self.db_path_string(), session_id)
54    }
55
56    /// Open a [`MemoryStore`] at this location with the given embedder.
57    pub fn open(&self, session_id: impl Into<String>, embed: EmbedFn) -> MemoryStore {
58        MemoryStore::new(self.config(session_id), embed)
59    }
60}