Skip to main content

ralph_workflow/agents/cache_environment/
io.rs

1// agents/cache_environment/io.rs — boundary module for cache environment operations.
2// File stem is `io` — recognized as boundary module by forbid_io_effects lint.
3
4use std::path::{Path, PathBuf};
5
6pub trait CacheEnvironment: Send + Sync {
7    fn cache_dir(&self) -> Option<PathBuf>;
8
9    fn read_file(&self, path: &Path) -> Result<String, std::io::Error>;
10
11    fn write_file(&self, path: &Path, content: &str) -> Result<(), std::io::Error>;
12
13    fn create_dir_all(&self, path: &Path) -> Result<(), std::io::Error>;
14}
15
16#[derive(Debug, Default, Clone, Copy)]
17pub struct RealCacheEnvironment;
18
19impl CacheEnvironment for RealCacheEnvironment {
20    fn cache_dir(&self) -> Option<PathBuf> {
21        dirs::cache_dir().map(|d| d.join("ralph-workflow"))
22    }
23
24    fn read_file(&self, path: &Path) -> Result<String, std::io::Error> {
25        std::fs::read_to_string(path)
26    }
27
28    fn write_file(&self, path: &Path, content: &str) -> Result<(), std::io::Error> {
29        std::fs::write(path, content)
30    }
31
32    fn create_dir_all(&self, path: &Path) -> Result<(), std::io::Error> {
33        std::fs::create_dir_all(path)
34    }
35}