thin-fetch 0.1.0

Low-level Git crate to fetch objects with high granularity
Documentation
use ring::digest::Digest;

use crate::HashKind;
use crate::utils::get_hash;

#[derive(Clone)]
pub struct Blob {
    pub hash: Digest,
    pub content: Vec<u8>,
}

impl Blob {
    #[must_use]
    pub fn new(content: Vec<u8>, hash_kind: HashKind) -> Self {
        Self {
            hash: get_hash("blob", &content, hash_kind),
            content,
        }
    }

    #[must_use]
    pub fn size(&self) -> u64 {
        self.content.len() as u64
    }
}

impl std::fmt::Debug for Blob {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut s = f.debug_struct("Blob");

        s.field("hash", &self.hash);

        match str::from_utf8(&self.content) {
            Ok(data) => s.field("content", &data),
            Err(_) => s.field("content", &self.content),
        };

        s.finish()
    }
}