summaryrefslogtreecommitdiff
path: root/examples/recursive-sha256/src/main.rs
blob: 6fcbbfad9ce5e42675896b5e96501e27e5bb093f (plain)
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
//! Print the SHA256 sum of each regular file in the current directory recursively.

use std::{fs::File, path::PathBuf};

use parseq::ParallelIterator;
use sha2::{Digest, Sha256};
use walkdir::WalkDir;

fn main() {
    WalkDir::new(".")
        .sort_by_file_name()
        .into_iter()
        .filter_map(|entry| match entry {
            Ok(entry) if entry.file_type().is_file() => Some(Ok(entry.into_path())),
            Ok(_) => None,
            Err(err) => Some(Err(Error::WalkdirError(err))),
        })
        .map_parallel(|item| {
            item.and_then(|path| {
                let mut file =
                    File::open(&path).map_err(|err| Error::IoError(path.clone(), err))?;
                let mut hasher = Sha256::new();
                std::io::copy(&mut file, &mut hasher)
                    .map_err(|err| Error::IoError(path.clone(), err))?;
                Ok((path, hasher.finalize()))
            })
        })
        .for_each(|item| match item {
            Ok((path, hash)) => println!("{:x} {}", hash, path.display()),
            Err(err) => eprintln!("{err}"),
        });
}

#[derive(Debug)]
enum Error {
    WalkdirError(walkdir::Error),
    IoError(PathBuf, std::io::Error),
}

impl std::error::Error for Error {}

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Error::WalkdirError(err) => {
                if let Some(path) = err.path() {
                    write!(f, "{}: {}", path.display(), err)
                } else {
                    write!(f, "{err}")
                }
            }
            Error::IoError(path, err) => write!(f, "{}: {}", path.display(), err),
        }
    }
}
Generated by cgit. See skreutz.com for my tech blog and contact information.