git_internal/internal/object/
mod.rs

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