use std::path::{Path, PathBuf};
use super::types::MemzConfig;
use super::{EmbedFn, MemoryStore};
pub const DEFAULT_DATA_DIR: &str = ".memz";
pub const DB_FILE_NAME: &str = "memory.db";
pub const ENV_DATA_DIR: &str = "MEMZ_DIR";
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MemzPaths {
pub data_dir: PathBuf,
}
impl MemzPaths {
pub fn new(data_dir: impl AsRef<Path>) -> Self {
Self {
data_dir: data_dir.as_ref().to_path_buf(),
}
}
pub fn project_local() -> Self {
Self::new(DEFAULT_DATA_DIR)
}
pub fn from_env() -> Self {
let dir = std::env::var(ENV_DATA_DIR).unwrap_or_else(|_| DEFAULT_DATA_DIR.to_string());
Self::new(dir)
}
pub fn db_path(&self) -> PathBuf {
self.data_dir.join(DB_FILE_NAME)
}
pub fn db_path_string(&self) -> String {
self.db_path().to_string_lossy().into_owned()
}
pub fn exists(&self) -> bool {
self.db_path().is_file()
}
pub fn config(&self, session_id: impl Into<String>) -> MemzConfig {
MemzConfig::new(self.db_path_string(), session_id)
}
pub fn open(&self, session_id: impl Into<String>, embed: EmbedFn) -> MemoryStore {
MemoryStore::new(self.config(session_id), embed)
}
}