fsblobstore 0.0.5

A file-system backed blob storage abstraction.
Documentation
use std::{
  fmt,
  hash::{Hash, Hasher},
  ops::Deref,
  str::FromStr
};

/// A hash of a content blob.
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
#[repr(transparent)]
pub struct ContentHash(Vec<u8>);

impl ContentHash {
  #[must_use]
  pub fn into_inner(self) -> Vec<u8> {
    self.0
  }
}

impl Hash for ContentHash {
  fn hash<H: Hasher>(&self, state: &mut H) {
    self.0.hash(state);
  }
}

impl From<Vec<u8>> for ContentHash {
  fn from(vec: Vec<u8>) -> Self {
    assert_eq!(vec.len(), 32);
    Self(vec)
  }
}

impl Deref for ContentHash {
  type Target = [u8];
  fn deref(&self) -> &Self::Target {
    &self.0
  }
}

impl fmt::Display for ContentHash {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    let hexhash = hex::encode(&self.0);
    write!(f, "{hexhash}")
  }
}

impl FromStr for ContentHash {
  type Err = ();

  fn from_str(s: &str) -> Result<Self, Self::Err> {
    hex::decode(s).map_or(Err(()), |buf| Ok(Self::from(buf)))
  }
}

// vim: set ft=rust et sw=2 ts=2 sts=2 cinoptions=2 tw=79 :