summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: d49266b6e7c224efcedaf4d0b7371959b14f4b47 (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
56
57
58
59
60
extern crate chrono;
extern crate crypto_hash;
extern crate serde_yaml;
extern crate walkdir;
extern crate rayon;

use std::env;
use std::io;
use std::collections::BTreeMap;
use walkdir::{WalkDir, DirEntry};
use rayon::prelude::*;

mod format;


type Map = BTreeMap<&'static str, String>;


fn main() -> io::Result<()> {
    for dirname in env::args().skip(1) {
        let maps: Vec<DirEntry> = WalkDir::new(&dirname)
            .into_iter()
            .filter_map(|e| e.ok())
            .collect();
        let maps: Vec<Map> = maps
            .into_par_iter()
            .map(mkmap)
            .collect();
        for map in maps {
            println!("{}", serde_yaml::to_string(&map).unwrap());
        }
    }
    Ok(())
}


fn mkmap(e: DirEntry) -> Map {
    let fields = vec![
        ("Name", format::name(&e)),
        ("Mtime", format::mtime(&e)),
        ("Mode", format::mode(&e)),
        ("Ino", format::inode(&e)),
        ("Dev", format::dev(&e)),
        ("Nlink", format::nlink(&e)),
        ("Size", format::size(&e)),
        ("Uid", format::uid(&e)),
        ("Username", format::username(&e)),
        ("Gid", format::gid(&e)),
        ("Group", format::group(&e)),
    ];
    let mut map: BTreeMap<&str, String> =
        BTreeMap::from(fields.iter().cloned().collect());

    if let Ok(m) = e.metadata() {
        if m.is_file() {
            map.insert("SHA256", format::sha256(&e));
        }
    }
    map
}