use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::fs::{read_dir, File};
use std::io::{Error, ErrorKind, Result};
use std::path::Path;
use crate::Sha384;
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub struct Manifest {
pub time: u64,
pub files: BTreeMap<String, String>,
}
impl Manifest {
pub fn new<P: AsRef<Path>>(time: u64, path: P) -> Result<Manifest> {
let mut files = BTreeMap::new();
for entry_res in read_dir(path.as_ref())? {
let entry = entry_res?;
let name = entry
.file_name()
.into_string()
.map_err(|_| Error::new(ErrorKind::InvalidData, "Filename is not UTF-8"))?;
let file = File::open(entry.path())?;
let sha = Sha384::new(file)?;
files.insert(name, sha.to_base32());
}
Ok(Manifest { time, files })
}
}