1use crate::{BlobRef, CommitRef, CommitRefIter, Data, Kind, ObjectRef, TagRef, TagRefIter, TreeRef, TreeRefIter};
4
5impl<'a> Data<'a> {
6 pub fn new(kind: Kind, data: &'a [u8]) -> Data<'a> {
8 Data { kind, data }
9 }
10 pub fn decode(&self) -> Result<ObjectRef<'a>, crate::decode::Error> {
16 Ok(match self.kind {
17 Kind::Tree => ObjectRef::Tree(TreeRef::from_bytes(self.data)?),
18 Kind::Blob => ObjectRef::Blob(BlobRef { data: self.data }),
19 Kind::Commit => ObjectRef::Commit(CommitRef::from_bytes(self.data)?),
20 Kind::Tag => ObjectRef::Tag(TagRef::from_bytes(self.data)?),
21 })
22 }
23
24 pub fn try_into_tree_iter(self) -> Option<TreeRefIter<'a>> {
27 match self.kind {
28 Kind::Tree => Some(TreeRefIter::from_bytes(self.data)),
29 _ => None,
30 }
31 }
32
33 pub fn try_into_commit_iter(self) -> Option<CommitRefIter<'a>> {
36 match self.kind {
37 Kind::Commit => Some(CommitRefIter::from_bytes(self.data)),
38 _ => None,
39 }
40 }
41
42 pub fn try_into_tag_iter(self) -> Option<TagRefIter<'a>> {
45 match self.kind {
46 Kind::Tag => Some(TagRefIter::from_bytes(self.data)),
47 _ => None,
48 }
49 }
50}
51
52pub mod verify {
54 #[derive(Debug, thiserror::Error)]
56 #[allow(missing_docs)]
57 pub enum Error {
58 #[error("Failed to hash object")]
59 Hasher(#[from] gix_hash::hasher::Error),
60 #[error(transparent)]
61 Verify(#[from] gix_hash::verify::Error),
62 }
63
64 impl crate::Data<'_> {
65 pub fn verify_checksum(&self, expected: &gix_hash::oid) -> Result<gix_hash::ObjectId, Error> {
69 let actual = crate::compute_hash(expected.kind(), self.kind, self.data)?;
70 actual.verify(expected)?;
71 Ok(actual)
72 }
73 }
74}
75
76#[cfg(test)]
77mod tests {
78 use super::*;
79
80 #[test]
81 fn size_of_object() {
82 #[cfg(target_pointer_width = "64")]
83 assert_eq!(std::mem::size_of::<Data<'_>>(), 24, "this shouldn't change unnoticed");
84 #[cfg(target_pointer_width = "32")]
85 assert_eq!(std::mem::size_of::<Data<'_>>(), 12, "this shouldn't change unnoticed");
86 }
87}