1use log::info;
2use sha2::{Digest, Sha512};
3use walkdir::WalkDir;
4
5use crate::hash_path;
6use crate::mapper;
7use crate::Config;
8use crate::Error;
9use crate::Map;
10
11impl Map {
12 pub fn get_blob(&self) -> &String {
14 &self.blob
15 }
16
17 fn build_configs(
19 path: &str,
20 subset: String,
21 family: String,
22 tag: &String,
23 ) -> Result<Vec<Config>, Error> {
24 let mut configs: Vec<Config> = Vec::new();
25
26 for entity in WalkDir::new(path) {
27 match entity {
28 Ok(e) => {
29 let map_path =
30 mapper::real_path_to_map(&e.path().to_str().unwrap().to_string())?;
31 configs.push(Config::from(
32 e.path().to_str().unwrap().to_string(),
33 map_path,
34 subset.clone(),
35 family.clone(),
36 tag.clone(),
37 )?);
38 }
39 Err(_) => (),
40 }
41 }
42 return Ok(configs);
43 }
44
45 fn get_map_hash(configs: &Vec<Config>) -> String {
48 let mut hasher = Sha512::new();
49 for c in configs {
50 hasher.update(c.get_hash());
51 }
52 format!("{:x}", hasher.finalize())
53 }
54
55 pub fn new(path: &String, subset: String, family: String, tag: String) -> Result<Self, Error> {
57 info!("Building map for {}", path);
58
59 let clean_path = path_clean::clean(path);
60
61 let map_blob = hash_path(&clean_path);
63 let config_vec = Map::build_configs(&clean_path, subset, family, &tag)?;
64 let map_hash = Map::get_map_hash(&config_vec);
65
66 return Ok(Map {
67 blob: map_blob,
68 ver: 0,
69 hash: map_hash,
70 tag: tag,
71 configs: config_vec,
72 });
73 }
74}