1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use serde::{Deserialize, Serialize};
use crate::*;
/// Error type for [`Blob`] decode operations.
#[derive(Debug, thiserror::Error)]
pub enum BlobContentError {
#[error(transparent)]
Blob(#[from] BlobError),
}
#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub struct BlobContent {
pub hash: ContentHash,
pub size: Size,
pub data: Blob,
}
impl BlobContent {
pub fn create(data: &[u8]) -> Result<Self, BlobContentError> {
let hash = ContentHash::compute(data);
let size = data.len().into();
let blob = Blob::compressed(data)?;
Ok(Self {
hash,
size,
data: blob,
})
}
// let mut hasher = Sha256::new();
// hasher.update(data);
// let hash_bytes = hasher.finalize();
// let hash_hex = data_encoding::HEXLOWER.encode(&hash_bytes);
// let mut encoder = ZlibEncoder::new(Vec::new(), Compression::default());
// encoder.write_all(data)?;
// let compressed = encoder.finish()?;
pub fn decode(&self) -> Result<Vec<u8>, BlobError> {
self.data.decode()
}
}