1use std::path::Path;
4
5use super::idx;
6
7pub fn load_from_disk(path: &Path) {
18 match crate::integrations::arch_toolkit::index::load(path) {
19 Ok(index) => {
20 if let Ok(mut guard) = idx().write() {
21 *guard = index;
22 }
23 }
24 Err(error) => {
25 tracing::debug!(path = %path.display(), error = %error, "official index cache unavailable");
26 }
27 }
28}
29
30pub fn save_to_disk(path: &Path) {
41 let Ok(guard) = idx().read() else {
42 tracing::warn!(path = %path.display(), "official index lock is poisoned; cache not saved");
43 return;
44 };
45 if guard.pkgs.is_empty() {
46 tracing::warn!(path = %path.display(), "attempting to save empty official index");
47 }
48 if let Err(error) = crate::integrations::arch_toolkit::index::save(&guard, path) {
49 tracing::warn!(path = %path.display(), error = %error, "failed to save official index");
50 } else {
51 tracing::info!(
52 path = %path.display(),
53 package_count = guard.pkgs.len(),
54 "saved official index"
55 );
56 }
57}
58
59#[cfg(test)]
60mod tests {
61 #[test]
72 fn index_persistence_round_trip_uses_toolkit() {
73 let _guard = crate::global_test_mutex_lock();
74 if let Ok(mut index) = super::idx().write() {
75 index.pkgs = vec![crate::index::OfficialPkg {
76 name: "pacsea-fixture".to_string(),
77 repo: "extra".to_string(),
78 arch: "x86_64".to_string(),
79 version: "1".to_string(),
80 description: "fixture".to_string(),
81 }];
82 index.rebuild_name_index();
83 }
84 let path = std::env::temp_dir().join(format!(
85 "pacsea-toolkit-index-{}-{}.json",
86 std::process::id(),
87 std::time::SystemTime::now()
88 .duration_since(std::time::UNIX_EPOCH)
89 .expect("system clock should follow epoch")
90 .as_nanos()
91 ));
92 super::save_to_disk(&path);
93 if let Ok(mut index) = super::idx().write() {
94 *index = crate::index::OfficialIndex::default();
95 }
96 super::load_from_disk(&path);
97 assert!(crate::index::find_package_by_name("pacsea-fixture").is_some());
98 let _ = std::fs::remove_file(path);
99 }
100}