git_internal/internal/object/
mod.rs

1//! Object model definitions for Git blobs, trees, commits, tags, and supporting traits that let the
2//! pack/zlib layers create strongly typed values from raw bytes.
3
4pub mod blob;
5pub mod commit;
6pub mod note;
7pub mod signature;
8pub mod tag;
9pub mod tree;
10pub mod types;
11pub mod utils;
12
13use std::{
14    fmt::Display,
15    io::{BufRead, Read},
16};
17
18use crate::{
19    errors::GitError,
20    hash::ObjectHash,
21    internal::{object::types::ObjectType, zlib::stream::inflate::ReadBoxed},
22};
23
24/// **The Object Trait**
25/// Defines the common interface for all Git object types, including blobs, trees, commits, and tags.
26pub trait ObjectTrait: Send + Sync + Display {
27    /// Creates a new object from a byte slice.
28    fn from_bytes(data: &[u8], hash: ObjectHash) -> Result<Self, GitError>
29    where
30        Self: Sized;
31
32    /// Generate a new Object from a `ReadBoxed<BufRead>`.
33    /// the input size,is only for new a vec with directive space allocation
34    /// the input data stream and output object should be plain base object .
35    fn from_buf_read<R: BufRead>(read: &mut ReadBoxed<R>, size: usize) -> Self
36    where
37        Self: Sized,
38    {
39        let mut content: Vec<u8> = Vec::with_capacity(size);
40        read.read_to_end(&mut content).unwrap();
41        let digest = read.hash.clone().finalize();
42        let hash = ObjectHash::from_bytes(&digest).unwrap();
43        Self::from_bytes(&content, hash).unwrap()
44    }
45
46    /// Returns the type of the object.
47    fn get_type(&self) -> ObjectType;
48
49    fn get_size(&self) -> usize;
50
51    fn to_data(&self) -> Result<Vec<u8>, GitError>;
52}