1use std::fs;
6use std::io::Write;
7use std::path::{Path, PathBuf};
8
9use thiserror::Error;
10
11use crate::session::Session;
12use crate::source::ReviewSource;
13
14const DIR: &str = ".diffler";
15const REVIEWS: &str = "reviews";
16const LEGACY_FILE: &str = "session.json";
17
18#[derive(Debug, Error)]
19pub enum StoreError {
20 #[error(transparent)]
21 Io(#[from] std::io::Error),
22 #[error("corrupt session file {0}: {1}")]
23 Corrupt(PathBuf, serde_json::Error),
24}
25
26#[derive(Debug, serde::Serialize, serde::Deserialize)]
27struct OnDisk {
28 version: u32,
29 #[serde(default, skip_serializing_if = "Option::is_none")]
33 source: Option<ReviewSource>,
34 #[serde(flatten)]
35 session: Session,
36}
37
38fn reviews_dir(repo_root: &Path) -> PathBuf {
39 repo_root.join(DIR).join(REVIEWS)
40}
41
42fn source_path(repo_root: &Path, source: &ReviewSource) -> PathBuf {
43 reviews_dir(repo_root).join(format!("{}.json", source.key()))
44}
45
46fn legacy_path(repo_root: &Path) -> PathBuf {
47 repo_root.join(DIR).join(LEGACY_FILE)
48}
49
50fn read_session(path: &Path) -> Result<Option<Session>, StoreError> {
51 match fs::read_to_string(path) {
52 Ok(raw) => {
53 let on_disk: OnDisk = serde_json::from_str(&raw)
54 .map_err(|e| StoreError::Corrupt(path.to_path_buf(), e))?;
55 Ok(Some(on_disk.session))
56 }
57 Err(err) if err.kind() == std::io::ErrorKind::NotFound => Ok(None),
58 Err(err) => Err(err.into()),
59 }
60}
61
62pub fn load_source(repo_root: &Path, source: &ReviewSource) -> Result<Session, StoreError> {
66 if let Some(session) = read_session(&source_path(repo_root, source))? {
67 return Ok(session);
68 }
69 if matches!(source, ReviewSource::WorkingTree)
70 && let Some(session) = read_session(&legacy_path(repo_root))?
71 {
72 return Ok(session);
73 }
74 Ok(Session::default())
75}
76
77pub fn save_source(
80 repo_root: &Path,
81 source: &ReviewSource,
82 session: &Session,
83) -> Result<(), StoreError> {
84 let dir = reviews_dir(repo_root);
85 fs::create_dir_all(&dir)?;
86 let gitignore = repo_root.join(DIR).join(".gitignore");
87 if !gitignore.exists() {
88 fs::write(&gitignore, "*\n")?;
89 }
90 let on_disk = OnDisk {
91 version: 1,
92 source: Some(source.clone()),
93 session: session.clone(),
94 };
95 let json = serde_json::to_string_pretty(&on_disk).map_err(std::io::Error::other)?;
96 let mut tmp = tempfile::NamedTempFile::new_in(&dir)?;
97 tmp.write_all(json.as_bytes())?;
98 tmp.persist(source_path(repo_root, source))
99 .map_err(|e| StoreError::Io(e.error))?;
100 if matches!(source, ReviewSource::WorkingTree) {
101 let legacy = legacy_path(repo_root);
102 if legacy.exists() {
103 fs::remove_file(legacy)?;
104 }
105 }
106 Ok(())
107}
108
109pub fn load_all(repo_root: &Path) -> Result<Vec<(ReviewSource, Session)>, StoreError> {
113 let mut reviews: Vec<(ReviewSource, Session)> = Vec::new();
114 let dir = reviews_dir(repo_root);
115 match fs::read_dir(&dir) {
116 Ok(entries) => {
117 for entry in entries {
118 let path = entry?.path();
119 if path.extension().is_none_or(|ext| ext != "json") {
120 continue;
121 }
122 let raw = fs::read_to_string(&path)?;
123 let on_disk: OnDisk =
124 serde_json::from_str(&raw).map_err(|e| StoreError::Corrupt(path.clone(), e))?;
125 let source = on_disk.source.unwrap_or(ReviewSource::WorkingTree);
126 reviews.push((source, on_disk.session));
127 }
128 }
129 Err(err) if err.kind() == std::io::ErrorKind::NotFound => {}
130 Err(err) => return Err(err.into()),
131 }
132 if !reviews
133 .iter()
134 .any(|(s, _)| matches!(s, ReviewSource::WorkingTree))
135 && let Some(session) = read_session(&legacy_path(repo_root))?
136 {
137 reviews.push((ReviewSource::WorkingTree, session));
138 }
139 reviews.sort_by_key(|(source, _)| source.key());
140 Ok(reviews)
141}
142
143pub fn load(repo_root: &Path) -> Result<Session, StoreError> {
145 load_source(repo_root, &ReviewSource::WorkingTree)
146}
147
148pub fn save(repo_root: &Path, session: &Session) -> Result<(), StoreError> {
150 save_source(repo_root, &ReviewSource::WorkingTree, session)
151}
152
153#[cfg(test)]
154mod tests {
155 use crate::test_support::anchor;
156
157 use super::*;
158
159 #[test]
160 fn missing_file_loads_default() {
161 let dir = tempfile::tempdir().expect("tempdir");
162 let s = load(dir.path()).expect("load");
163 assert_eq!(s, Session::default());
164 }
165
166 #[test]
167 fn save_load_round_trip() {
168 let dir = tempfile::tempdir().expect("tempdir");
169 let mut s = Session::default();
170 s.add_comment(anchor("a.txt", Some(1)), "reviewer", "hm");
171 s.mark_viewed("a.txt", "hash-1");
172 save(dir.path(), &s).expect("save");
173 let back = load(dir.path()).expect("load");
174 assert_eq!(s, back);
175 }
176
177 #[test]
178 fn save_writes_gitignore() {
179 let dir = tempfile::tempdir().expect("tempdir");
180 save(dir.path(), &Session::default()).expect("save");
181 let gi = std::fs::read_to_string(dir.path().join(".diffler/.gitignore")).expect("read");
182 assert_eq!(gi, "*\n");
183 }
184
185 #[test]
186 fn corrupt_file_is_an_error_not_a_reset() {
187 let dir = tempfile::tempdir().expect("tempdir");
188 std::fs::create_dir_all(dir.path().join(".diffler/reviews")).expect("mkdir");
189 std::fs::write(
190 dir.path().join(".diffler/reviews/working.json"),
191 "{not json",
192 )
193 .expect("write");
194 assert!(matches!(load(dir.path()), Err(StoreError::Corrupt(..))));
195 }
196
197 #[test]
198 fn sources_persist_independently() {
199 let dir = tempfile::tempdir().expect("tempdir");
200 let mut work = Session::default();
201 work.mark_viewed("a.txt", "h-work");
202 let mut commit = Session::default();
203 commit.mark_viewed("a.txt", "h-commit");
204
205 save_source(dir.path(), &ReviewSource::WorkingTree, &work).expect("save work");
206 save_source(dir.path(), &ReviewSource::commit("abc"), &commit).expect("save commit");
207
208 assert_eq!(load(dir.path()).expect("load work"), work);
209 assert_eq!(
210 load_source(dir.path(), &ReviewSource::commit("abc")).expect("load commit"),
211 commit
212 );
213 assert!(
215 load_source(dir.path(), &ReviewSource::commit("abc"))
216 .expect("load")
217 .is_viewed("a.txt", "h-commit")
218 );
219 assert!(
220 !load(dir.path())
221 .expect("load")
222 .is_viewed("a.txt", "h-commit")
223 );
224 }
225
226 #[test]
227 fn legacy_file_migrates_to_working_on_save() {
228 let dir = tempfile::tempdir().expect("tempdir");
229 std::fs::create_dir_all(dir.path().join(".diffler")).expect("mkdir");
230 let legacy = dir.path().join(".diffler/session.json");
231 std::fs::write(
232 &legacy,
233 r#"{"version":1,"comments":[],"viewed":{"a.txt":"h-legacy"}}"#,
234 )
235 .expect("write legacy");
236
237 let loaded = load(dir.path()).expect("load");
239 assert!(loaded.is_viewed("a.txt", "h-legacy"));
240
241 save(dir.path(), &loaded).expect("save");
243 assert!(!legacy.exists(), "legacy file removed after migration");
244 assert!(dir.path().join(".diffler/reviews/working.json").exists());
245 assert_eq!(load(dir.path()).expect("reload"), loaded);
246 }
247
248 #[test]
249 fn load_all_returns_every_source_sorted_by_key() {
250 let dir = tempfile::tempdir().expect("tempdir");
251 save_source(dir.path(), &ReviewSource::WorkingTree, &Session::default()).expect("w");
252 save_source(
253 dir.path(),
254 &ReviewSource::commit("bbb"),
255 &Session::default(),
256 )
257 .expect("c");
258 save_source(
259 dir.path(),
260 &ReviewSource::commit("aaa"),
261 &Session::default(),
262 )
263 .expect("c");
264
265 let all = load_all(dir.path()).expect("load_all");
266 let keys: Vec<String> = all.iter().map(|(s, _)| s.key()).collect();
267 assert_eq!(keys, ["commit-aaa", "commit-bbb", "working"]);
268 }
269
270 #[test]
271 fn load_all_includes_legacy_working_before_migration() {
272 let dir = tempfile::tempdir().expect("tempdir");
273 std::fs::create_dir_all(dir.path().join(".diffler")).expect("mkdir");
274 std::fs::write(
275 dir.path().join(".diffler/session.json"),
276 r#"{"version":1,"comments":[],"viewed":{"a.txt":"h"}}"#,
277 )
278 .expect("write legacy");
279 save_source(
280 dir.path(),
281 &ReviewSource::commit("abc"),
282 &Session::default(),
283 )
284 .expect("c");
285
286 let all = load_all(dir.path()).expect("load_all");
287 let keys: Vec<String> = all.iter().map(|(s, _)| s.key()).collect();
288 assert_eq!(keys, ["commit-abc", "working"]);
289 }
290}