1use serde::{Deserialize, Serialize};
4use std::collections::BTreeMap;
5use std::fs::{read_dir, File};
6use std::io::{Error, ErrorKind, Result};
7use std::path::Path;
8
9use crate::Sha384;
10
11#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
13pub struct Manifest {
14 pub time: u64,
16 pub files: BTreeMap<String, String>,
18}
19
20impl Manifest {
21 pub fn new<P: AsRef<Path>>(time: u64, path: P) -> Result<Manifest> {
36 let mut files = BTreeMap::new();
37
38 for entry_res in read_dir(path.as_ref())? {
39 let entry = entry_res?;
40
41 let name = entry
42 .file_name()
43 .into_string()
44 .map_err(|_| Error::new(ErrorKind::InvalidData, "Filename is not UTF-8"))?;
45
46 let file = File::open(entry.path())?;
47 let sha = Sha384::new(file)?;
48
49 files.insert(name, sha.to_base32());
50 }
51
52 Ok(Manifest { time, files })
53 }
54}