(WIP) implementing storage::directory

This commit is contained in:
qwjyh 2023-09-01 10:37:30 +09:00
parent 33e1a9aba7
commit 1f0e43cd52
4 changed files with 68 additions and 6 deletions

View file

@ -213,6 +213,9 @@ fn main() -> Result<()> {
println!("Added new storage.");
trace!("Finished adding storage");
}
StorageType::SubDirectory => {
todo!()
}
}
}
StorageCommands::List {} => {

View file

@ -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<Storage>` from devices.yml([DEVICESFILE]).

49
src/storages/directory.rs Normal file
View file

@ -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<Storage>,
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<Storage>, // 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,
)
}
}

View file

@ -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,