Skip to main content

git_async/object/
blob.rs

1use crate::object::{Object, ObjectId};
2use accessory::Accessors;
3use alloc::vec::Vec;
4
5/// A blob object
6///
7/// Represents arbitrary data, e.g. the contents of a file
8#[derive(Debug, Clone, Accessors)]
9pub struct Blob {
10    /// The [`ObjectId`] of the blob object
11    #[access(get(cp))]
12    id: ObjectId,
13
14    /// The data that the blob contains
15    #[access(get(ty(&[u8])))]
16    data: Vec<u8>,
17}
18
19impl PartialEq for Blob {
20    fn eq(&self, other: &Self) -> bool {
21        self.id == other.id
22    }
23}
24impl Eq for Blob {}
25impl PartialOrd for Blob {
26    fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
27        Some(self.cmp(other))
28    }
29}
30impl Ord for Blob {
31    fn cmp(&self, other: &Self) -> core::cmp::Ordering {
32        self.id.cmp(&other.id)
33    }
34}
35
36impl Blob {
37    pub(crate) fn new(id: ObjectId, data: Vec<u8>) -> Self {
38        Blob { id, data }
39    }
40
41    /// Move the data out of the blob object.
42    pub fn data_owned(self) -> Vec<u8> {
43        self.data
44    }
45
46    /// Wrap the [`Blob`] as a generic [`Object`].
47    pub fn as_object(self) -> Object {
48        Object::Blob(self)
49    }
50}