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::session::Anchor;
156
157 use super::*;
158
159 fn anchor() -> Anchor {
160 Anchor {
161 file: "a.txt".into(),
162 line: Some(1),
163 line_end: None,
164 on_old_side: false,
165 line_text: None,
166 }
167 }
168
169 #[test]
170 fn missing_file_loads_default() {
171 let dir = tempfile::tempdir().expect("tempdir");
172 let s = load(dir.path()).expect("load");
173 assert_eq!(s, Session::default());
174 }
175
176 #[test]
177 fn save_load_round_trip() {
178 let dir = tempfile::tempdir().expect("tempdir");
179 let mut s = Session::default();
180 s.add_comment("mattf", anchor(), "hm");
181 s.mark_viewed("a.txt", "hash-1");
182 save(dir.path(), &s).expect("save");
183 let back = load(dir.path()).expect("load");
184 assert_eq!(s, back);
185 }
186
187 #[test]
188 fn save_writes_gitignore() {
189 let dir = tempfile::tempdir().expect("tempdir");
190 save(dir.path(), &Session::default()).expect("save");
191 let gi = std::fs::read_to_string(dir.path().join(".diffler/.gitignore")).expect("read");
192 assert_eq!(gi, "*\n");
193 }
194
195 #[test]
196 fn corrupt_file_is_an_error_not_a_reset() {
197 let dir = tempfile::tempdir().expect("tempdir");
198 std::fs::create_dir_all(dir.path().join(".diffler/reviews")).expect("mkdir");
199 std::fs::write(
200 dir.path().join(".diffler/reviews/working.json"),
201 "{not json",
202 )
203 .expect("write");
204 assert!(matches!(load(dir.path()), Err(StoreError::Corrupt(..))));
205 }
206
207 #[test]
208 fn sources_persist_independently() {
209 let dir = tempfile::tempdir().expect("tempdir");
210 let mut work = Session::default();
211 work.mark_viewed("a.txt", "h-work");
212 let mut commit = Session::default();
213 commit.mark_viewed("a.txt", "h-commit");
214
215 save_source(dir.path(), &ReviewSource::WorkingTree, &work).expect("save work");
216 save_source(dir.path(), &ReviewSource::commit("abc"), &commit).expect("save commit");
217
218 assert_eq!(load(dir.path()).expect("load work"), work);
219 assert_eq!(
220 load_source(dir.path(), &ReviewSource::commit("abc")).expect("load commit"),
221 commit
222 );
223 assert!(
225 load_source(dir.path(), &ReviewSource::commit("abc"))
226 .expect("load")
227 .is_viewed("a.txt", "h-commit")
228 );
229 assert!(
230 !load(dir.path())
231 .expect("load")
232 .is_viewed("a.txt", "h-commit")
233 );
234 }
235
236 #[test]
237 fn legacy_file_migrates_to_working_on_save() {
238 let dir = tempfile::tempdir().expect("tempdir");
239 std::fs::create_dir_all(dir.path().join(".diffler")).expect("mkdir");
240 let legacy = dir.path().join(".diffler/session.json");
241 std::fs::write(
242 &legacy,
243 r#"{"version":1,"comments":[],"viewed":{"a.txt":"h-legacy"}}"#,
244 )
245 .expect("write legacy");
246
247 let loaded = load(dir.path()).expect("load");
249 assert!(loaded.is_viewed("a.txt", "h-legacy"));
250
251 save(dir.path(), &loaded).expect("save");
253 assert!(!legacy.exists(), "legacy file removed after migration");
254 assert!(dir.path().join(".diffler/reviews/working.json").exists());
255 assert_eq!(load(dir.path()).expect("reload"), loaded);
256 }
257
258 #[test]
259 fn load_all_returns_every_source_sorted_by_key() {
260 let dir = tempfile::tempdir().expect("tempdir");
261 save_source(dir.path(), &ReviewSource::WorkingTree, &Session::default()).expect("w");
262 save_source(
263 dir.path(),
264 &ReviewSource::commit("bbb"),
265 &Session::default(),
266 )
267 .expect("c");
268 save_source(
269 dir.path(),
270 &ReviewSource::commit("aaa"),
271 &Session::default(),
272 )
273 .expect("c");
274
275 let all = load_all(dir.path()).expect("load_all");
276 let keys: Vec<String> = all.iter().map(|(s, _)| s.key()).collect();
277 assert_eq!(keys, ["commit-aaa", "commit-bbb", "working"]);
278 }
279
280 #[test]
281 fn load_all_includes_legacy_working_before_migration() {
282 let dir = tempfile::tempdir().expect("tempdir");
283 std::fs::create_dir_all(dir.path().join(".diffler")).expect("mkdir");
284 std::fs::write(
285 dir.path().join(".diffler/session.json"),
286 r#"{"version":1,"comments":[],"viewed":{"a.txt":"h"}}"#,
287 )
288 .expect("write legacy");
289 save_source(
290 dir.path(),
291 &ReviewSource::commit("abc"),
292 &Session::default(),
293 )
294 .expect("c");
295
296 let all = load_all(dir.path()).expect("load_all");
297 let keys: Vec<String> = all.iter().map(|(s, _)| s.key()).collect();
298 assert_eq!(keys, ["commit-abc", "working"]);
299 }
300}