From 5ea5730646ef3ebc1ee5451a5ca60554568e1a86 Mon Sep 17 00:00:00 2001 From: Benjamin Nguyen Date: Fri, 10 Mar 2023 18:27:49 -0800 Subject: [PATCH] more restructuring --- src/fs/error.rs | 2 +- src/fs/mod.rs | 3 --- src/main.rs | 9 +++---- src/{ => render}/context/config.rs | 6 +---- src/{ => render}/context/mod.rs | 43 +++++------------------------- src/{fs => render}/disk_usage.rs | 14 ++-------- src/render/mod.rs | 8 ++++++ src/render/node.rs | 6 ++--- src/render/order.rs | 30 +++++++-------------- src/render/tree/mod.rs | 27 ++++++++++--------- 10 files changed, 48 insertions(+), 100 deletions(-) rename src/{ => render}/context/config.rs (91%) rename src/{ => render}/context/mod.rs (84%) rename src/{fs => render}/disk_usage.rs (90%) diff --git a/src/fs/error.rs b/src/fs/error.rs index 9f280ee2..91dcd100 100644 --- a/src/fs/error.rs +++ b/src/fs/error.rs @@ -1,4 +1,4 @@ -use crate::context; +use crate::render::context; use std::{ convert::From, error::Error as StdError, diff --git a/src/fs/mod.rs b/src/fs/mod.rs index e36ca5f5..dca65dca 100644 --- a/src/fs/mod.rs +++ b/src/fs/mod.rs @@ -1,6 +1,3 @@ -/// Operations that decide how to present info about disk usage. -pub mod disk_usage; - /// Errors related to filesystem traversal. pub mod error; diff --git a/src/main.rs b/src/main.rs index 0d3a6d84..45468ee4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,10 @@ use std::process::ExitCode; use clap::{CommandFactory, FromArgMatches}; -use context::Context; -use render::tree::{self, Tree}; - -/// CLI rules and definitions and context wherein [Tree] will operate. -mod context; +use render::{ + context::Context, + tree::{self, Tree}, +}; /// Filesystem operations. mod fs; diff --git a/src/context/config.rs b/src/render/context/config.rs similarity index 91% rename from src/context/config.rs rename to src/render/context/config.rs index 4bc58220..f78b77af 100644 --- a/src/context/config.rs +++ b/src/render/context/config.rs @@ -1,8 +1,4 @@ -use std::{ - env, - fs, - path::PathBuf, -}; +use std::{env, fs, path::PathBuf}; pub const CONFIG_ENV_VAR: &str = "ERDTREE_CONFIG_PATH"; pub const CONFIG_NAME: &str = ".erdtreerc"; diff --git a/src/context/mod.rs b/src/render/context/mod.rs similarity index 84% rename from src/context/mod.rs rename to src/render/context/mod.rs index 13ed28ef..f85a4d81 100644 --- a/src/context/mod.rs +++ b/src/render/context/mod.rs @@ -1,4 +1,5 @@ -use clap::{FromArgMatches, Parser, ValueEnum}; +use super::{disk_usage::DiskUsage, order::SortType}; +use clap::Parser; use ignore::{ overrides::{Override, OverrideBuilder}, WalkBuilder, WalkParallel, @@ -27,7 +28,7 @@ pub struct Context { /// Print physical or logical file size #[arg(short, long, value_enum, default_value_t = DiskUsage::Logical)] - disk_usage: DiskUsage, + pub disk_usage: DiskUsage, /// Include or exclude files using glob patterns #[arg(short, long)] @@ -66,8 +67,8 @@ pub struct Context { pub scale: usize, /// Sort-order to display directory content - #[arg(short, long, value_enum, default_value_t = Order::None)] - sort: Order, + #[arg(short, long, value_enum)] + sort: Option, /// Always sorts directories above files #[arg(long)] @@ -86,33 +87,6 @@ pub struct Context { pub suppress_size: bool, } -/// Order in which to print nodes. -#[derive(Copy, Clone, Debug, ValueEnum)] -pub enum Order { - /// Sort entries by file name - Name, - - /// Sort entries by size smallest to largest, top to bottom - Size, - - /// Sort entries by size largest to smallest, bottom to top - SizeRev, - - /// No sorting - None, -} - -/// Display disk usage output as either logical size or physical size. -#[derive(Copy, Clone, Debug, ValueEnum)] -pub enum DiskUsage { - /// How many bytes does a file contain - Logical, - - /// How much actual space on disk based on blocks allocated, taking into account sparse files - /// and compression. - Physical, -} - impl Context { /// Returns reference to the path of the root directory to be traversed. pub fn dir(&self) -> &Path { @@ -122,7 +96,7 @@ impl Context { } /// The sort-order used for printing. - pub fn sort(&self) -> Order { + pub fn sort(&self) -> Option { self.sort } @@ -131,11 +105,6 @@ impl Context { self.dirs_first } - /// Getter for `disk_usage` field. - pub fn disk_usage(&self) -> &DiskUsage { - &self.disk_usage - } - /// The max depth to print. Note that all directories are fully traversed to compute file /// sizes; this just determines how much to print. pub fn level(&self) -> Option { diff --git a/src/fs/disk_usage.rs b/src/render/disk_usage.rs similarity index 90% rename from src/fs/disk_usage.rs rename to src/render/disk_usage.rs index 3190ce78..1f26ccb5 100644 --- a/src/fs/disk_usage.rs +++ b/src/render/disk_usage.rs @@ -1,8 +1,7 @@ -use crate::context; use ansi_term::Color; +use clap::ValueEnum; use filesize::PathExt; use std::{ - convert::From, fmt::{self, Display}, fs::Metadata, ops::AddAssign, @@ -10,7 +9,7 @@ use std::{ }; /// Determines between logical or physical size for display -#[derive(Clone, Debug)] +#[derive(Copy, Clone, Debug, ValueEnum)] pub enum DiskUsage { /// How many bytes does a file contain Logical, @@ -101,15 +100,6 @@ impl Display for FileSize { } } -impl From<&context::DiskUsage> for DiskUsage { - fn from(du: &context::DiskUsage) -> Self { - match du { - context::DiskUsage::Logical => Self::Logical, - context::DiskUsage::Physical => Self::Physical, - } - } -} - impl Display for Prefix { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { diff --git a/src/render/mod.rs b/src/render/mod.rs index 7c398e26..03bf4bec 100644 --- a/src/render/mod.rs +++ b/src/render/mod.rs @@ -1,3 +1,11 @@ +/// CLI rules and definitions and context wherein [Tree] will operate. +/// +/// [`Tree`]: tree::Tree +pub mod context; + +/// Operations that decide how to present info about disk usage. +pub mod disk_usage; + /// Contains components of the [`Tree`] data structure that derive from [`DirEntry`]. /// /// [`Tree`]: tree::Tree diff --git a/src/render/node.rs b/src/render/node.rs index 6ca6751b..516b9cc4 100644 --- a/src/render/node.rs +++ b/src/render/node.rs @@ -1,10 +1,8 @@ use super::get_ls_colors; use crate::{ + fs::inode::Inode, icons::{self, icon_from_ext, icon_from_file_name, icon_from_file_type}, - fs::{ - disk_usage::{DiskUsage, FileSize}, - inode::Inode, - }, + render::disk_usage::{DiskUsage, FileSize}, }; use ansi_term::Color; use ansi_term::Style; diff --git a/src/render/order.rs b/src/render/order.rs index 9c423cbf..f84fa39c 100644 --- a/src/render/order.rs +++ b/src/render/order.rs @@ -1,14 +1,18 @@ use super::node::Node; -use crate::context; +use clap::ValueEnum; use std::{cmp::Ordering, convert::From}; /// Order in which to print nodes. -#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] +#[derive(Copy, Clone, Debug, ValueEnum, PartialEq, Eq, PartialOrd, Ord)] pub enum SortType { + /// Sort entries by file name Name, + + /// Sort entries by size smallest to largest, top to bottom Size, + + /// Sort entries by size largest to smallest, bottom to top SizeRev, - None, } #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] @@ -74,22 +78,8 @@ impl SortType { } } -impl From<(context::Order, bool)> for Order { - fn from((order, dir_first): (context::Order, bool)) -> Self { - Order { - sort: order.into(), - dir_first, - } - } -} - -impl From for SortType { - fn from(ord: context::Order) -> Self { - match ord { - context::Order::Name => SortType::Name, - context::Order::Size => SortType::Size, - context::Order::SizeRev => SortType::SizeRev, - context::Order::None => SortType::None, - } +impl From<(SortType, bool)> for Order { + fn from((sort, dir_first): (SortType, bool)) -> Self { + Order { sort, dir_first } } } diff --git a/src/render/tree/mod.rs b/src/render/tree/mod.rs index 95ce3708..41a8f2c1 100644 --- a/src/render/tree/mod.rs +++ b/src/render/tree/mod.rs @@ -3,10 +3,10 @@ use super::{ order::Order, }; use crate::{ - context::Context, - fs::{ + fs::error::Error, + render::{ + context::Context, disk_usage::{DiskUsage, FileSize}, - error::Error, }, }; use crossbeam::channel::{self, Sender}; @@ -33,7 +33,7 @@ pub struct Tree { icons: bool, pub level: Option, #[allow(dead_code)] - order: Order, + order: Option, root: Node, #[allow(dead_code)] scale: usize, @@ -49,14 +49,14 @@ impl Tree { /// Initializes a [Tree]. pub fn new( walker: WalkParallel, - order: Order, + order: Option, level: Option, icons: bool, disk_usage: DiskUsage, scale: usize, suppress_size: bool, ) -> TreeResult { - let root = Self::traverse(walker, &order, icons, &disk_usage, scale, suppress_size)?; + let root = Self::traverse(walker, order, icons, &disk_usage, scale, suppress_size)?; Ok(Self { disk_usage, @@ -81,7 +81,7 @@ impl Tree { /// outside of the parallel traversal step please report an issue. fn traverse( walker: WalkParallel, - order: &Order, + order: Option, icons: bool, disk_usage: &DiskUsage, scale: usize, @@ -162,7 +162,7 @@ impl Tree { fn assemble_tree( current_dir: &mut Node, branches: &mut Branches, - order: &Order, + order: Option, disk_usage: &DiskUsage, scale: usize, ) { @@ -192,9 +192,10 @@ impl Tree { current_node.set_file_size(dir_size) } - order - .comparator() - .map(|func| current_node.children_mut().map(|nodes| nodes.sort_by(func))); + if let Some(ord) = order { + ord.comparator() + .map(|func| current_node.children_mut().map(|nodes| nodes.sort_by(func))); + } } } @@ -203,8 +204,8 @@ impl TryFrom for Tree { fn try_from(clargs: Context) -> Result { let walker = WalkParallel::try_from(&clargs)?; - let order = Order::from((clargs.sort(), clargs.dirs_first())); - let du = DiskUsage::from(clargs.disk_usage()); + let order = clargs.sort().map(|s| Order::from((s, clargs.dirs_first()))); + let du = clargs.disk_usage; let scale = clargs.scale; let suppress_size = clargs.suppress_size; let tree = Tree::new(