Skip to main content

compare_dir/
lib.rs

1mod column_formatter;
2mod dir_comparer;
3mod file_comparer;
4mod file_hash_cache;
5mod file_hasher;
6mod file_item;
7mod file_iterator;
8mod progress;
9mod sort_stream;
10mod system_time_ext;
11
12pub(crate) use column_formatter::ColumnFormatter;
13pub use dir_comparer::{DirectoryComparer, FileComparisonMethod};
14pub use file_comparer::{Classification, FileComparer, FileComparisonResult};
15pub(crate) use file_hash_cache::FileHashCache;
16pub use file_hasher::{DuplicatedFiles, FileHasher};
17pub use file_item::FileItem;
18pub(crate) use file_iterator::FileIterator;
19pub(crate) use progress::Progress;
20pub use progress::ProgressBuilder;
21pub(crate) use progress::ProgressValue;
22pub(crate) use sort_stream::sort_stream;
23pub(crate) use system_time_ext::SystemTimeExt;
24
25/// Output format for comparison results.
26#[derive(Debug, Clone, Copy, PartialEq, Eq)]
27pub enum OutputFormat {
28    Default,
29    Symbol,
30    Yaml,
31    Shell,
32    PowerShell,
33}
34
35use std::path::{Path, PathBuf};
36
37pub(crate) fn build_thread_pool(
38    threads: usize,
39) -> Result<rayon::ThreadPool, rayon::ThreadPoolBuildError> {
40    rayon::ThreadPoolBuilder::new().num_threads(threads).build()
41}
42
43pub(crate) fn human_readable_size(size: u64) -> String {
44    const KB: u64 = 1024;
45    if size < KB {
46        return format!("{} bytes", size);
47    }
48    const KB_AS_F: f64 = KB as f64;
49    let mut size = size as f64;
50    for unit in ["KB", "MB"] {
51        size /= KB_AS_F;
52        if size < KB_AS_F {
53            return format!("{:.1}{}", size, unit);
54        }
55    }
56    format!("{:.1}GB", size / KB_AS_F)
57}
58
59pub(crate) fn common_ancestor(paths: &[impl AsRef<Path>]) -> Option<PathBuf> {
60    if paths.is_empty() {
61        return None;
62    }
63    let mut iter = paths.iter();
64    let mut common = iter.next()?.as_ref().to_path_buf();
65    for path in iter {
66        let path = path.as_ref();
67        let mut new_common = PathBuf::new();
68        for (c, p) in common.components().zip(path.components()) {
69            if c == p {
70                new_common.push(c);
71            } else {
72                break;
73            }
74        }
75        common = new_common;
76        if common.as_os_str().is_empty() {
77            return None;
78        }
79    }
80    Some(common)
81}
82
83#[cfg(test)]
84mod tests {
85    use super::*;
86
87    #[test]
88    fn human_readable_size_tests() {
89        assert_eq!(human_readable_size(0), "0 bytes");
90        assert_eq!(human_readable_size(1), "1 bytes");
91        assert_eq!(human_readable_size(1023), "1023 bytes");
92        assert_eq!(human_readable_size(1024), "1.0KB");
93        assert_eq!(human_readable_size(1024 * 1024), "1.0MB");
94        assert_eq!(human_readable_size(1024 * 1024 * 1024), "1.0GB");
95        assert_eq!(human_readable_size(1024 * 1024 * 1024 * 1024), "1024.0GB");
96    }
97
98    #[test]
99    fn common_ancestor_tests() {
100        let empty: &[PathBuf] = &[];
101        assert_eq!(common_ancestor(empty), None);
102
103        let p1 = Path::new("/a/b/c");
104        assert_eq!(common_ancestor(&[p1]), Some(PathBuf::from("/a/b/c")));
105
106        let p2 = Path::new("/a/b/d");
107        assert_eq!(common_ancestor(&[p1, p2]), Some(PathBuf::from("/a/b")));
108
109        let p3 = Path::new("/a/x/y");
110        assert_eq!(common_ancestor(&[p1, p2, p3]), Some(PathBuf::from("/a")));
111
112        let p4 = Path::new("/b/c");
113        assert_eq!(common_ancestor(&[p1, p4]), Some(PathBuf::from("/")));
114
115        // Prefix case
116        let p5 = Path::new("/a/b");
117        assert_eq!(common_ancestor(&[p1, p5]), Some(PathBuf::from("/a/b")));
118
119        // Relative paths (no common root)
120        let r1 = Path::new("a/b");
121        let r2 = Path::new("c/d");
122        assert_eq!(common_ancestor(&[r1, r2]), None);
123
124        // Mixed absolute/relative
125        let a1 = Path::new("/a/b");
126        let r3 = Path::new("a/b");
127        assert_eq!(common_ancestor(&[a1, r3]), None);
128    }
129}