hashdeep_compare/
sort.rs

1use crate::common::LogFile;
2use crate::log_entry::LogEntry;
3use crate::log_ops;
4
5/// Reads a hashdeep log file and writes its entries to a new file, sorted by name.
6///
7/// On success, returns a Vec of warning strings, if any warnings were emitted while reading the file.
8///
9/// # Errors
10///
11/// Any error emitted while reading or writing the files will be returned.
12pub fn sort_log(filename: &str, out_filename: &str) -> Result<Option<Vec<String>>, Box<dyn std::error::Error>>{
13
14    fn sort_entries(log_file: &mut LogFile<Vec<LogEntry>>) {
15        log_file.entries.sort_by(|v1, v2| {
16            v1.filename.cmp(&v2.filename)
17        });
18    }
19
20    log_ops::process_log(filename, out_filename, sort_entries)
21}
22
23#[cfg(test)]
24mod test {
25    use super::*;
26
27    use predicates::prelude::*;
28
29    #[test]
30    fn sort_log_test() {
31        {
32            let temp_dir = tempfile::tempdir().unwrap();
33            let temp_file = temp_dir.path().join("test1 sorted.txt");
34            let temp_file_path_str = temp_file.to_str().unwrap();
35
36            sort_log("tests/test1.txt", temp_file_path_str).unwrap();
37
38            let p = predicates::path::eq_file("tests/test1 sorted.txt");
39            assert!(p.eval(temp_file.as_path()));
40        }
41    }
42}