Skip to main content

thin_fetch/
sorted_pack.rs

1use std::collections::HashMap;
2
3use crate::object::{Blob, Commit, Object, Tree};
4use crate::utils::as_hex;
5
6pub struct SortedPack<'a> {
7    pub objects: HashMap<String, &'a Object>,
8    pub commits: HashMap<String, &'a Commit>,
9    pub trees: HashMap<String, &'a Tree>,
10    pub blobs: HashMap<String, &'a Blob>,
11}
12
13impl<'a> SortedPack<'a> {
14    #[must_use]
15    pub fn new() -> Self {
16        Self {
17            objects: HashMap::new(),
18            commits: HashMap::new(),
19            trees: HashMap::new(),
20            blobs: HashMap::new(),
21        }
22    }
23
24    pub fn append(&mut self, pack: &'a [Object]) {
25        for obj in pack {
26            let hash = as_hex(obj.hash().as_ref());
27
28            self.objects.insert(hash.clone(), obj);
29
30            match obj {
31                Object::Commit(commit) => {
32                    self.commits.insert(hash, commit);
33                }
34                Object::Tree(tree) => {
35                    self.trees.insert(hash, tree);
36                }
37                Object::Blob(blob) => {
38                    self.blobs.insert(hash, blob);
39                }
40                Object::Tag(_tag) => {}
41            }
42        }
43    }
44}
45
46impl Default for SortedPack<'_> {
47    fn default() -> Self {
48        Self::new()
49    }
50}