1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use super::{Algorithm, ContentDigest, Reader, Writer};
use std::collections::BTreeSet;
use std::ops::{Deref, DerefMut};
use futures::io::{self, copy, sink, AsyncRead};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Algorithms(BTreeSet<Algorithm>);
impl Default for Algorithms {
fn default() -> Self {
let mut set = BTreeSet::new();
assert!(set.insert(Algorithm::Sha224));
assert!(set.insert(Algorithm::Sha256));
assert!(set.insert(Algorithm::Sha384));
assert!(set.insert(Algorithm::Sha512));
Self(set)
}
}
impl From<BTreeSet<Algorithm>> for Algorithms {
fn from(value: BTreeSet<Algorithm>) -> Self {
Self(value)
}
}
impl Deref for Algorithms {
type Target = BTreeSet<Algorithm>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for Algorithms {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl Algorithms {
pub fn reader<T>(&self, reader: T) -> Reader<T> {
Reader::new(reader, self.iter().cloned())
}
pub fn writer<T>(&self, writer: T) -> Writer<T> {
Writer::new(writer, self.iter().cloned())
}
pub async fn read(&self, reader: impl Unpin + AsyncRead) -> io::Result<(u64, ContentDigest)> {
let mut r = self.reader(reader);
let n = copy(&mut r, &mut sink()).await?;
Ok((n, r.digests()))
}
pub fn read_sync(&self, reader: impl std::io::Read) -> io::Result<(u64, ContentDigest)> {
let mut r = self.reader(reader);
let n = std::io::copy(&mut r, &mut std::io::sink())?;
Ok((n, r.digests()))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[async_std::test]
async fn digest() {
let algorithms = Algorithms::default();
let rdr = &b"foo"[..];
let content_digest = "sha-224=:CAj2TmDViXn8tnbJbsk4Jw3qQkRa7vzTpOb42w==:,sha-256=:LCa0a2j/xo/5m0U8HTBBNBNCLXBkg7+g+YpeiGJm564=:,sha-384=:mMEf/f3VQGdrGhN8saIrKnA1DJpEFx1rEYDGvly7LuP3nVMsih3Z7y6OCOdSo7q7:,sha-512=:9/u6bgY2+JDlb7vzKD5STG+jIErimDgtYkdB0NxmODJuKCxBvl5CVNiCB3LFUYosWowMf37aGVlKfrU5RT4e1w==:"
.parse::<ContentDigest>()
.unwrap();
assert_eq!(
algorithms.read(rdr).await.unwrap(),
("foo".len() as _, content_digest.clone())
);
assert_eq!(
algorithms.read_sync(rdr).unwrap(),
("foo".len() as _, content_digest)
);
}
}