Skip to main content

flashkraft_core/domain/
image_info.rs

1//! Image Information Domain Model
2//!
3//! This module contains the ImageInfo struct which represents
4//! information about a disk image file.
5
6use std::path::PathBuf;
7
8/// Information about a disk image file
9#[derive(Debug, Clone)]
10pub struct ImageInfo {
11    /// Full path to the image file
12    pub path: PathBuf,
13    /// Display name of the file
14    pub name: String,
15    /// Size of the file in megabytes
16    pub size_mb: f64,
17}
18
19impl ImageInfo {
20    /// Create a new ImageInfo from a PathBuf
21    ///
22    /// # Arguments
23    ///
24    /// * `path` - The path to the image file
25    ///
26    /// # Returns
27    ///
28    /// An ImageInfo instance with extracted file name and size
29    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        // Create a temporary file for testing
58        let temp_dir = std::env::temp_dir();
59        let test_path = temp_dir.join("test_image.img");
60
61        // Write some data to get a known size
62        if let Ok(mut file) = File::create(&test_path) {
63            let _ = file.write_all(&[0u8; 1024 * 1024]); // 1 MB
64        }
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        // Size should be approximately 1 MB
71        assert!((image_info.size_mb - 1.0).abs() < 0.1);
72
73        // Cleanup
74        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}