git_internal/internal/object/
mod.rs

1pub mod blob;
2pub mod commit;
3pub mod note;
4pub mod signature;
5pub mod tag;
6pub mod tree;
7pub mod types;
8pub mod utils;
9
10use std::{
11    fmt::Display,
12    io::{BufRead, Read},
13    str::FromStr,
14};
15
16use sha1::Digest;
17
18use crate::internal::object::types::ObjectType;
19use crate::internal::zlib::stream::inflate::ReadBoxed;
20use crate::{errors::GitError, hash::ObjectHash};
21
22pub trait ObjectTrait: Send + Sync + Display {
23    /// Creates a new object from a byte slice.
24    fn from_bytes(data: &[u8], hash: ObjectHash) -> Result<Self, GitError>
25    where
26        Self: Sized;
27
28    /// Generate a new Object from a `ReadBoxed<BufRead>`.
29    /// the input size,is only for new a vec with directive space allocation
30    /// the input data stream and output object should be plain base object .
31    fn from_buf_read<R: BufRead>(read: &mut ReadBoxed<R>, size: usize) -> Self
32    where
33        Self: Sized,
34    {
35        let mut content: Vec<u8> = Vec::with_capacity(size);
36        read.read_to_end(&mut content).unwrap();
37        let h = read.hash.clone();
38        let hash_str = h.finalize();
39        Self::from_bytes(
40            &content,
41            ObjectHash::from_str(&format!("{hash_str:x}")).unwrap(),
42        )
43        .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}