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 sort_stream::sort_stream;
22pub(crate) use system_time_ext::SystemTimeExt;
23
24#[derive(Debug, Clone, Copy, PartialEq, Eq)]
26pub enum OutputFormat {
27 Default,
28 Symbol,
29 Yaml,
30 Shell,
31 PowerShell,
32}
33
34use std::path::{Path, PathBuf};
35
36pub(crate) fn build_thread_pool(
37 threads: usize,
38) -> Result<rayon::ThreadPool, rayon::ThreadPoolBuildError> {
39 rayon::ThreadPoolBuilder::new().num_threads(threads).build()
40}
41
42pub(crate) fn human_readable_size(size: u64) -> String {
43 const KB: u64 = 1024;
44 if size < KB {
45 return format!("{} bytes", size);
46 }
47 const KB_AS_F: f64 = KB as f64;
48 let mut size = size as f64;
49 for unit in ["KB", "MB"] {
50 size /= KB_AS_F;
51 if size < KB_AS_F {
52 return format!("{:.1}{}", size, unit);
53 }
54 }
55 format!("{:.1}GB", size / KB_AS_F)
56}
57
58pub(crate) fn common_ancestor(paths: &[impl AsRef<Path>]) -> Option<PathBuf> {
59 if paths.is_empty() {
60 return None;
61 }
62 let mut iter = paths.iter();
63 let mut common = iter.next()?.as_ref().to_path_buf();
64 for path in iter {
65 let path = path.as_ref();
66 let mut new_common = PathBuf::new();
67 for (c, p) in common.components().zip(path.components()) {
68 if c == p {
69 new_common.push(c);
70 } else {
71 break;
72 }
73 }
74 common = new_common;
75 if common.as_os_str().is_empty() {
76 return None;
77 }
78 }
79 Some(common)
80}
81
82#[cfg(test)]
83mod tests {
84 use super::*;
85
86 #[test]
87 fn human_readable_size_tests() {
88 assert_eq!(human_readable_size(0), "0 bytes");
89 assert_eq!(human_readable_size(1), "1 bytes");
90 assert_eq!(human_readable_size(1023), "1023 bytes");
91 assert_eq!(human_readable_size(1024), "1.0KB");
92 assert_eq!(human_readable_size(1024 * 1024), "1.0MB");
93 assert_eq!(human_readable_size(1024 * 1024 * 1024), "1.0GB");
94 assert_eq!(human_readable_size(1024 * 1024 * 1024 * 1024), "1024.0GB");
95 }
96
97 #[test]
98 fn common_ancestor_tests() {
99 let empty: &[PathBuf] = &[];
100 assert_eq!(common_ancestor(empty), None);
101
102 let p1 = Path::new("/a/b/c");
103 assert_eq!(common_ancestor(&[p1]), Some(PathBuf::from("/a/b/c")));
104
105 let p2 = Path::new("/a/b/d");
106 assert_eq!(common_ancestor(&[p1, p2]), Some(PathBuf::from("/a/b")));
107
108 let p3 = Path::new("/a/x/y");
109 assert_eq!(common_ancestor(&[p1, p2, p3]), Some(PathBuf::from("/a")));
110
111 let p4 = Path::new("/b/c");
112 assert_eq!(common_ancestor(&[p1, p4]), Some(PathBuf::from("/")));
113
114 let p5 = Path::new("/a/b");
116 assert_eq!(common_ancestor(&[p1, p5]), Some(PathBuf::from("/a/b")));
117
118 let r1 = Path::new("a/b");
120 let r2 = Path::new("c/d");
121 assert_eq!(common_ancestor(&[r1, r2]), None);
122
123 let a1 = Path::new("/a/b");
125 let r3 = Path::new("a/b");
126 assert_eq!(common_ancestor(&[a1, r3]), None);
127 }
128}