summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBenji Nguyen <45523555+solidiquis@users.noreply.github.com>2023-07-27 10:01:48 -0700
committerGitHub <noreply@github.com>2023-07-27 10:01:48 -0700
commitdd92eb68417f24ba4984139851d3af04d76f52ab (patch)
treeaeda63af9c0b02893fa830c13f8e9876863d6500
parent6fc34947fb55ba87685b22ed33fa683427189786 (diff)
parent4ee49757d7df448e342b6e505a5048df8444517c (diff)
Merge pull request #230 from KP64/master
refactor: :recycle: Shortening Code and Removal of Duplication
-rw-r--r--src/context/args.rs20
-rw-r--r--src/context/column.rs13
-rw-r--r--src/context/config/toml/test.rs2
-rw-r--r--src/context/error.rs9
-rw-r--r--src/icons/mod.rs2
-rw-r--r--src/tree/mod.rs42
-rw-r--r--src/tree/node/mod.rs22
7 files changed, 19 insertions, 91 deletions
diff --git a/src/context/args.rs b/src/context/args.rs
index 17027cc..0608a23 100644
--- a/src/context/args.rs
+++ b/src/context/args.rs
@@ -66,7 +66,7 @@ pub trait Reconciler: CommandFactory + FromArgMatches {
let argument_source = user_args
.value_source(id_str)
.map_or(&config_args, |source| {
- if matches!(source, ValueSource::CommandLine) {
+ if source == ValueSource::CommandLine {
&user_args
} else {
&config_args
@@ -112,25 +112,19 @@ fn init_empty_args() -> Vec<OsString> {
/// Loads an [`ArgMatches`] from `.erdtreerc`.
#[inline]
fn load_rc_config_args() -> Option<ArgMatches> {
- if let Some(rc_config) = config::rc::read_config_to_string() {
+ config::rc::read_config_to_string().map(|rc_config| {
let parsed_args = config::rc::parse(&rc_config);
- let config_args = Context::command().get_matches_from(parsed_args);
-
- return Some(config_args);
- }
-
- None
+ Context::command().get_matches_from(parsed_args)
+ })
}
/// Loads an [`ArgMatches`] from `.erdtree.toml`.
#[inline]
fn load_toml_config_args(named_table: Option<&str>) -> Result<Option<ArgMatches>, Error> {
- if let Ok(toml_config) = config::toml::load() {
+ config::toml::load().map_or(Ok(None), |toml_config| {
let parsed_args = config::toml::parse(toml_config, named_table)?;
let config_args = Context::command().get_matches_from(parsed_args);
- return Ok(Some(config_args));
- }
-
- Ok(None)
+ Ok(Some(config_args))
+ })
}
diff --git a/src/context/column.rs b/src/context/column.rs
index 16bf5f8..c66daa4 100644
--- a/src/context/column.rs
+++ b/src/context/column.rs
@@ -3,6 +3,7 @@ use std::convert::From;
/// Utility struct to help store maximum column widths for attributes of each node. Each width is
/// measured as the number of columns of the tty's window.
+#[derive(Default)]
pub struct Properties {
pub max_size_width: usize,
pub max_size_unit_width: usize,
@@ -32,18 +33,8 @@ impl From<&Context> for Properties {
};
Self {
- max_size_width: 0,
max_size_unit_width: unit_width,
- #[cfg(unix)]
- max_nlink_width: 0,
- #[cfg(unix)]
- max_ino_width: 0,
- #[cfg(unix)]
- max_block_width: 0,
- #[cfg(unix)]
- max_owner_width: 0,
- #[cfg(unix)]
- max_group_width: 0,
+ ..Default::default()
}
}
}
diff --git a/src/context/config/toml/test.rs b/src/context/config/toml/test.rs
index adba50d..368c9db 100644
--- a/src/context/config/toml/test.rs
+++ b/src/context/config/toml/test.rs
@@ -21,7 +21,7 @@ fn parse_toml() -> Result<(), Box<dyn std::error::Error>> {
threads = 10
"#;
- config_file.write(toml_contents.as_bytes())?;
+ config_file.write_all(toml_contents.as_bytes())?;
let file = config_file
.path()
diff --git a/src/context/error.rs b/src/context/error.rs
index 6b2e82b..89fbb74 100644
--- a/src/context/error.rs
+++ b/src/context/error.rs
@@ -2,7 +2,6 @@ use super::config::toml::error::Error as TomlError;
use clap::{parser::MatchesError, Error as ClapError};
use ignore::Error as IgnoreError;
use regex::Error as RegexError;
-use std::convert::From;
#[derive(Debug, thiserror::Error)]
pub enum Error {
@@ -25,7 +24,7 @@ pub enum Error {
PatternNotProvided,
#[error("{0}")]
- ConfigError(TomlError),
+ ConfigError(#[from] TomlError),
#[error("{0}")]
MatchError(#[from] MatchesError),
@@ -36,9 +35,3 @@ pub enum Error {
#[error("Please migrate from `erdtreerc` to `.erdtree.toml` to make use of `--config`")]
Rc,
}
-
-impl From<TomlError> for Error {
- fn from(value: TomlError) -> Self {
- Self::ConfigError(value)
- }
-}
diff --git a/src/icons/mod.rs b/src/icons/mod.rs
index 7ac580f..01e9298 100644
--- a/src/icons/mod.rs
+++ b/src/icons/mod.rs
@@ -13,7 +13,7 @@ pub mod fs;
/// Attempts to return an icon given a file extension along with its default color code 8-bit
/// value.
fn icon_from_ext(ext: &OsStr) -> Option<(u8, &'static str)> {
- EXT_ICON_MAP.get(ext).map(|(code, icon)| (*code, *icon))
+ EXT_ICON_MAP.get(ext).copied()
}
/// Attempts to return an icon based on file type.
diff --git a/src/tree/mod.rs b/src/tree/mod.rs
index be2b7ee..33568e3 100644
--- a/src/tree/mod.rs
+++ b/src/tree/mod.rs
@@ -207,12 +207,7 @@ impl Tree {
for child_id in &children {
let index = *child_id;
- let is_dir = {
- let arena = tree[index].get();
- arena.is_dir()
- };
-
- if is_dir {
+ if tree[index].get().is_dir() {
Self::assemble_tree(
tree,
index,
@@ -252,10 +247,6 @@ impl Tree {
let dir = tree[current_node_id].get();
- #[cfg(unix)]
- Self::update_column_properties(column_properties, dir, ctx);
-
- #[cfg(not(unix))]
Self::update_column_properties(column_properties, dir, ctx);
children.sort_by(|&id_a, &id_b| {
@@ -321,7 +312,6 @@ impl Tree {
}
/// Updates [`column::Properties`] with provided [`Node`].
- #[cfg(unix)]
fn update_column_properties(col_props: &mut column::Properties, node: &Node, ctx: &Context) {
if let Some(file_size) = node.file_size() {
if ctx.byte_metric() && ctx.human {
@@ -348,6 +338,7 @@ impl Tree {
};
}
+ #[cfg(unix)]
if ctx.long {
if let Some(owner) = node.owner() {
let owner_len = owner.len();
@@ -390,35 +381,6 @@ impl Tree {
}
}
}
-
- /// Updates [column::Properties] with provided [Node].
- #[cfg(not(unix))]
- fn update_column_properties(col_props: &mut column::Properties, node: &Node, ctx: &Context) {
- if let Some(file_size) = node.file_size() {
- if ctx.byte_metric() && ctx.human {
- let out = format!("{file_size}");
- let [size, unit]: [&str; 2] =
- out.split(' ').collect::<Vec<&str>>().try_into().unwrap();
-
- let file_size_cols = size.len();
- let file_size_unit_cols = unit.len();
-
- if file_size_cols > col_props.max_size_width {
- col_props.max_size_width = file_size_cols;
- }
-
- if file_size_unit_cols > col_props.max_size_unit_width {
- col_props.max_size_unit_width = file_size_unit_cols;
- }
- } else {
- let file_size_cols = utils::num_integral(file_size.value());
-
- if file_size_cols > col_props.max_size_width {
- col_props.max_size_width = file_size_cols;
- }
- };
- }
- }
}
impl TryFrom<&Context> for WalkParallel {
diff --git a/src/tree/node/mod.rs b/src/tree/node/mod.rs
index cca2e9e..b010469 100644
--- a/src/tree/node/mod.rs
+++ b/src/tree/node/mod.rs
@@ -94,11 +94,7 @@ impl Node {
let blocks = self.metadata.blocks();
- if blocks == 0 {
- None
- } else {
- Some(blocks)
- }
+ (blocks != 0).then_some(blocks)
}
/// Timestamp of when file was last modified.
@@ -123,22 +119,14 @@ impl Node {
/// Returns the underlying `ino` of the [`DirEntry`].
#[cfg(unix)]
- pub const fn ino(&self) -> Option<u64> {
- if let Some(inode) = self.inode {
- Some(inode.ino)
- } else {
- None
- }
+ pub fn ino(&self) -> Option<u64> {
+ self.inode.map(|inode| inode.ino)
}
/// Returns the underlying `nlink` of the [`DirEntry`].
#[cfg(unix)]
- pub const fn nlink(&self) -> Option<u64> {
- if let Some(inode) = self.inode {
- Some(inode.nlink)
- } else {
- None
- }
+ pub fn nlink(&self) -> Option<u64> {
+ self.inode.map(|inode| inode.nlink)
}
/// Returns `true` if node is a directory.