use std::str::FromStr;
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Blob(Vec<u8>);
impl Blob {
#[must_use]
pub const fn new(contents: Vec<u8>) -> Self {
Blob(contents)
}
#[must_use]
pub const fn contents(&self) -> &Vec<u8> {
&self.0
}
#[must_use]
pub fn into_contents(self) -> Vec<u8> {
self.0
}
#[must_use]
#[allow(clippy::missing_const_for_fn)]
pub fn as_slice(&self) -> &[u8] {
&self.0
}
#[must_use]
pub fn meta(&self) -> BlobMeta {
BlobMeta::new(&self.0)
}
}
impl From<Vec<u8>> for Blob {
fn from(contents: Vec<u8>) -> Self {
Blob::new(contents)
}
}
impl From<&[u8]> for Blob {
fn from(contents: &[u8]) -> Self {
Blob::new(contents.to_vec())
}
}
impl From<Blob> for Vec<u8> {
fn from(blob: Blob) -> Self {
blob.into_contents()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlobMeta {
digest: Digest,
size_bytes: u64,
}
impl BlobMeta {
#[must_use]
pub fn new(contents: &[u8]) -> Self {
let digest = Digest::hash(contents);
let size_bytes = contents.len() as u64;
Self { digest, size_bytes }
}
#[must_use]
pub const fn from_digest_size(digest: Digest, size_bytes: u64) -> Self {
BlobMeta { digest, size_bytes }
}
#[must_use]
pub const fn digest(&self) -> Digest {
self.digest
}
#[must_use]
pub const fn size_bytes(&self) -> u64 {
self.size_bytes
}
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Digest([u8; 32]);
impl std::fmt::Debug for Digest {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Digest({})", hex::encode(self.0))
}
}
impl Digest {
#[must_use]
pub fn hash(data: &[u8]) -> Self {
let b3_digest = blake3::hash(data);
let mut bytes = [0; 32];
bytes.copy_from_slice(b3_digest.as_bytes());
Self(bytes)
}
#[must_use]
pub const fn as_bytes(&self) -> &[u8; 32] {
&self.0
}
pub fn from_bytes(bytes: &[u8]) -> Result<Self, error::InvalidDigest> {
if bytes.len() < 32 {
return Err(error::InvalidDigest::NotEnoughInput);
}
if bytes.len() > 32 {
return Err(error::InvalidDigest::InvalidLength);
}
let mut hash = [0; 32];
hash.copy_from_slice(&bytes[..32]);
Ok(Digest(hash))
}
}
impl From<[u8; 32]> for Digest {
fn from(bytes: [u8; 32]) -> Self {
Self(bytes)
}
}
impl std::fmt::Display for Digest {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
hex::encode(self.0).fmt(f)
}
}
impl FromStr for Digest {
type Err = error::InvalidDigest;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let bytes = hex::decode(s).map_err(error::InvalidDigest::InvalidHex)?;
if bytes.len() != 32 {
return Err(error::InvalidDigest::InvalidLength);
}
let mut hash = [0; 32];
hash.copy_from_slice(&bytes);
Ok(Digest(hash))
}
}
pub mod error {
use thiserror::Error;
#[derive(Clone, Copy, PartialEq, Error)]
pub enum InvalidDigest {
#[error("Not enough input")]
NotEnoughInput,
#[error("Invalid hex: {0}")]
InvalidHex(hex::FromHexError),
#[error("Invalid length")]
InvalidLength,
}
impl std::fmt::Debug for InvalidDigest {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(self, f)
}
}
}