Skip to content

Commit

Permalink
Merge pull request #230 from KP64/master
Browse files Browse the repository at this point in the history
refactor: ♻️ Shortening Code and Removal of Duplication
  • Loading branch information
solidiquis authored Jul 27, 2023
2 parents 6fc3494 + 4ee4975 commit dd92eb6
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 91 deletions.
20 changes: 7 additions & 13 deletions src/context/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
})
}
13 changes: 2 additions & 11 deletions src/context/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
}
}
}
2 changes: 1 addition & 1 deletion src/context/config/toml/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
9 changes: 1 addition & 8 deletions src/context/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -25,7 +24,7 @@ pub enum Error {
PatternNotProvided,

#[error("{0}")]
ConfigError(TomlError),
ConfigError(#[from] TomlError),

#[error("{0}")]
MatchError(#[from] MatchesError),
Expand All @@ -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)
}
}
2 changes: 1 addition & 1 deletion src/icons/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
42 changes: 2 additions & 40 deletions src/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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| {
Expand Down Expand Up @@ -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 {
Expand All @@ -348,6 +338,7 @@ impl Tree {
};
}

#[cfg(unix)]
if ctx.long {
if let Some(owner) = node.owner() {
let owner_len = owner.len();
Expand Down Expand Up @@ -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 {
Expand Down
22 changes: 5 additions & 17 deletions src/tree/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down

0 comments on commit dd92eb6

Please sign in to comment.