BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / call / report / storage.rs

storage.rs

storage.rs 3.5 KiB Download Raw
1use std::fs;
2use std::io;
3use std::path::{Path, PathBuf};
4
5use super::Document;
6
7const REPORT_DIRECTORY: &str = "call-reports";
8const KEEP_REPORTS: usize = 30;
9const JSON_EXTENSION: &str = "json";
10const MARKDOWN_EXTENSION: &str = "md";
11
12#[derive(Debug, Clone, PartialEq, Eq)]
13pub struct SavedReport {
14 pub json: PathBuf,
15 pub markdown: PathBuf,
16}
17
18#[cfg(test)]
19pub fn report_directory() -> PathBuf {
20 std::env::temp_dir().join("rusty-teams-test-call-reports")
21}
22
23#[cfg(not(test))]
24pub fn report_directory() -> PathBuf {
25 directories::ProjectDirs::from("", "", store::DATA_DIR_NAME)
26 .map(|directories| directories.data_local_dir().join(REPORT_DIRECTORY))
27 .unwrap_or_else(|| PathBuf::from(REPORT_DIRECTORY))
28}
29
30pub fn save_report(directory: &Path, document: &Document) -> io::Result<SavedReport> {
31 fs::create_dir_all(directory)?;
32 let stem = free_stem(directory, &document.stem);
33 let saved = SavedReport {
34 json: directory.join(format!("{stem}.{JSON_EXTENSION}")),
35 markdown: directory.join(format!("{stem}.{MARKDOWN_EXTENSION}")),
36 };
37 fs::write(&saved.json, &document.json)?;
38 fs::write(&saved.markdown, &document.markdown)?;
39 prune(directory, KEEP_REPORTS);
40 Ok(saved)
41}
42
43fn free_stem(directory: &Path, stem: &str) -> String {
44 let taken = |candidate: &str| directory.join(format!("{candidate}.{JSON_EXTENSION}")).exists();
45 if !taken(stem) {
46 return stem.to_owned();
47 }
48 (2..)
49 .map(|counter| format!("{stem}-{counter}"))
50 .find(|candidate| !taken(candidate))
51 .expect("an unbounded counter finds a free name")
52}
53
54fn prune(directory: &Path, keep: usize) {
55 let Ok(entries) = fs::read_dir(directory) else {
56 return;
57 };
58 let mut stems: Vec<String> = entries
59 .filter_map(Result::ok)
60 .map(|entry| entry.path())
61 .filter(|path| path.extension().is_some_and(|extension| extension == JSON_EXTENSION))
62 .filter_map(|path| path.file_stem().map(|stem| stem.to_string_lossy().into_owned()))
63 .collect();
64 stems.sort();
65 let surplus = stems.len().saturating_sub(keep);
66 for stem in &stems[..surplus] {
67 for extension in [JSON_EXTENSION, MARKDOWN_EXTENSION] {
68 let _ = fs::remove_file(directory.join(format!("{stem}.{extension}")));
69 }
70 }
71}
72
73#[cfg(test)]
74mod tests {
75 use super::*;
76
77 fn document(stem: &str) -> Document {
78 Document { stem: stem.to_owned(), json: "{}".to_owned(), markdown: "# x".to_owned() }
79 }
80
81 #[test]
82 fn the_newest_thirty_reports_are_kept_with_both_files() {
83 let directory = tempfile::tempdir().unwrap();
84 for index in 0..33 {
85 save_report(directory.path(), &document(&format!("20260101T0000{index:02}Z-1to1"))).unwrap();
86 }
87 let mut names: Vec<String> =
88 fs::read_dir(directory.path()).unwrap().map(|entry| entry.unwrap().file_name().to_string_lossy().into_owned()).collect();
89 names.sort();
90 assert_eq!(names.len(), 60);
91 assert_eq!(names[0], "20260101T000003Z-1to1.json");
92 assert_eq!(names[59], "20260101T000032Z-1to1.md");
93 }
94
95 #[test]
96 fn a_report_in_the_same_second_gets_a_counter() {
97 let directory = tempfile::tempdir().unwrap();
98 let first = save_report(directory.path(), &document("20260101T000000Z-echo")).unwrap();
99 let second = save_report(directory.path(), &document("20260101T000000Z-echo")).unwrap();
100 assert_ne!(first.json, second.json);
101 assert!(second.json.to_string_lossy().ends_with("20260101T000000Z-echo-2.json"));
102 }
103}