flashkraft_core/domain/
image_info.rs1use std::path::PathBuf;
7
8#[derive(Debug, Clone)]
10pub struct ImageInfo {
11 pub path: PathBuf,
13 pub name: String,
15 pub size_mb: f64,
17}
18
19impl ImageInfo {
20 pub fn from_path(path: PathBuf) -> Self {
30 let name = path
31 .file_name()
32 .and_then(|n| n.to_str())
33 .unwrap_or("Unknown")
34 .to_string();
35
36 let size_mb = path
37 .metadata()
38 .map(|m| m.len() as f64 / (1024.0 * 1024.0))
39 .unwrap_or(0.0);
40
41 Self {
42 path,
43 name,
44 size_mb,
45 }
46 }
47}
48
49#[cfg(test)]
50mod tests {
51 use super::*;
52 use std::fs::File;
53 use std::io::Write;
54
55 #[test]
56 fn test_image_info_from_path() {
57 let temp_dir = std::env::temp_dir();
59 let test_path = temp_dir.join("test_image.img");
60
61 if let Ok(mut file) = File::create(&test_path) {
63 let _ = file.write_all(&[0u8; 1024 * 1024]); }
65
66 let image_info = ImageInfo::from_path(test_path.clone());
67
68 assert_eq!(image_info.name, "test_image.img");
69 assert_eq!(image_info.path, test_path);
70 assert!((image_info.size_mb - 1.0).abs() < 0.1);
72
73 let _ = std::fs::remove_file(test_path);
75 }
76
77 #[test]
78 fn test_image_info_nonexistent_file() {
79 let path = PathBuf::from("/nonexistent/test.img");
80 let image_info = ImageInfo::from_path(path);
81
82 assert_eq!(image_info.name, "test.img");
83 assert_eq!(image_info.size_mb, 0.0);
84 }
85}