1use autoeq_core::AutoeqError;
4use std::collections::HashMap;
5use std::path::{Path, PathBuf};
6use std::sync::{Arc, Mutex};
7
8pub mod roomeq;
10
11pub trait ArtifactStore: Send + Sync {
14 fn create_dir_all(&self, path: &Path) -> Result<(), AutoeqError>;
16
17 fn write(&self, path: &Path, contents: &[u8]) -> Result<(), AutoeqError>;
19
20 fn read(&self, path: &Path) -> Result<Option<Vec<u8>>, AutoeqError>;
22}
23
24#[derive(Debug, Default, Clone, Copy)]
26pub struct FsArtifactStore;
27
28impl FsArtifactStore {
29 pub fn new() -> Self {
31 Self
32 }
33}
34
35impl ArtifactStore for FsArtifactStore {
36 fn create_dir_all(&self, path: &Path) -> Result<(), AutoeqError> {
37 std::fs::create_dir_all(path).map_err(|e| AutoeqError::DirectoryCreation {
38 path: path.display().to_string(),
39 message: e.to_string(),
40 })
41 }
42
43 fn write(&self, path: &Path, contents: &[u8]) -> Result<(), AutoeqError> {
44 std::fs::write(path, contents).map_err(|e| AutoeqError::FileOperation {
45 path: path.display().to_string(),
46 message: e.to_string(),
47 })
48 }
49
50 fn read(&self, path: &Path) -> Result<Option<Vec<u8>>, AutoeqError> {
51 match std::fs::read(path) {
52 Ok(v) => Ok(Some(v)),
53 Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
54 Err(e) => Err(AutoeqError::FileOperation {
55 path: path.display().to_string(),
56 message: e.to_string(),
57 }),
58 }
59 }
60}
61
62#[derive(Debug, Default, Clone)]
64pub struct MemoryArtifactStore {
65 files: Arc<Mutex<HashMap<PathBuf, Vec<u8>>>>,
66 dirs: Arc<Mutex<Vec<PathBuf>>>,
67}
68
69impl MemoryArtifactStore {
70 pub fn new() -> Self {
72 Self::default()
73 }
74
75 pub fn get(&self, path: &Path) -> Option<Vec<u8>> {
77 self.files.lock().unwrap().get(path).cloned()
78 }
79
80 pub fn is_dir(&self, path: &Path) -> bool {
82 self.dirs.lock().unwrap().contains(&path.to_path_buf())
83 }
84
85 pub fn file_count(&self) -> usize {
87 self.files.lock().unwrap().len()
88 }
89}
90
91impl ArtifactStore for MemoryArtifactStore {
92 fn create_dir_all(&self, path: &Path) -> Result<(), AutoeqError> {
93 let mut dirs = self.dirs.lock().unwrap();
94 let mut current = Some(path);
95 while let Some(p) = current {
96 let owned = p.to_path_buf();
97 if !dirs.contains(&owned) {
98 dirs.push(owned);
99 }
100 current = p.parent();
101 }
102 Ok(())
103 }
104
105 fn write(&self, path: &Path, contents: &[u8]) -> Result<(), AutoeqError> {
106 self.files
107 .lock()
108 .unwrap()
109 .insert(path.to_path_buf(), contents.to_vec());
110 Ok(())
111 }
112
113 fn read(&self, path: &Path) -> Result<Option<Vec<u8>>, AutoeqError> {
114 Ok(self.files.lock().unwrap().get(path).cloned())
115 }
116}
117
118#[cfg(test)]
119mod tests {
120 use super::{ArtifactStore, FsArtifactStore, MemoryArtifactStore};
121 use std::path::Path;
122
123 #[test]
124 fn memory_store_round_trip() {
125 let store = MemoryArtifactStore::new();
126 store.create_dir_all(Path::new("a/b")).unwrap();
127 store.write(Path::new("a/b/c.txt"), b"hello").unwrap();
128 assert!(store.is_dir(Path::new("a/b")));
129 assert_eq!(store.get(Path::new("a/b/c.txt")).unwrap(), b"hello");
130 }
131
132 #[test]
133 fn memory_store_missing_read_returns_none() {
134 let store = MemoryArtifactStore::new();
135 assert!(store.read(Path::new("missing.txt")).unwrap().is_none());
136 }
137
138 #[test]
139 fn fs_store_round_trip() {
140 let tmp = tempfile::TempDir::new().unwrap();
141 let store = FsArtifactStore::new();
142 let path = tmp.path().join("sub/dir/file.txt");
143 store.create_dir_all(path.parent().unwrap()).unwrap();
144 store.write(&path, b"world").unwrap();
145 assert_eq!(store.read(&path).unwrap().unwrap(), b"world");
146 }
147}