use crate::{BlobRef, CommitRef, CommitRefIter, Data, Kind, ObjectRef, TagRef, TagRefIter, TreeRef, TreeRefIter};
impl<'a> Data<'a> {
pub fn new(data: &'a [u8], kind: Kind, hash_kind: gix_hash::Kind) -> Data<'a> {
Data { kind, hash_kind, data }
}
pub fn decode(&self) -> Result<ObjectRef<'a>, crate::decode::Error> {
Ok(match self.kind {
Kind::Tree => ObjectRef::Tree(TreeRef::from_bytes(self.data, self.hash_kind)?),
Kind::Blob => ObjectRef::Blob(BlobRef { data: self.data }),
Kind::Commit => ObjectRef::Commit(CommitRef::from_bytes(self.data)?),
Kind::Tag => ObjectRef::Tag(TagRef::from_bytes(self.data)?),
})
}
pub fn try_into_tree_iter(self) -> Option<TreeRefIter<'a>> {
match self.kind {
Kind::Tree => Some(TreeRefIter::from_bytes(self.data, self.hash_kind)),
_ => None,
}
}
pub fn try_into_commit_iter(self) -> Option<CommitRefIter<'a>> {
match self.kind {
Kind::Commit => Some(CommitRefIter::from_bytes(self.data)),
_ => None,
}
}
pub fn try_into_tag_iter(self) -> Option<TagRefIter<'a>> {
match self.kind {
Kind::Tag => Some(TagRefIter::from_bytes(self.data)),
_ => None,
}
}
}
pub mod verify {
#[derive(Debug, thiserror::Error)]
#[allow(missing_docs)]
pub enum Error {
#[error("Failed to hash object")]
Hasher(#[from] gix_hash::hasher::Error),
#[error(transparent)]
Verify(#[from] gix_hash::verify::Error),
}
impl crate::Data<'_> {
pub fn verify_checksum(&self, expected: &gix_hash::oid) -> Result<gix_hash::ObjectId, Error> {
let actual = crate::compute_hash(expected.kind(), self.kind, self.data)?;
actual.verify(expected)?;
Ok(actual)
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn size_of_object() {
#[cfg(target_pointer_width = "64")]
assert_eq!(std::mem::size_of::<Data<'_>>(), 24, "this shouldn't change unnoticed");
#[cfg(target_pointer_width = "32")]
assert_eq!(std::mem::size_of::<Data<'_>>(), 12, "this shouldn't change unnoticed");
}
}