Skip to main content

elph_core/floppy/
paths.rs

1use std::path::{Path, PathBuf};
2
3use super::types::FloppyConfig;
4use super::{EmbedFn, MemoryStore};
5
6/// Default data directory name for a standalone floppy store.
7pub const DEFAULT_DATA_DIR: &str = ".floppy";
8
9/// Database file name inside the data directory.
10pub const DB_FILE_NAME: &str = "memory.db";
11
12/// Resolved filesystem paths for a floppy store.
13#[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    /// Project-local default: `./.floppy`
26    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    /// Build a [`FloppyConfig`] for this location.
47    pub fn config(&self, session_id: impl Into<String>) -> FloppyConfig {
48        FloppyConfig::new(self.db_path_string(), session_id)
49    }
50
51    /// Open a [`MemoryStore`] at this location with the given embedder.
52    pub fn open(&self, session_id: impl Into<String>, embed: EmbedFn) -> MemoryStore {
53        MemoryStore::new(self.config(session_id), embed)
54    }
55}