thin_fetch/object/
blob.rs1use ring::digest::Digest;
2
3use crate::HashKind;
4use crate::utils::get_hash;
5
6#[derive(Clone)]
7pub struct Blob {
8 pub hash: Digest,
9 pub content: Vec<u8>,
10}
11
12impl Blob {
13 #[must_use]
14 pub fn new(content: Vec<u8>, hash_kind: HashKind) -> Self {
15 Self {
16 hash: get_hash("blob", &content, hash_kind),
17 content,
18 }
19 }
20
21 #[must_use]
22 pub fn size(&self) -> u64 {
23 self.content.len() as u64
24 }
25}
26
27impl std::fmt::Debug for Blob {
28 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
29 let mut s = f.debug_struct("Blob");
30
31 s.field("hash", &self.hash);
32
33 match str::from_utf8(&self.content) {
34 Ok(data) => s.field("content", &data),
35 Err(_) => s.field("content", &self.content),
36 };
37
38 s.finish()
39 }
40}