Skip to main content

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