From 1f0e43cd5294e19f9223998880a6d968a44d1a1d Mon Sep 17 00:00:00 2001 From: qwjyh Date: Fri, 1 Sep 2023 10:37:30 +0900 Subject: [PATCH] (WIP) implementing storage::directory --- src/main.rs | 3 ++ src/storages.rs | 20 +++++++--- src/storages/directory.rs | 49 ++++++++++++++++++++++++ src/storages/physical_drive_partition.rs | 2 +- 4 files changed, 68 insertions(+), 6 deletions(-) create mode 100644 src/storages/directory.rs diff --git a/src/main.rs b/src/main.rs index 3c4b4cd..dc7a125 100644 --- a/src/main.rs +++ b/src/main.rs @@ -213,6 +213,9 @@ fn main() -> Result<()> { println!("Added new storage."); trace!("Finished adding storage"); } + StorageType::SubDirectory => { + todo!() + } } } StorageCommands::List {} => { diff --git a/src/storages.rs b/src/storages.rs index f1ae006..457924c 100644 --- a/src/storages.rs +++ b/src/storages.rs @@ -1,10 +1,11 @@ //! Manipulates storages. +use crate::storages::directory::Directory; +use crate::storages::physical_drive_partition::PhysicalDrivePartition; use anyhow::{anyhow, Context, Result}; use clap::ValueEnum; -use physical_drive_partition::PhysicalDrivePartition; use serde::{Deserialize, Serialize}; -use std::{ffi, fmt, fs, path::Path, io}; +use std::{ffi, fmt, fs, io, path::Path}; /// YAML file to store known storages.. pub const STORAGESFILE: &str = "storages.yml"; @@ -12,6 +13,7 @@ pub const STORAGESFILE: &str = "storages.yml"; #[derive(ValueEnum, Clone, Copy, Debug)] pub enum StorageType { Physical, + SubDirectory, // Online, } @@ -19,6 +21,7 @@ pub enum StorageType { #[derive(Serialize, Deserialize, Debug)] pub enum Storage { PhysicalStorage(PhysicalDrivePartition), + SubDirectory(Directory), // /// Online storage provided by others. // OnlineStorage { // name: String, @@ -28,9 +31,14 @@ pub enum Storage { } impl Storage { - pub fn add_alias(&mut self, disk: &sysinfo::Disk, config_dir: &std::path::PathBuf) -> anyhow::Result<()> { + pub fn add_alias( + &mut self, + disk: &sysinfo::Disk, + config_dir: &std::path::PathBuf, + ) -> anyhow::Result<()> { match self { Self::PhysicalStorage(s) => s.add_alias(disk, config_dir), + Self::SubDirectory(_) => Err(anyhow!("SubDirectory doesn't have system alias.")), } } } @@ -39,15 +47,16 @@ impl StorageExt for Storage { fn name(&self) -> &String { match self { Self::PhysicalStorage(s) => s.name(), + Self::SubDirectory(s) => s.name(), } } - } impl fmt::Display for Storage { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Self::PhysicalStorage(s) => s.fmt(f), + Self::SubDirectory(s) => s.fmt(f), } } } @@ -57,6 +66,7 @@ pub trait StorageExt { fn name(&self) -> &String; } +pub mod directory; pub mod physical_drive_partition; /// Get `Vec` from devices.yml([DEVICESFILE]). @@ -91,4 +101,4 @@ pub fn write_storages(config_dir: &Path, storages: Vec) -> Result<()> { let f = fs::File::create(config_dir.join(STORAGESFILE))?; let writer = io::BufWriter::new(f); serde_yaml::to_writer(writer, &storages).map_err(|e| anyhow!(e)) -} \ No newline at end of file +} diff --git a/src/storages/directory.rs b/src/storages/directory.rs new file mode 100644 index 0000000..f50cf1c --- /dev/null +++ b/src/storages/directory.rs @@ -0,0 +1,49 @@ +//! Manipulate subdirectories of other storages, including directories. + +use serde::{Deserialize, Serialize}; +use std::{path, fmt::{self, write}, rc::Rc}; + +use super::{Storage, StorageExt}; + +/// Subdirectory of other [Storage]s. +#[derive(Serialize, Deserialize, Debug)] +pub struct Directory { + name: String, + parent: Box, + 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, + parent: Rc, // todo implement serialize + relative_path: path::PathBuf, + notes: String, + ) -> Directory { + Directory {name, parent, relative_path, notes} + } +} + +impl StorageExt for Directory { + fn name(&self) -> &String { + &self.name + } +} + +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, + ) + } +} \ No newline at end of file diff --git a/src/storages/physical_drive_partition.rs b/src/storages/physical_drive_partition.rs index 3f3a2b2..0b5d934 100644 --- a/src/storages/physical_drive_partition.rs +++ b/src/storages/physical_drive_partition.rs @@ -80,7 +80,7 @@ impl fmt::Display for PhysicalDrivePartition { let removable_indicator = if self.is_removable { "+" } else { "-" }; write!( f, - "{name:<10} {size} {removable:<1} {kind:<6} {fs:<5}", + "P {name:<10} {size} {removable:<1} {kind:<6} {fs:<5}", name = self.name(), size = Byte::from_bytes(self.capacity.into()).get_appropriate_unit(true), removable = removable_indicator,