mecha10-core 0.6.2

Mecha10 core runtime and foundational types
Documentation
//! Filesystem-based `StateManager` backend.

use crate::error::{Mecha10Error, Result};
use std::path::{Path, PathBuf};
use tokio::fs;

/// Filesystem-based state storage
#[derive(Clone)]
pub struct FilesystemStateManager {
    pub(super) base_path: PathBuf,
}

impl FilesystemStateManager {
    /// Create new filesystem state manager
    pub async fn new<P: AsRef<Path>>(base_path: P) -> Result<Self> {
        let base_path = base_path.as_ref().to_path_buf();

        // Create directory if it doesn't exist
        fs::create_dir_all(&base_path)
            .await
            .map_err(|e| Mecha10Error::IoError {
                message: format!("Failed to create state directory: {}", e),
                suggestion: "Check directory permissions".to_string(),
            })?;

        Ok(Self { base_path })
    }

    pub(super) fn state_path(&self, node_id: &str) -> PathBuf {
        self.base_path.join(format!("{}.state.json", node_id))
    }

    pub(super) fn snapshot_path(&self, snapshot_id: &str) -> PathBuf {
        self.base_path
            .join("snapshots")
            .join(format!("{}.snapshot.json", snapshot_id))
    }

    pub(super) fn snapshot_meta_path(&self, snapshot_id: &str) -> PathBuf {
        self.base_path
            .join("snapshots")
            .join(format!("{}.meta.json", snapshot_id))
    }
}