1use bytes::Bytes;
2use data_encoding::BASE64;
3use std::str::FromStr;
4use thiserror::Error;
5
6#[derive(Clone, Copy, PartialEq, Eq, Hash)]
7#[cfg_attr(feature = "serde", derive(serde_with::SerializeDisplay))]
8#[repr(transparent)]
9pub struct B3Digest([u8; Self::LENGTH]);
10
11impl B3Digest {
12 pub const LENGTH: usize = blake3::OUT_LEN;
13 pub const EMPTY: Self = B3Digest([
15 0xaf, 0x13, 0x49, 0xb9, 0xf5, 0xf9, 0xa1, 0xa6, 0xa0, 0x40, 0x4d, 0xea, 0x36, 0xdc, 0xc9,
16 0x49, 0x9b, 0xcb, 0x25, 0xc9, 0xad, 0xc1, 0x12, 0xb7, 0xcc, 0x9a, 0x93, 0xca, 0xe4, 0x1f,
17 0x32, 0x62,
18 ]);
19}
20
21#[derive(Error, Debug, PartialEq)]
22pub enum Error {
23 #[error("invalid digest length: {0}")]
24 InvalidDigestLen(usize),
25 #[error("invalid hash type: expected a 'blake3-' prefixed digest")]
26 InvalidHashType,
27}
28
29impl AsRef<[u8; B3Digest::LENGTH]> for B3Digest {
30 fn as_ref(&self) -> &[u8; Self::LENGTH] {
31 &self.0
32 }
33}
34
35impl std::ops::Deref for B3Digest {
36 type Target = [u8; Self::LENGTH];
37
38 fn deref(&self) -> &Self::Target {
39 &self.0
40 }
41}
42
43impl From<B3Digest> for bytes::Bytes {
44 fn from(val: B3Digest) -> Self {
45 Bytes::copy_from_slice(&val.0)
46 }
47}
48
49impl From<blake3::Hash> for B3Digest {
50 fn from(value: blake3::Hash) -> Self {
51 Self(*value.as_bytes())
52 }
53}
54impl From<digest::Output<blake3::Hasher>> for B3Digest {
55 fn from(value: digest::Output<blake3::Hasher>) -> Self {
56 Self(value.into())
57 }
58}
59
60impl TryFrom<&[u8]> for B3Digest {
61 type Error = Error;
62
63 fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
66 Ok(Self(
67 value
68 .try_into()
69 .map_err(|_e| Error::InvalidDigestLen(value.len()))?,
70 ))
71 }
72}
73
74impl TryFrom<bytes::Bytes> for B3Digest {
75 type Error = Error;
76
77 fn try_from(value: bytes::Bytes) -> Result<Self, Self::Error> {
78 value[..].try_into()
79 }
80}
81
82impl TryFrom<Vec<u8>> for B3Digest {
83 type Error = Error;
84
85 fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
86 value[..].try_into()
87 }
88}
89
90impl From<&[u8; B3Digest::LENGTH]> for B3Digest {
91 fn from(value: &[u8; B3Digest::LENGTH]) -> Self {
92 Self(*value)
93 }
94}
95
96impl From<B3Digest> for [u8; B3Digest::LENGTH] {
97 fn from(value: B3Digest) -> Self {
98 value.0
99 }
100}
101
102impl std::fmt::Display for B3Digest {
103 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
104 f.write_str("blake3-").unwrap();
105 BASE64.encode_write(&self.0, f)
106 }
107}
108
109impl std::fmt::Debug for B3Digest {
110 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
111 f.write_str("blake3-").unwrap();
112 BASE64.encode_write(&self.0, f)
113 }
114}
115
116impl FromStr for B3Digest {
117 type Err = Error;
118
119 fn from_str(s: &str) -> Result<Self, Self::Err> {
120 if !s.starts_with("blake3-") {
121 return Err(Error::InvalidHashType);
122 }
123 let encoded = &s[7..];
124 let decoded = BASE64
125 .decode(encoded.as_bytes())
126 .map_err(|_| Error::InvalidDigestLen(s.len()))?;
127 decoded.as_slice().try_into()
128 }
129}
130
131#[cfg(test)]
132mod test {
133 use crate::B3Digest;
134
135 #[test]
137 fn empty() {
138 let exp: B3Digest = blake3::hash(b"").as_bytes().into();
139 assert_eq!(exp, B3Digest::EMPTY);
140 }
141}