From e3675632c1039124ba4dae27bf3b71dc32125601 Mon Sep 17 00:00:00 2001 From: qwjyh Date: Tue, 12 Mar 2024 00:02:32 +0900 Subject: [PATCH] fixup! replace raw HashMap with Storages --- src/cmd_storage.rs | 47 +++++++++--------------- src/storages/physical_drive_partition.rs | 6 +-- 2 files changed, 21 insertions(+), 32 deletions(-) diff --git a/src/cmd_storage.rs b/src/cmd_storage.rs index 63e2139..2ce9e31 100644 --- a/src/cmd_storage.rs +++ b/src/cmd_storage.rs @@ -37,7 +37,7 @@ pub(crate) fn cmd_storage_add( trace!("found storages: {:?}", storages); let device = devices::get_device(&config_dir)?; - let (key, storage) = match storage_type { + let storage = match storage_type { StorageType::P => { let use_sysinfo = { let options = vec![ @@ -55,7 +55,7 @@ pub(crate) fn cmd_storage_add( _ => false, } }; - let (key, storage) = if use_sysinfo { + let storage = if use_sysinfo { // select storage physical_drive_partition::select_physical_storage(device, &storages)? } else { @@ -89,21 +89,18 @@ pub(crate) fn cmd_storage_add( .prompt()?, ); let local_info = local_info::LocalInfo::new("".to_string(), mount_path); - ( - name.clone(), - physical_drive_partition::PhysicalDrivePartition::new( - name, - kind, - capacity, - fs, - is_removable, - local_info, - &device, - ), + physical_drive_partition::PhysicalDrivePartition::new( + name, + kind, + capacity, + fs, + is_removable, + local_info, + &device, ) }; - println!("storage: {}: {:?}", key, storage); - (key, Storage::PhysicalStorage(storage)) + println!("storage: {}: {:?}", storage.name(), storage); + Storage::PhysicalStorage(storage) } StorageType::S => { if storages.list.is_empty() { @@ -128,13 +125,9 @@ pub(crate) fn cmd_storage_add( let key_name = ask_unique_name(&storages, "sub-directory".to_string())?; let notes = Text::new("Notes for this sub-directory:").prompt()?; let storage = directory::Directory::try_from_device_path( - key_name.clone(), - path, - notes, - &device, - &storages, + key_name, path, notes, &device, &storages, )?; - (key_name, Storage::SubDirectory(storage)) + Storage::SubDirectory(storage) } StorageType::O => { let path = path.unwrap_or_else(|| { @@ -167,18 +160,14 @@ pub(crate) fn cmd_storage_add( .prompt() .context("Failed to get provider")?; let storage = storages::online_storage::OnlineStorage::new( - name.clone(), - provider, - capacity, - alias, - path, - &device, + name, provider, capacity, alias, path, &device, ); - (name, Storage::Online(storage)) + Storage::Online(storage) } }; // add to storages + let new_storage_name = storage.name().clone(); storages.add(storage)?; trace!("updated storages: {:?}", storages); @@ -189,7 +178,7 @@ pub(crate) fn cmd_storage_add( add_and_commit( &repo, &Path::new(storages::STORAGESFILE), - &format!("Add new storage(physical drive): {}", key), + &format!("Add new storage(physical drive): {}", new_storage_name), )?; println!("Added new storage."); diff --git a/src/storages/physical_drive_partition.rs b/src/storages/physical_drive_partition.rs index b86cea2..51adc75 100644 --- a/src/storages/physical_drive_partition.rs +++ b/src/storages/physical_drive_partition.rs @@ -193,7 +193,7 @@ impl fmt::Display for PhysicalDrivePartition { pub fn select_physical_storage( device: Device, storages: &Storages, -) -> Result<(String, PhysicalDrivePartition)> { +) -> Result { trace!("select_physical_storage"); // get disk info fron sysinfo let sys_disks = @@ -217,8 +217,8 @@ pub fn select_physical_storage( println!("The name {} is already used.", disk_name); } trace!("selected name: {}", disk_name); - let storage = PhysicalDrivePartition::try_from_sysinfo_disk(&disk, disk_name.clone(), device)?; - Ok((disk_name, storage)) + let storage = PhysicalDrivePartition::try_from_sysinfo_disk(&disk, disk_name, device)?; + Ok(storage) } pub fn select_sysinfo_disk(sysinfo: &sysinfo::System) -> Result<&Disk> {