git_internal/internal/pack/
entry.rs

1//! Lightweight representation of a decoded Git object coming out of a pack stream, with helpers to
2//! convert to/from strongly typed objects.
3
4use std::hash::{Hash, Hasher};
5
6use serde::{Deserialize, Serialize};
7
8use crate::{
9    hash::ObjectHash,
10    internal::object::{
11        ObjectTrait, blob::Blob, commit::Commit, tag::Tag, tree::Tree, types::ObjectType,
12    },
13};
14
15///
16/// Git object data from pack file
17///
18#[derive(Eq, Clone, Debug, Serialize, Deserialize)]
19pub struct Entry {
20    pub obj_type: ObjectType,
21    pub data: Vec<u8>,
22    pub hash: ObjectHash,
23    pub chain_len: usize,
24}
25
26impl PartialEq for Entry {
27    fn eq(&self, other: &Self) -> bool {
28        // hash is enough to compare, right?
29        self.obj_type == other.obj_type && self.hash == other.hash
30    }
31}
32
33impl Hash for Entry {
34    fn hash<H: Hasher>(&self, state: &mut H) {
35        self.obj_type.hash(state);
36        self.hash.hash(state);
37    }
38}
39
40impl From<Blob> for Entry {
41    fn from(value: Blob) -> Self {
42        Self {
43            obj_type: ObjectType::Blob,
44            data: value.data,
45            hash: value.id,
46            chain_len: 0,
47        }
48    }
49}
50
51impl From<Commit> for Entry {
52    fn from(value: Commit) -> Self {
53        Self {
54            obj_type: ObjectType::Commit,
55            data: value.to_data().unwrap(),
56            hash: value.id,
57            chain_len: 0,
58        }
59    }
60}
61
62impl From<Tree> for Entry {
63    fn from(value: Tree) -> Self {
64        Self {
65            obj_type: ObjectType::Tree,
66            data: value.to_data().unwrap(),
67            hash: value.id,
68            chain_len: 0,
69        }
70    }
71}
72
73impl From<Tag> for Entry {
74    fn from(value: Tag) -> Self {
75        Self {
76            obj_type: ObjectType::Tag,
77            data: value.to_data().unwrap(),
78            hash: value.id,
79            chain_len: 0,
80        }
81    }
82}