|
| 1 | +//! MTP type definitions for frontend communication. |
| 2 | +//! |
| 3 | +//! These types are serialized to JSON for Tauri commands. |
| 4 | +
|
| 5 | +// Phase 1 foundation: some types not yet used, will be used in subsequent phases |
| 6 | +#![allow(dead_code, reason = "Phase 1 foundation: types used in later phases")] |
| 7 | + |
| 8 | +use serde::{Deserialize, Serialize}; |
| 9 | + |
| 10 | +/// Information about a connected MTP device. |
| 11 | +/// |
| 12 | +/// This represents a device detected via USB, before opening an MTP session. |
| 13 | +/// Used by the frontend to display available devices in the volume picker. |
| 14 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 15 | +#[serde(rename_all = "camelCase")] |
| 16 | +pub struct MtpDeviceInfo { |
| 17 | + /// Unique identifier for the device (based on USB bus/address). |
| 18 | + pub id: String, |
| 19 | + /// USB vendor ID (e.g., 0x18d1 for Google). |
| 20 | + pub vendor_id: u16, |
| 21 | + /// USB product ID. |
| 22 | + pub product_id: u16, |
| 23 | + /// Device manufacturer name, if available from USB descriptor. |
| 24 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 25 | + pub manufacturer: Option<String>, |
| 26 | + /// Device product name, if available from USB descriptor. |
| 27 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 28 | + pub product: Option<String>, |
| 29 | + /// USB serial number, if available. |
| 30 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 31 | + pub serial_number: Option<String>, |
| 32 | +} |
| 33 | + |
| 34 | +impl MtpDeviceInfo { |
| 35 | + /// Returns a display name for the device. |
| 36 | + /// |
| 37 | + /// Prefers product name, falls back to "MTP Device (vendor:product)". |
| 38 | + pub fn display_name(&self) -> String { |
| 39 | + if let Some(product) = &self.product { |
| 40 | + return product.clone(); |
| 41 | + } |
| 42 | + if let Some(manufacturer) = &self.manufacturer { |
| 43 | + return format!("{} device", manufacturer); |
| 44 | + } |
| 45 | + format!("MTP device ({:04x}:{:04x})", self.vendor_id, self.product_id) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +/// Information about a storage area on an MTP device. |
| 50 | +/// |
| 51 | +/// Android devices typically have one or more storages: "Internal Storage", "SD Card", etc. |
| 52 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 53 | +#[serde(rename_all = "camelCase")] |
| 54 | +pub struct MtpStorageInfo { |
| 55 | + /// Storage ID (MTP storage handle). |
| 56 | + pub id: u32, |
| 57 | + /// Display name (e.g., "Internal shared storage"). |
| 58 | + pub name: String, |
| 59 | + /// Total capacity in bytes. |
| 60 | + pub total_bytes: u64, |
| 61 | + /// Available space in bytes. |
| 62 | + pub available_bytes: u64, |
| 63 | + /// Storage type description (e.g., "FixedROM", "RemovableRAM"). |
| 64 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 65 | + pub storage_type: Option<String>, |
| 66 | +} |
| 67 | + |
| 68 | +#[cfg(test)] |
| 69 | +mod tests { |
| 70 | + use super::*; |
| 71 | + |
| 72 | + #[test] |
| 73 | + fn test_device_display_name_with_product() { |
| 74 | + let device = MtpDeviceInfo { |
| 75 | + id: "usb-1-2".to_string(), |
| 76 | + vendor_id: 0x18d1, |
| 77 | + product_id: 0x4ee1, |
| 78 | + manufacturer: Some("Google".to_string()), |
| 79 | + product: Some("Pixel 8".to_string()), |
| 80 | + serial_number: None, |
| 81 | + }; |
| 82 | + assert_eq!(device.display_name(), "Pixel 8"); |
| 83 | + } |
| 84 | + |
| 85 | + #[test] |
| 86 | + fn test_device_display_name_with_manufacturer() { |
| 87 | + let device = MtpDeviceInfo { |
| 88 | + id: "usb-1-3".to_string(), |
| 89 | + vendor_id: 0x04e8, |
| 90 | + product_id: 0x6860, |
| 91 | + manufacturer: Some("Samsung".to_string()), |
| 92 | + product: None, |
| 93 | + serial_number: None, |
| 94 | + }; |
| 95 | + assert_eq!(device.display_name(), "Samsung device"); |
| 96 | + } |
| 97 | + |
| 98 | + #[test] |
| 99 | + fn test_device_display_name_fallback() { |
| 100 | + let device = MtpDeviceInfo { |
| 101 | + id: "usb-1-4".to_string(), |
| 102 | + vendor_id: 0x1234, |
| 103 | + product_id: 0x5678, |
| 104 | + manufacturer: None, |
| 105 | + product: None, |
| 106 | + serial_number: None, |
| 107 | + }; |
| 108 | + assert_eq!(device.display_name(), "MTP device (1234:5678)"); |
| 109 | + } |
| 110 | + |
| 111 | + #[test] |
| 112 | + fn test_device_serialization() { |
| 113 | + let device = MtpDeviceInfo { |
| 114 | + id: "test".to_string(), |
| 115 | + vendor_id: 0x18d1, |
| 116 | + product_id: 0x4ee1, |
| 117 | + manufacturer: Some("Google".to_string()), |
| 118 | + product: Some("Pixel".to_string()), |
| 119 | + serial_number: None, |
| 120 | + }; |
| 121 | + let json = serde_json::to_string(&device).unwrap(); |
| 122 | + assert!(json.contains("\"vendorId\":")); |
| 123 | + assert!(json.contains("\"productId\":")); |
| 124 | + // serialNumber should be omitted when None |
| 125 | + assert!(!json.contains("serialNumber")); |
| 126 | + } |
| 127 | + |
| 128 | + #[test] |
| 129 | + fn test_storage_serialization() { |
| 130 | + let storage = MtpStorageInfo { |
| 131 | + id: 0x10001, |
| 132 | + name: "Internal Storage".to_string(), |
| 133 | + total_bytes: 128_000_000_000, |
| 134 | + available_bytes: 64_000_000_000, |
| 135 | + storage_type: Some("FixedRAM".to_string()), |
| 136 | + }; |
| 137 | + let json = serde_json::to_string(&storage).unwrap(); |
| 138 | + assert!(json.contains("\"totalBytes\":128000000000")); |
| 139 | + assert!(json.contains("\"availableBytes\":64000000000")); |
| 140 | + } |
| 141 | +} |
0 commit comments