Skip to main content

patina/layer/
mod.rs

1use anyhow::{Context, Result};
2use std::fs;
3use std::path::{Path, PathBuf};
4
5pub struct Layer {
6    root_path: PathBuf,
7}
8
9impl Layer {
10    pub fn new(root_path: impl AsRef<Path>) -> Self {
11        Self {
12            root_path: root_path.as_ref().to_path_buf(),
13        }
14    }
15
16    /// Initialize layer directory structure if it doesn't exist
17    pub fn init(&self) -> Result<()> {
18        let dirs = [
19            self.root_path.join("core"),
20            self.root_path.join("surface"),
21            self.root_path.join("dust"),
22            self.root_path.join("sessions"),
23        ];
24
25        for dir in &dirs {
26            fs::create_dir_all(dir)
27                .with_context(|| format!("Failed to create directory: {}", dir.display()))?;
28        }
29
30        Ok(())
31    }
32
33    /// Get the sessions directory path
34    pub fn sessions_path(&self) -> PathBuf {
35        self.root_path.join("sessions")
36    }
37
38    /// Get the core directory path
39    pub fn core_path(&self) -> PathBuf {
40        self.root_path.join("core")
41    }
42
43    /// Get the surface directory path
44    pub fn surface_path(&self) -> PathBuf {
45        self.root_path.join("surface")
46    }
47
48    /// Get the dust directory path
49    pub fn dust_path(&self) -> PathBuf {
50        self.root_path.join("dust")
51    }
52}
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57    use tempfile::TempDir;
58
59    #[test]
60    fn test_layer_new() {
61        let temp_dir = TempDir::new().unwrap();
62        let layer = Layer::new(&temp_dir);
63        assert_eq!(layer.root_path, temp_dir.path());
64    }
65
66    #[test]
67    fn test_layer_init_creates_directories() {
68        let temp_dir = TempDir::new().unwrap();
69        let layer = Layer::new(&temp_dir);
70
71        layer.init().unwrap();
72
73        assert!(temp_dir.path().join("core").exists());
74        assert!(temp_dir.path().join("surface").exists());
75        assert!(temp_dir.path().join("dust").exists());
76        assert!(temp_dir.path().join("sessions").exists());
77    }
78
79    #[test]
80    fn test_path_accessors() {
81        let temp_dir = TempDir::new().unwrap();
82        let layer = Layer::new(&temp_dir);
83
84        assert_eq!(layer.core_path(), temp_dir.path().join("core"));
85        assert_eq!(layer.surface_path(), temp_dir.path().join("surface"));
86        assert_eq!(layer.dust_path(), temp_dir.path().join("dust"));
87        assert_eq!(layer.sessions_path(), temp_dir.path().join("sessions"));
88    }
89}