Use HashMap instead of Vector<Storage>

This commit is contained in:
qwjyh 2023-12-04 21:34:24 +09:00
parent 1f0e43cd52
commit 7a4c1e5325
7 changed files with 404 additions and 234 deletions

View file

@ -1,7 +1,15 @@
//! Manipulate subdirectories of other storages, including directories.
use anyhow::{anyhow, Context, Result};
use serde::{Deserialize, Serialize};
use std::{path, fmt::{self, write}, rc::Rc};
use std::{
collections::HashMap,
fmt::{self, write},
path,
rc::Rc,
};
use crate::devices;
use super::{Storage, StorageExt};
@ -9,7 +17,7 @@ use super::{Storage, StorageExt};
#[derive(Serialize, Deserialize, Debug)]
pub struct Directory {
name: String,
parent: Box<Storage>,
parent: String,
relative_path: path::PathBuf,
notes: String,
}
@ -21,18 +29,48 @@ impl Directory {
/// - `notes`: supplimental notes.
pub fn new(
name: String,
parent: Rc<Storage>, // todo implement serialize
parent: String, // todo implement serialize
relative_path: path::PathBuf,
notes: String,
) -> Directory {
Directory {name, parent, relative_path, notes}
Directory {
name,
parent,
relative_path,
notes,
}
}
/// Get parent `&Storage` of directory.
fn parent<'a>(&'a self, storages: &'a HashMap<String, Storage>) -> Result<&Storage> {
let parent = &self.parent;
storages.get(&self.parent.clone()).context(format!(
"No parent {} exists for directory {}",
parent,
self.name()
))
}
// /// Resolve mount path of directory with current device.
// fn mount_path(
// &self,
// &device: &devices::Device,
// &storages: &HashMap<String, Storage>,
// ) -> Result<path::PathBuf> {
// let parent = self.parent(&storages)?;
// let parent_mount_path = parent.mount_path(&device, &storages)?;
// Ok(parent_mount_path.join(self.relative_path.clone()))
// }
}
impl StorageExt for Directory {
fn name(&self) -> &String {
&self.name
}
// fn mount_path(&self, &device: &devices::Device, &storages: &HashMap<String, Storage>) -> Result<&path::PathBuf> {
// Ok(&self.mount_path(&device, &storages)?)
// }
}
impl fmt::Display for Directory {
@ -46,4 +84,4 @@ impl fmt::Display for Directory {
notes = self.notes,
)
}
}
}