http_content_digest/digest/
body.rs1use super::{ContentHasher, DigestHash};
2use http_body_util::BodyExt;
3
4pub struct DigestWithContent {
5 pub body: bytes::Bytes,
6 pub digest: DigestHash,
7}
8
9impl<T> BodyDigest for T where T: http_body::Body {}
10
11pub trait BodyDigest
12where
13 Self: http_body::Body,
14{
15 fn into_bytes(self) -> impl Future<Output = Result<bytes::Bytes, Self::Error>> + Send
16 where
17 Self: Sized + Send,
18 Self::Data: Send,
19 {
20 async { Ok(self.collect().await?.to_bytes()) }
21 }
22
23 fn digest<H: ContentHasher>(
24 self,
25 ) -> impl Future<Output = Result<DigestWithContent, Self::Error>> + Send
26 where
27 Self: Sized + Send,
28 Self::Data: Send,
29 {
30 async {
31 let bytes = self.into_bytes().await?;
32 let digest = H::hash(&bytes);
33
34 Ok(DigestWithContent {
35 body: bytes,
36 digest,
37 })
38 }
39 }
40}