1use serde::Serialize;
2use std::path::PathBuf;
3use std::time::SystemTime;
4
5#[derive(Debug, Clone, Serialize)]
8pub struct DirEntry {
9 pub path: PathBuf,
11
12 pub name: String,
14
15 pub is_dir: bool,
17
18 pub size: u64,
20
21 pub disk_usage: u64,
23
24 pub file_count: u64,
26
27 pub dir_count: u64,
29
30 pub mtime: Option<SystemTime>,
32
33 pub children: Vec<DirEntry>,
35
36 pub error: Option<String>,
38}
39
40impl DirEntry {
41 pub fn shallow_clone(&self) -> Self {
43 Self {
44 path: self.path.clone(),
45 name: self.name.clone(),
46 is_dir: self.is_dir,
47 size: self.size,
48 disk_usage: self.disk_usage,
49 file_count: self.file_count,
50 dir_count: self.dir_count,
51 mtime: self.mtime,
52 children: Vec::new(),
53 error: self.error.clone(),
54 }
55 }
56
57 pub fn new_file(path: PathBuf, size: u64, disk_usage: u64, mtime: Option<SystemTime>) -> Self {
59 let name = path
60 .file_name()
61 .map(|n| n.to_string_lossy().to_string())
62 .unwrap_or_default();
63
64 Self {
65 path,
66 name,
67 is_dir: false,
68 size,
69 disk_usage,
70 file_count: 1,
71 dir_count: 0,
72 mtime,
73 children: vec![],
74 error: None,
75 }
76 }
77
78 pub fn new_dir(path: PathBuf, mtime: Option<SystemTime>) -> Self {
80 let name = path
81 .file_name()
82 .map(|n| n.to_string_lossy().to_string())
83 .unwrap_or_else(|| path.to_string_lossy().to_string());
84
85 Self {
86 path,
87 name,
88 is_dir: true,
89 size: 0,
90 disk_usage: 0,
91 file_count: 0,
92 dir_count: 0,
93 mtime,
94 children: vec![],
95 error: None,
96 }
97 }
98
99 pub fn new_error(path: PathBuf, error: String) -> Self {
101 let name = path
102 .file_name()
103 .map(|n| n.to_string_lossy().to_string())
104 .unwrap_or_default();
105
106 Self {
107 path,
108 name,
109 is_dir: false,
110 size: 0,
111 disk_usage: 0,
112 file_count: 0,
113 dir_count: 0,
114 mtime: None,
115 children: vec![],
116 error: Some(error),
117 }
118 }
119
120 pub fn recalculate_totals(&mut self) {
122 if !self.is_dir {
123 return;
124 }
125
126 self.size = 0;
127 self.disk_usage = 0;
128 self.file_count = 0;
129 self.dir_count = 0;
130
131 for child in &self.children {
132 self.size += child.size;
133 self.disk_usage += child.disk_usage;
134 self.file_count += child.file_count;
135 if child.is_dir {
136 self.dir_count += 1 + child.dir_count;
137 }
138 }
139 }
140
141 pub fn sort_by_size(&mut self) {
143 self.children
144 .sort_by_key(|entry| std::cmp::Reverse(entry.size));
145 for child in &mut self.children {
146 child.sort_by_size();
147 }
148 }
149
150 pub fn sort_by_name(&mut self) {
152 self.children.sort_by(|a, b| a.name.cmp(&b.name));
153 for child in &mut self.children {
154 child.sort_by_name();
155 }
156 }
157
158 pub fn total_entries(&self) -> u64 {
160 self.file_count + self.dir_count + if self.is_dir { 1 } else { 0 }
161 }
162}
163
164#[cfg(test)]
165mod tests {
166 use super::*;
167
168 #[test]
169 fn test_dir_entry_creation() {
170 let entry = DirEntry {
171 path: PathBuf::from("/test"),
172 name: "test".to_string(),
173 is_dir: true,
174 size: 1024,
175 disk_usage: 4096,
176 file_count: 5,
177 dir_count: 2,
178 mtime: None,
179 children: vec![],
180 error: None,
181 };
182 assert!(entry.is_dir);
183 assert_eq!(entry.size, 1024);
184 assert_eq!(entry.name, "test");
185 assert_eq!(entry.file_count, 5);
186 assert_eq!(entry.dir_count, 2);
187 }
188
189 #[test]
190 fn test_dir_entry_with_error() {
191 let entry = DirEntry {
192 path: PathBuf::from("/forbidden"),
193 name: "forbidden".to_string(),
194 is_dir: true,
195 size: 0,
196 disk_usage: 0,
197 file_count: 0,
198 dir_count: 0,
199 mtime: None,
200 children: vec![],
201 error: Some("Permission denied".to_string()),
202 };
203 assert!(entry.error.is_some());
204 assert_eq!(entry.error.as_ref().unwrap(), "Permission denied");
205 }
206
207 #[test]
208 fn test_new_file() {
209 let entry = DirEntry::new_file(PathBuf::from("/parent/file.txt"), 1024, 4096, None);
210 assert!(!entry.is_dir);
211 assert_eq!(entry.name, "file.txt");
212 assert_eq!(entry.size, 1024);
213 assert_eq!(entry.disk_usage, 4096);
214 assert_eq!(entry.file_count, 1);
215 assert_eq!(entry.dir_count, 0);
216 }
217
218 #[test]
219 fn test_new_dir() {
220 let entry = DirEntry::new_dir(PathBuf::from("/parent/subdir"), None);
221 assert!(entry.is_dir);
222 assert_eq!(entry.name, "subdir");
223 assert_eq!(entry.size, 0);
224 assert_eq!(entry.file_count, 0);
225 }
226
227 #[test]
228 fn test_new_dir_root_path() {
229 let entry = DirEntry::new_dir(PathBuf::from("/"), None);
230 assert!(entry.is_dir);
231 assert_eq!(entry.name, "/");
232 }
233
234 #[test]
235 fn test_new_error() {
236 let entry = DirEntry::new_error(
237 PathBuf::from("/forbidden/file"),
238 "Permission denied".to_string(),
239 );
240 assert!(entry.error.is_some());
241 assert_eq!(entry.name, "file");
242 assert_eq!(entry.size, 0);
243 }
244
245 #[test]
246 fn test_recalculate_totals() {
247 let mut parent = DirEntry::new_dir(PathBuf::from("/parent"), None);
248 parent.children.push(DirEntry::new_file(
249 PathBuf::from("/parent/file1.txt"),
250 100,
251 4096,
252 None,
253 ));
254 parent.children.push(DirEntry::new_file(
255 PathBuf::from("/parent/file2.txt"),
256 200,
257 4096,
258 None,
259 ));
260
261 parent.recalculate_totals();
262
263 assert_eq!(parent.size, 300);
264 assert_eq!(parent.disk_usage, 8192);
265 assert_eq!(parent.file_count, 2);
266 }
267
268 #[test]
269 fn test_recalculate_totals_with_subdirs() {
270 let mut parent = DirEntry::new_dir(PathBuf::from("/parent"), None);
271
272 let mut subdir = DirEntry::new_dir(PathBuf::from("/parent/subdir"), None);
273 subdir.children.push(DirEntry::new_file(
274 PathBuf::from("/parent/subdir/nested.txt"),
275 500,
276 4096,
277 None,
278 ));
279 subdir.recalculate_totals();
280
281 parent.children.push(subdir);
282 parent.children.push(DirEntry::new_file(
283 PathBuf::from("/parent/file.txt"),
284 100,
285 4096,
286 None,
287 ));
288 parent.recalculate_totals();
289
290 assert_eq!(parent.size, 600);
291 assert_eq!(parent.file_count, 2);
292 assert_eq!(parent.dir_count, 1);
293 }
294
295 #[test]
296 fn test_sort_by_size() {
297 let mut parent = DirEntry::new_dir(PathBuf::from("/parent"), None);
298 parent.children.push(DirEntry::new_file(
299 PathBuf::from("/parent/small.txt"),
300 100,
301 4096,
302 None,
303 ));
304 parent.children.push(DirEntry::new_file(
305 PathBuf::from("/parent/large.txt"),
306 1000,
307 4096,
308 None,
309 ));
310 parent.children.push(DirEntry::new_file(
311 PathBuf::from("/parent/medium.txt"),
312 500,
313 4096,
314 None,
315 ));
316
317 parent.sort_by_size();
318
319 assert_eq!(parent.children[0].name, "large.txt");
320 assert_eq!(parent.children[1].name, "medium.txt");
321 assert_eq!(parent.children[2].name, "small.txt");
322 }
323
324 #[test]
325 fn test_sort_by_name() {
326 let mut parent = DirEntry::new_dir(PathBuf::from("/parent"), None);
327 parent.children.push(DirEntry::new_file(
328 PathBuf::from("/parent/charlie.txt"),
329 100,
330 4096,
331 None,
332 ));
333 parent.children.push(DirEntry::new_file(
334 PathBuf::from("/parent/alpha.txt"),
335 100,
336 4096,
337 None,
338 ));
339 parent.children.push(DirEntry::new_file(
340 PathBuf::from("/parent/bravo.txt"),
341 100,
342 4096,
343 None,
344 ));
345
346 parent.sort_by_name();
347
348 assert_eq!(parent.children[0].name, "alpha.txt");
349 assert_eq!(parent.children[1].name, "bravo.txt");
350 assert_eq!(parent.children[2].name, "charlie.txt");
351 }
352
353 #[test]
354 fn test_total_entries() {
355 let mut parent = DirEntry::new_dir(PathBuf::from("/parent"), None);
356 parent.file_count = 5;
357 parent.dir_count = 2;
358
359 assert_eq!(parent.total_entries(), 8); }
361
362 #[test]
363 fn test_total_entries_file() {
364 let file = DirEntry::new_file(PathBuf::from("/file.txt"), 100, 4096, None);
365 assert_eq!(file.total_entries(), 1); }
367}