margo_fetch/
downloader.rs

1#[cfg(feature = "downloader_simple_reqwest")]
2mod simple_reqwest;
3#[cfg(feature = "downloader_simple_reqwest")]
4pub use self::simple_reqwest::SimpleReqwestDownloader;
5
6use futures::future::Future as StdFuture;
7use std::path::Path;
8
9#[allow(dead_code)]
10type Error = ();
11
12#[allow(dead_code)]
13#[cfg(feature = "std")]
14type Future<T> = Box<dyn StdFuture<Item = T, Error = Error> + Send>;
15
16// TODO: find an alternative to path, so it is also usable with no_std
17#[cfg(feature = "std")]
18pub trait Downloader {
19    type F: StdFuture<Item = (), Error = ()> + Send;
20
21    fn checked_download(&self, download_url: &str, target_path: &Path, checksum: &str) -> Self::F;
22}
23
24#[cfg(feature = "downloader_checksum")]
25pub mod checksum {
26    use log::trace;
27    use sha2::{Digest, Sha256};
28
29    pub fn verify_checksum<R: std::io::Read>(input: &mut R, checksum: &str) -> bool {
30        let mut hasher = Sha256::new();
31
32        std::io::copy(input, &mut hasher).unwrap();
33        let calculated_checksum = hex::encode(hasher.result());
34        trace!("CALCULATED CHECKSUM: {:?}", calculated_checksum);
35        trace!("EXPECTED CHECKSUM: {:?}", checksum);
36        let checksums_match = &calculated_checksum == checksum;
37
38        checksums_match
39    }
40}