Skip to main content

pacenotes_installer\ledger/
ownership.rs

1impl super::Ledger {
2    pub fn is_empty(&self) -> bool {
3        self.document.packages.is_empty()
4    }
5
6    pub fn len(&self) -> usize {
7        self.document.packages.len()
8    }
9
10    /// By `game/directory/name`.
11    pub fn packages(&self) -> &std::collections::BTreeMap<String, super::LedgerEntry> {
12        &self.document.packages
13    }
14
15    pub fn get(&self, key: &str) -> Option<&super::LedgerEntry> {
16        self.document.packages.get(key)
17    }
18
19    /// Entries a dead run left mid-install, to deal with before planning anything.
20    pub fn unfinished(&self) -> Vec<String> {
21        self.document
22            .packages
23            .iter()
24            .filter(|(_, entry)| entry.state == super::EntryState::Installing)
25            .map(|(key, _)| key.clone())
26            .collect()
27    }
28
29    /// Compared folded, since two packages spelling a shared path differently own one file.
30    pub fn owners_of(&self, path: &str) -> Vec<&str> {
31        let key = fold(path);
32        self.document
33            .packages
34            .iter()
35            .filter(|(_, entry)| entry.files.contains_key(&key))
36            .map(|(package, _)| package.as_str())
37            .collect()
38    }
39
40    /// A file nothing owns was the user's, and is backed up before it is overwritten.
41    pub fn owns(&self, path: &str) -> bool {
42        !self.owners_of(path).is_empty()
43    }
44}
45
46/// The key a path is stored and compared under: lowercase, as the filesystem
47/// compares names, though not NTFS's exact table.
48pub fn fold(path: &str) -> String {
49    path.to_lowercase()
50}