2023-09-01 10:37:30 +09:00
|
|
|
//! Manipulate subdirectories of other storages, including directories.
|
|
|
|
|
2023-12-04 21:34:24 +09:00
|
|
|
use anyhow::{anyhow, Context, Result};
|
2023-09-01 10:37:30 +09:00
|
|
|
use serde::{Deserialize, Serialize};
|
2023-12-04 21:34:24 +09:00
|
|
|
use std::{
|
|
|
|
collections::HashMap,
|
|
|
|
fmt::{self, write},
|
|
|
|
path,
|
|
|
|
rc::Rc,
|
|
|
|
};
|
|
|
|
|
|
|
|
use crate::devices;
|
2023-09-01 10:37:30 +09:00
|
|
|
|
|
|
|
use super::{Storage, StorageExt};
|
|
|
|
|
|
|
|
/// Subdirectory of other [Storage]s.
|
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
|
|
pub struct Directory {
|
|
|
|
name: String,
|
2023-12-04 21:34:24 +09:00
|
|
|
parent: String,
|
2023-09-01 10:37:30 +09:00
|
|
|
relative_path: path::PathBuf,
|
|
|
|
notes: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Directory {
|
|
|
|
/// - `name`: id
|
|
|
|
/// - `parent`: where the directory locates.
|
|
|
|
/// - `relative_path`: path from root of the parent storage.
|
|
|
|
/// - `notes`: supplimental notes.
|
|
|
|
pub fn new(
|
|
|
|
name: String,
|
2023-12-04 21:34:24 +09:00
|
|
|
parent: String, // todo implement serialize
|
2023-09-01 10:37:30 +09:00
|
|
|
relative_path: path::PathBuf,
|
|
|
|
notes: String,
|
|
|
|
) -> Directory {
|
2023-12-04 21:34:24 +09:00
|
|
|
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()
|
|
|
|
))
|
2023-09-01 10:37:30 +09:00
|
|
|
}
|
2023-12-04 21:34:24 +09:00
|
|
|
|
|
|
|
// /// 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()))
|
|
|
|
// }
|
2023-09-01 10:37:30 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
impl StorageExt for Directory {
|
|
|
|
fn name(&self) -> &String {
|
|
|
|
&self.name
|
|
|
|
}
|
2023-12-04 21:34:24 +09:00
|
|
|
|
|
|
|
// fn mount_path(&self, &device: &devices::Device, &storages: &HashMap<String, Storage>) -> Result<&path::PathBuf> {
|
|
|
|
// Ok(&self.mount_path(&device, &storages)?)
|
|
|
|
// }
|
2023-09-01 10:37:30 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Directory {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"S {name:<10} < {parent:<10}{relative_path:<10} : {notes}",
|
|
|
|
name = self.name(),
|
|
|
|
parent = self.parent,
|
|
|
|
relative_path = self.relative_path.display(),
|
|
|
|
notes = self.notes,
|
|
|
|
)
|
|
|
|
}
|
2023-12-04 21:34:24 +09:00
|
|
|
}
|