Skip to main content

mecha10_core/state/
filesystem.rs

1//! Filesystem-based `StateManager` backend.
2
3use crate::error::{Mecha10Error, Result};
4use std::path::{Path, PathBuf};
5use tokio::fs;
6
7#[derive(Clone)]
8pub struct FilesystemStateManager {
9    pub(super) base_path: PathBuf,
10}
11
12impl FilesystemStateManager {
13    /// Create new filesystem state manager
14    pub async fn new<P: AsRef<Path>>(base_path: P) -> Result<Self> {
15        let base_path = base_path.as_ref().to_path_buf();
16
17        fs::create_dir_all(&base_path)
18            .await
19            .map_err(|e| Mecha10Error::IoError {
20                message: format!("Failed to create state directory: {}", e),
21                suggestion: "Check directory permissions".to_string(),
22            })?;
23
24        Ok(Self { base_path })
25    }
26
27    pub(super) fn state_path(&self, node_id: &str) -> PathBuf {
28        self.base_path.join(format!("{}.state.json", node_id))
29    }
30
31    /// Shared `snapshots/` subdirectory behind `snapshot_path`/`snapshot_meta_path`.
32    fn snapshots_dir(&self) -> PathBuf {
33        self.base_path.join("snapshots")
34    }
35
36    pub(super) fn snapshot_path(&self, snapshot_id: &str) -> PathBuf {
37        self.snapshots_dir().join(format!("{}.snapshot.json", snapshot_id))
38    }
39
40    pub(super) fn snapshot_meta_path(&self, snapshot_id: &str) -> PathBuf {
41        self.snapshots_dir().join(format!("{}.meta.json", snapshot_id))
42    }
43}