mithril_aggregator/tools/
digest_helpers.rs1use std::path::Path;
2use thiserror::Error;
3
4#[derive(Error, Debug, PartialEq, Eq)]
5pub enum DigestExtractError {
6 #[error("Could not extract file stem from path: `{0}`")]
7 FileStemExtractFailed(String),
8
9 #[error("Could not found digest in the file name: `{0}`")]
10 DigestNotFound(String),
11
12 #[error("Invalid digest, all characters should be hexadecimal: `{0}`")]
13 InvalidDigest(String),
14}
15
16pub fn extract_digest_from_path(filepath: &Path) -> Result<String, DigestExtractError> {
25 let archive_name = filepath.file_stem().filter(|f| f.to_str().is_some());
26 if archive_name.is_none() {
27 return Err(DigestExtractError::FileStemExtractFailed(format!(
28 "{}",
29 filepath.display()
30 )));
31 }
32
33 match archive_name.unwrap().to_str().unwrap().split('.').nth(1) {
34 Some(digest) => {
35 if digest.chars().all(|c| c.is_ascii_hexdigit()) {
37 Ok(digest.to_string())
38 } else {
39 Err(DigestExtractError::InvalidDigest(digest.to_string()))
40 }
41 }
42 None => Err(DigestExtractError::DigestNotFound(format!(
43 "{}",
44 filepath.display()
45 ))),
46 }
47}
48
49#[cfg(test)]
50mod tests {
51 use super::extract_digest_from_path;
52 use crate::tools::digest_helpers::DigestExtractError;
53 use std::path::Path;
54
55 #[test]
56 fn should_extract_digest_from_path() {
57 let digest = "41e27b9ed5a32531b95b2b7ff3c0757591a06a337efaf19a524a998e348028e7".to_string();
58 let filename = format!("testnet.{digest}.tar.gz");
59 let result = extract_digest_from_path(Path::new(&filename));
60
61 assert_eq!(Ok(digest), result);
62 }
63
64 #[test]
65 fn extract_digest_from_path_fail_if_file_named_incorrectly() {
66 let digest = "41e27b9ed5a32531b95b2b7ff3c0757591a06a337efaf19a524a998e348028e7".to_string();
67 let filename = format!("{digest}.zip");
68 let result = extract_digest_from_path(Path::new(&filename));
69
70 assert_eq!(Err(DigestExtractError::DigestNotFound(filename)), result);
71 }
72
73 #[test]
74 fn extract_digest_from_path_fail_if_tar_file_named_incorrectly() {
75 let digest = "41e27b9ed5a32531b95b2b7ff3c0757591a06a337efaf19a524a998e348028e7".to_string();
76 let filename = format!("{digest}.tar.gz");
77 let result = extract_digest_from_path(Path::new(&filename));
78
79 assert_eq!(
80 Err(DigestExtractError::InvalidDigest("tar".to_string())),
81 result
82 );
83 }
84
85 #[test]
86 fn extract_digest_from_path_fail_if_digest_not_hexadecimal() {
87 let digest = "not_hexadecimal".to_string();
88 let filename = format!("testnet.{digest}.tar.gz");
89 let result = extract_digest_from_path(Path::new(&filename));
90
91 assert_eq!(Err(DigestExtractError::InvalidDigest(digest)), result);
92 }
93}