Skip to content

Commit 4ee4975

Browse files
committed
refactor: ♻️ Shortening Code and Removal of Duplication
1 parent 6fc3494 commit 4ee4975

File tree

7 files changed

+19
-91
lines changed

7 files changed

+19
-91
lines changed

src/context/args.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pub trait Reconciler: CommandFactory + FromArgMatches {
6666
let argument_source = user_args
6767
.value_source(id_str)
6868
.map_or(&config_args, |source| {
69-
if matches!(source, ValueSource::CommandLine) {
69+
if source == ValueSource::CommandLine {
7070
&user_args
7171
} else {
7272
&config_args
@@ -112,25 +112,19 @@ fn init_empty_args() -> Vec<OsString> {
112112
/// Loads an [`ArgMatches`] from `.erdtreerc`.
113113
#[inline]
114114
fn load_rc_config_args() -> Option<ArgMatches> {
115-
if let Some(rc_config) = config::rc::read_config_to_string() {
115+
config::rc::read_config_to_string().map(|rc_config| {
116116
let parsed_args = config::rc::parse(&rc_config);
117-
let config_args = Context::command().get_matches_from(parsed_args);
118-
119-
return Some(config_args);
120-
}
121-
122-
None
117+
Context::command().get_matches_from(parsed_args)
118+
})
123119
}
124120

125121
/// Loads an [`ArgMatches`] from `.erdtree.toml`.
126122
#[inline]
127123
fn load_toml_config_args(named_table: Option<&str>) -> Result<Option<ArgMatches>, Error> {
128-
if let Ok(toml_config) = config::toml::load() {
124+
config::toml::load().map_or(Ok(None), |toml_config| {
129125
let parsed_args = config::toml::parse(toml_config, named_table)?;
130126
let config_args = Context::command().get_matches_from(parsed_args);
131127

132-
return Ok(Some(config_args));
133-
}
134-
135-
Ok(None)
128+
Ok(Some(config_args))
129+
})
136130
}

src/context/column.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::convert::From;
33

44
/// Utility struct to help store maximum column widths for attributes of each node. Each width is
55
/// measured as the number of columns of the tty's window.
6+
#[derive(Default)]
67
pub struct Properties {
78
pub max_size_width: usize,
89
pub max_size_unit_width: usize,
@@ -32,18 +33,8 @@ impl From<&Context> for Properties {
3233
};
3334

3435
Self {
35-
max_size_width: 0,
3636
max_size_unit_width: unit_width,
37-
#[cfg(unix)]
38-
max_nlink_width: 0,
39-
#[cfg(unix)]
40-
max_ino_width: 0,
41-
#[cfg(unix)]
42-
max_block_width: 0,
43-
#[cfg(unix)]
44-
max_owner_width: 0,
45-
#[cfg(unix)]
46-
max_group_width: 0,
37+
..Default::default()
4738
}
4839
}
4940
}

src/context/config/toml/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ fn parse_toml() -> Result<(), Box<dyn std::error::Error>> {
2121
threads = 10
2222
"#;
2323

24-
config_file.write(toml_contents.as_bytes())?;
24+
config_file.write_all(toml_contents.as_bytes())?;
2525

2626
let file = config_file
2727
.path()

src/context/error.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use super::config::toml::error::Error as TomlError;
22
use clap::{parser::MatchesError, Error as ClapError};
33
use ignore::Error as IgnoreError;
44
use regex::Error as RegexError;
5-
use std::convert::From;
65

76
#[derive(Debug, thiserror::Error)]
87
pub enum Error {
@@ -25,7 +24,7 @@ pub enum Error {
2524
PatternNotProvided,
2625

2726
#[error("{0}")]
28-
ConfigError(TomlError),
27+
ConfigError(#[from] TomlError),
2928

3029
#[error("{0}")]
3130
MatchError(#[from] MatchesError),
@@ -36,9 +35,3 @@ pub enum Error {
3635
#[error("Please migrate from `erdtreerc` to `.erdtree.toml` to make use of `--config`")]
3736
Rc,
3837
}
39-
40-
impl From<TomlError> for Error {
41-
fn from(value: TomlError) -> Self {
42-
Self::ConfigError(value)
43-
}
44-
}

src/icons/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub mod fs;
1313
/// Attempts to return an icon given a file extension along with its default color code 8-bit
1414
/// value.
1515
fn icon_from_ext(ext: &OsStr) -> Option<(u8, &'static str)> {
16-
EXT_ICON_MAP.get(ext).map(|(code, icon)| (*code, *icon))
16+
EXT_ICON_MAP.get(ext).copied()
1717
}
1818

1919
/// Attempts to return an icon based on file type.

src/tree/mod.rs

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -207,12 +207,7 @@ impl Tree {
207207
for child_id in &children {
208208
let index = *child_id;
209209

210-
let is_dir = {
211-
let arena = tree[index].get();
212-
arena.is_dir()
213-
};
214-
215-
if is_dir {
210+
if tree[index].get().is_dir() {
216211
Self::assemble_tree(
217212
tree,
218213
index,
@@ -252,10 +247,6 @@ impl Tree {
252247

253248
let dir = tree[current_node_id].get();
254249

255-
#[cfg(unix)]
256-
Self::update_column_properties(column_properties, dir, ctx);
257-
258-
#[cfg(not(unix))]
259250
Self::update_column_properties(column_properties, dir, ctx);
260251

261252
children.sort_by(|&id_a, &id_b| {
@@ -321,7 +312,6 @@ impl Tree {
321312
}
322313

323314
/// Updates [`column::Properties`] with provided [`Node`].
324-
#[cfg(unix)]
325315
fn update_column_properties(col_props: &mut column::Properties, node: &Node, ctx: &Context) {
326316
if let Some(file_size) = node.file_size() {
327317
if ctx.byte_metric() && ctx.human {
@@ -348,6 +338,7 @@ impl Tree {
348338
};
349339
}
350340

341+
#[cfg(unix)]
351342
if ctx.long {
352343
if let Some(owner) = node.owner() {
353344
let owner_len = owner.len();
@@ -390,35 +381,6 @@ impl Tree {
390381
}
391382
}
392383
}
393-
394-
/// Updates [column::Properties] with provided [Node].
395-
#[cfg(not(unix))]
396-
fn update_column_properties(col_props: &mut column::Properties, node: &Node, ctx: &Context) {
397-
if let Some(file_size) = node.file_size() {
398-
if ctx.byte_metric() && ctx.human {
399-
let out = format!("{file_size}");
400-
let [size, unit]: [&str; 2] =
401-
out.split(' ').collect::<Vec<&str>>().try_into().unwrap();
402-
403-
let file_size_cols = size.len();
404-
let file_size_unit_cols = unit.len();
405-
406-
if file_size_cols > col_props.max_size_width {
407-
col_props.max_size_width = file_size_cols;
408-
}
409-
410-
if file_size_unit_cols > col_props.max_size_unit_width {
411-
col_props.max_size_unit_width = file_size_unit_cols;
412-
}
413-
} else {
414-
let file_size_cols = utils::num_integral(file_size.value());
415-
416-
if file_size_cols > col_props.max_size_width {
417-
col_props.max_size_width = file_size_cols;
418-
}
419-
};
420-
}
421-
}
422384
}
423385

424386
impl TryFrom<&Context> for WalkParallel {

src/tree/node/mod.rs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,7 @@ impl Node {
9494

9595
let blocks = self.metadata.blocks();
9696

97-
if blocks == 0 {
98-
None
99-
} else {
100-
Some(blocks)
101-
}
97+
(blocks != 0).then_some(blocks)
10298
}
10399

104100
/// Timestamp of when file was last modified.
@@ -123,22 +119,14 @@ impl Node {
123119

124120
/// Returns the underlying `ino` of the [`DirEntry`].
125121
#[cfg(unix)]
126-
pub const fn ino(&self) -> Option<u64> {
127-
if let Some(inode) = self.inode {
128-
Some(inode.ino)
129-
} else {
130-
None
131-
}
122+
pub fn ino(&self) -> Option<u64> {
123+
self.inode.map(|inode| inode.ino)
132124
}
133125

134126
/// Returns the underlying `nlink` of the [`DirEntry`].
135127
#[cfg(unix)]
136-
pub const fn nlink(&self) -> Option<u64> {
137-
if let Some(inode) = self.inode {
138-
Some(inode.nlink)
139-
} else {
140-
None
141-
}
128+
pub fn nlink(&self) -> Option<u64> {
129+
self.inode.map(|inode| inode.nlink)
142130
}
143131

144132
/// Returns `true` if node is a directory.

0 commit comments

Comments
 (0)