Skip to main content

pacsea/index/
persist.rs

1//! Official package index persistence through arch-toolkit.
2
3use std::path::Path;
4
5use super::idx;
6
7/// What: Load the official index from disk through arch-toolkit.
8///
9/// Inputs:
10/// - `path`: Existing Pacsea index JSON path.
11///
12/// Output:
13/// - Replaces the process-wide index when loading and conversion succeed.
14///
15/// Details:
16/// - Invalid or corrupt cache data is ignored so startup remains resilient; name lookup is rebuilt.
17pub 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
30/// What: Persist the current official index through arch-toolkit.
31///
32/// Inputs:
33/// - `path`: Destination JSON path.
34///
35/// Output:
36/// - Writes a Pacsea-compatible index snapshot; failures are logged and remain nonfatal.
37///
38/// Details:
39/// - arch-toolkit handles parent creation and serialization while Pacsea retains process-wide state.
40pub 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    /// What: Verify Pacsea index snapshots persist through the toolkit boundary.
62    ///
63    /// Inputs:
64    /// - One in-memory package and a temporary destination.
65    ///
66    /// Output:
67    /// - Reloaded process state contains the package and rebuilt lookup.
68    ///
69    /// Details:
70    /// - Uses a deterministic local file and removes it afterward.
71    #[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}