elph_core/floppy/
paths.rs1use std::path::{Path, PathBuf};
2
3use super::types::FloppyConfig;
4use super::{EmbedFn, MemoryStore};
5
6pub const DEFAULT_DATA_DIR: &str = ".floppy";
8
9pub const DB_FILE_NAME: &str = "memory.db";
11
12#[derive(Debug, Clone, PartialEq, Eq)]
14pub struct FloppyPaths {
15 pub data_dir: PathBuf,
16}
17
18impl FloppyPaths {
19 pub fn new(data_dir: impl AsRef<Path>) -> Self {
20 Self {
21 data_dir: data_dir.as_ref().to_path_buf(),
22 }
23 }
24
25 pub fn project_local() -> Self {
27 Self::new(DEFAULT_DATA_DIR)
28 }
29
30 pub fn data_dir(&self) -> &Path {
31 &self.data_dir
32 }
33
34 pub fn db_path(&self) -> PathBuf {
35 self.data_dir.join(DB_FILE_NAME)
36 }
37
38 pub fn db_path_string(&self) -> String {
39 self.db_path().to_string_lossy().into_owned()
40 }
41
42 pub fn exists(&self) -> bool {
43 self.db_path().is_file()
44 }
45
46 pub fn config(&self, session_id: impl Into<String>) -> FloppyConfig {
48 FloppyConfig::new(self.db_path_string(), session_id)
49 }
50
51 pub fn open(&self, session_id: impl Into<String>, embed: EmbedFn) -> MemoryStore {
53 MemoryStore::new(self.config(session_id), embed)
54 }
55}