use std::collections::HashMap;
use crate::object::{Blob, Commit, Object, Tree};
use crate::utils::as_hex;
pub struct SortedPack<'a> {
pub objects: HashMap<String, &'a Object>,
pub commits: HashMap<String, &'a Commit>,
pub trees: HashMap<String, &'a Tree>,
pub blobs: HashMap<String, &'a Blob>,
}
impl<'a> SortedPack<'a> {
#[must_use]
pub fn new() -> Self {
Self {
objects: HashMap::new(),
commits: HashMap::new(),
trees: HashMap::new(),
blobs: HashMap::new(),
}
}
pub fn append(&mut self, pack: &'a [Object]) {
for obj in pack {
let hash = as_hex(obj.hash().as_ref());
self.objects.insert(hash.clone(), obj);
match obj {
Object::Commit(commit) => {
self.commits.insert(hash, commit);
}
Object::Tree(tree) => {
self.trees.insert(hash, tree);
}
Object::Blob(blob) => {
self.blobs.insert(hash, blob);
}
Object::Tag(_tag) => {}
}
}
}
}
impl Default for SortedPack<'_> {
fn default() -> Self {
Self::new()
}
}