1use std::path::{Path, PathBuf};
2
3use super::types::MemzConfig;
4use super::{EmbedFn, MemoryStore};
5
6pub const DEFAULT_DATA_DIR: &str = ".memz";
8
9pub const DB_FILE_NAME: &str = "memory.db";
11
12pub const ENV_DATA_DIR: &str = "MEMZ_DIR";
14
15#[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 pub fn project_local() -> Self {
30 Self::new(DEFAULT_DATA_DIR)
31 }
32
33 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 pub fn config(&self, session_id: impl Into<String>) -> MemzConfig {
53 MemzConfig::new(self.db_path_string(), session_id)
54 }
55
56 pub fn open(&self, session_id: impl Into<String>, embed: EmbedFn) -> MemoryStore {
58 MemoryStore::new(self.config(session_id), embed)
59 }
60}