change types for localinfos

This commit is contained in:
qwjyh 2024-05-21 14:45:20 +09:00
parent df0ce53d84
commit 87b73bd125
4 changed files with 21 additions and 21 deletions

View file

@ -1,5 +1,5 @@
use std::{ use std::{
collections::HashMap, collections::BTreeMap,
io::{self, stdout, Write}, io::{self, stdout, Write},
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
@ -103,14 +103,14 @@ pub fn cmd_backup_list(
storages: &Storages, storages: &Storages,
) -> Result<()> { ) -> Result<()> {
let devices = devices::get_devices(config_dir)?; let devices = devices::get_devices(config_dir)?;
let backups: HashMap<(String, String), Backup> = match device_name { let backups: BTreeMap<(String, String), Backup> = match device_name {
Some(device_name) => { Some(device_name) => {
let device = devices let device = devices
.iter() .iter()
.find(|dev| dev.name() == device_name) .find(|dev| dev.name() == device_name)
.context(format!("Device with name {} doesn't exist", device_name))?; .context(format!("Device with name {} doesn't exist", device_name))?;
let backups = Backups::read(config_dir, device)?; let backups = Backups::read(config_dir, device)?;
let mut allbackups = HashMap::new(); let mut allbackups = BTreeMap::new();
for (name, backup) in backups.list { for (name, backup) in backups.list {
if allbackups.insert((device.name(), name), backup).is_some() { if allbackups.insert((device.name(), name), backup).is_some() {
return Err(anyhow!("unexpected duplication in backups hashmap")); return Err(anyhow!("unexpected duplication in backups hashmap"));
@ -119,7 +119,7 @@ pub fn cmd_backup_list(
allbackups allbackups
} }
None => { None => {
let mut allbackups = HashMap::new(); let mut allbackups = BTreeMap::new();
for device in &devices { for device in &devices {
let backups = Backups::read(config_dir, device)?; let backups = Backups::read(config_dir, device)?;
for (name, backup) in backups.list { for (name, backup) in backups.list {
@ -132,7 +132,7 @@ pub fn cmd_backup_list(
} }
}; };
// source/destination filtering // source/destination filtering
let backups: HashMap<(String, String), Backup> = backups let backups: BTreeMap<(String, String), Backup> = backups
.into_iter() .into_iter()
.filter(|((_dev, _name), backup)| { .filter(|((_dev, _name), backup)| {
let src_matched = match &src_storage { let src_matched = match &src_storage {
@ -156,7 +156,7 @@ pub fn cmd_backup_list(
/// TODO: status printing /// TODO: status printing
fn write_backups_list( fn write_backups_list(
mut writer: impl io::Write, mut writer: impl io::Write,
backups: HashMap<(String, String), Backup>, backups: BTreeMap<(String, String), Backup>,
longprint: bool, longprint: bool,
storages: &Storages, storages: &Storages,
devices: &[Device], devices: &[Device],

View file

@ -2,7 +2,7 @@
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{collections::HashMap, fmt, path}; use std::{collections::BTreeMap, fmt, path};
use crate::devices; use crate::devices;
use crate::util; use crate::util;
@ -20,7 +20,7 @@ pub struct Directory {
relative_path: path::PathBuf, relative_path: path::PathBuf,
pub notes: String, pub notes: String,
/// Device and localinfo pairs. /// Device and localinfo pairs.
local_infos: HashMap<String, LocalInfo>, local_infos: BTreeMap<String, LocalInfo>,
} }
impl Directory { impl Directory {
@ -33,7 +33,7 @@ impl Directory {
parent: String, parent: String,
relative_path: path::PathBuf, relative_path: path::PathBuf,
notes: String, notes: String,
local_infos: HashMap<String, LocalInfo>, local_infos: BTreeMap<String, LocalInfo>,
) -> Directory { ) -> Directory {
Directory { Directory {
name, name,
@ -61,7 +61,7 @@ impl Directory {
parent.name().to_string(), parent.name().to_string(),
diff_path, diff_path,
notes, notes,
HashMap::from([(device.name(), local_info)]), BTreeMap::from([(device.name(), local_info)]),
)) ))
} }
@ -142,7 +142,7 @@ impl fmt::Display for Directory {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use std::{collections::HashMap, path::PathBuf}; use std::{collections::BTreeMap, path::PathBuf};
use crate::{ use crate::{
devices::Device, devices::Device,
@ -161,7 +161,7 @@ mod test {
let local_info_dir = let local_info_dir =
LocalInfo::new("dir_alias".to_string(), PathBuf::from("/mnt/sample/subdir")); LocalInfo::new("dir_alias".to_string(), PathBuf::from("/mnt/sample/subdir"));
let device = Device::new("test_device".to_string()); let device = Device::new("test_device".to_string());
let mut local_infos = HashMap::new(); let mut local_infos = BTreeMap::new();
local_infos.insert(device.name(), local_info_dir); local_infos.insert(device.name(), local_info_dir);
let physical = PhysicalDrivePartition::new( let physical = PhysicalDrivePartition::new(
"parent".to_string(), "parent".to_string(),

View file

@ -3,7 +3,7 @@
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use byte_unit::Byte; use byte_unit::Byte;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::collections::HashMap; use std::collections::BTreeMap;
use std::fmt; use std::fmt;
use std::path; use std::path;
@ -23,7 +23,7 @@ pub struct OnlineStorage {
/// Capacity in bytes. /// Capacity in bytes.
capacity: u64, capacity: u64,
/// Device and local info pairs. /// Device and local info pairs.
local_infos: HashMap<String, LocalInfo>, local_infos: BTreeMap<String, LocalInfo>,
} }
impl OnlineStorage { impl OnlineStorage {
@ -42,7 +42,7 @@ impl OnlineStorage {
name, name,
provider, provider,
capacity, capacity,
local_infos: HashMap::from([(device.name(), local_info)]), local_infos: BTreeMap::from([(device.name(), local_info)]),
} }
} }
} }

View file

@ -7,7 +7,7 @@ use anyhow::{anyhow, Context, Result};
use byte_unit::Byte; use byte_unit::Byte;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::path::{self, Path}; use std::path::{self, Path};
use std::{collections::HashMap, fmt}; use std::{collections::BTreeMap, fmt};
use sysinfo::{Disk, DiskExt, SystemExt}; use sysinfo::{Disk, DiskExt, SystemExt};
use super::local_info::{self, LocalInfo}; use super::local_info::{self, LocalInfo};
@ -20,8 +20,8 @@ pub struct PhysicalDrivePartition {
capacity: u64, capacity: u64,
fs: String, fs: String,
is_removable: bool, is_removable: bool,
// system_names: HashMap<String, String>, // system_names: BTreeMap<String, String>,
local_infos: HashMap<String, LocalInfo>, local_infos: BTreeMap<String, LocalInfo>,
} }
impl PhysicalDrivePartition { impl PhysicalDrivePartition {
@ -40,7 +40,7 @@ impl PhysicalDrivePartition {
capacity, capacity,
fs, fs,
is_removable, is_removable,
local_infos: HashMap::from([(device.name(), local_info)]), local_infos: BTreeMap::from([(device.name(), local_info)]),
} }
} }
@ -65,8 +65,8 @@ impl PhysicalDrivePartition {
capacity: disk.total_space(), capacity: disk.total_space(),
fs: fs.to_string(), fs: fs.to_string(),
is_removable: disk.is_removable(), is_removable: disk.is_removable(),
// system_names: HashMap::from([(device.name(), alias)]), // system_names: BTreeMap::from([(device.name(), alias)]),
local_infos: HashMap::from([(device.name(), local_info)]), local_infos: BTreeMap::from([(device.name(), local_info)]),
}) })
} }