use super::Digest;
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct RootHash(Digest<16>);
impl std::ops::Deref for RootHash {
type Target = Digest<16>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl RootHash {
pub(crate) const fn new(value: PageDigest) -> Self {
Self(value.0)
}
}
#[cfg(feature = "digest_base64")]
impl std::fmt::Display for RootHash {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct PageDigest(Digest<16>);
impl std::ops::Deref for PageDigest {
type Target = Digest<16>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl PageDigest {
pub const fn new(value: [u8; 16]) -> Self {
Self(Digest::new(value))
}
}
impl From<Digest<16>> for PageDigest {
fn from(value: Digest<16>) -> Self {
Self(value)
}
}
#[cfg(feature = "digest_base64")]
impl std::fmt::Display for PageDigest {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct ValueDigest<const N: usize>(Digest<N>);
impl<const N: usize> std::ops::Deref for ValueDigest<N> {
type Target = Digest<N>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<const N: usize> ValueDigest<N> {
pub(crate) const fn new(value: Digest<N>) -> Self {
Self(value)
}
}
#[cfg(feature = "digest_base64")]
impl<const N: usize> std::fmt::Display for ValueDigest<N> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[cfg(feature = "digest_base64")]
fn test_base64_format() {
let d = Digest::new([0x62, 0x61, 0x6e, 0x61, 0x6e, 0x61, 0x73, 0x0a]);
assert_eq!(d.to_string(), "YmFuYW5hcwo=");
let value = ValueDigest::new(Digest::new([
0x62, 0x61, 0x6e, 0x61, 0x6e, 0x61, 0x73, 0x0a,
]));
assert_eq!(value.to_string(), "YmFuYW5hcwo=");
let page = PageDigest::from(Digest::new([
0x62, 0x61, 0x6e, 0x61, 0x6e, 0x61, 0x73, 0x0a, 0x62, 0x61, 0x6e, 0x61, 0x6e, 0x61,
0x73, 0x0a,
]));
assert_eq!(page.to_string(), "YmFuYW5hcwpiYW5hbmFzCg==");
}
#[test]
fn test_as_bytes() {
let b = [
42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42,
];
let d = PageDigest::from(Digest::new(b));
assert_eq!(b, *d.as_bytes());
}
}