Skip to content

Commit

Permalink
Merge pull request #100 from KP64/master
Browse files Browse the repository at this point in the history
Application of Clippy lints and possible improvement in development ergonomics
  • Loading branch information
solidiquis authored Mar 27, 2023
2 parents 82c192e + 341fcae commit 7d0f1dd
Show file tree
Hide file tree
Showing 15 changed files with 170 additions and 263 deletions.
125 changes: 34 additions & 91 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ keywords = ["tree", "commandline", "command-line", "du", "disk-usage"]
exclude = ["assets/*"]
readme = "README.md"
license = "MIT"
rust-version = "1.65"
rust-version = "1.67.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand All @@ -26,12 +26,12 @@ path = "src/main.rs"
ansi_term = "0.12.1"
clap = { version = "4.1.1", features = ["derive"] }
clap_complete = "4.1.1"
crossbeam = "0.8.2"
filesize = "0.2.0"
ignore = "0.4.2"
indextree = "4.6.0"
lscolors = { version = "0.13.0", features = ["ansi_term"] }
once_cell = "1.17.0"
thiserror = "1.0.40"

[dev-dependencies]
indoc = "2.0.0"
Expand Down
22 changes: 5 additions & 17 deletions src/fs/inode.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
use std::{
convert::TryFrom,
error::Error as StdError,
fmt::{self, Display},
fs::Metadata,
};
use std::{convert::TryFrom, fs::Metadata};

/// Represents a file's underlying inode.
#[derive(Debug)]
Expand All @@ -15,27 +10,20 @@ pub struct Inode {

impl Inode {
/// Initializer for an inode given all the properties that make it unique.
pub fn new(ino: u64, dev: u64, nlink: u64) -> Self {
pub const fn new(ino: u64, dev: u64, nlink: u64) -> Self {
Self { ino, dev, nlink }
}

/// Returns a tuple fields of the [Inode] that mark is unique.
pub fn properties(&self) -> (u64, u64) {
pub const fn properties(&self) -> (u64, u64) {
(self.ino, self.dev)
}
}

#[derive(Debug)]
#[derive(Debug, thiserror::Error)]
#[error("Insufficient information to compute inode")]
pub struct Error;

impl Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Insufficient information to compute inode")
}
}

impl StdError for Error {}

impl TryFrom<Metadata> for Inode {
type Error = Error;

Expand Down
6 changes: 3 additions & 3 deletions src/icons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,17 +293,17 @@ pub fn icon_from_ext(ext: &OsStr) -> Option<&str> {
/// Attempts to return an icon based on file type.
pub fn icon_from_file_type(ft: &FileType) -> Option<&str> {
if ft.is_dir() {
return FILE_TYPE_ICON_MAP.get("dir").map(|i| *i);
return FILE_TYPE_ICON_MAP.get("dir").copied();
} else if ft.is_symlink() {
return FILE_TYPE_ICON_MAP.get("symlink").map(|i| *i);
return FILE_TYPE_ICON_MAP.get("symlink").copied();
}

None
}

/// Attempts to get the icon associated with the special file kind.
pub fn icon_from_file_name(name: &OsStr) -> Option<&str> {
FILE_NAME_ICON_MAP.get(name).map(|i| *i)
FILE_NAME_ICON_MAP.get(name).copied()
}

/// Returns the default fallback icon.
Expand Down
20 changes: 20 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
#![warn(
clippy::all,
clippy::correctness,
clippy::suspicious,
clippy::style,
clippy::complexity,
clippy::perf,
clippy::pedantic,
clippy::nursery
)]
#![allow(
clippy::struct_excessive_bools,
clippy::cast_precision_loss,
clippy::items_after_statements,
clippy::similar_names,
clippy::doc_markdown,
clippy::too_many_arguments,
clippy::type_complexity,
clippy::fallible_impl_from
)]
use clap::CommandFactory;
use render::{
context::Context,
Expand Down
19 changes: 8 additions & 11 deletions src/render/context/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,29 @@ const XDG_CONFIG_HOME: &str = "XDG_CONFIG_HOME";
/// - `$HOME/.erdtreerc`
pub fn read_config_to_string<T: AsRef<Path>>(path: Option<T>) -> Option<String> {
if let Some(p) = path {
return fs::read_to_string(p).ok().map(prepend_arg_prefix);
return fs::read_to_string(p).ok().map(|l| prepend_arg_prefix(&l));
}

config_from_config_path()
.or_else(config_from_xdg_path)
.or_else(config_from_home)
.map(prepend_arg_prefix)
.map(|e| prepend_arg_prefix(&e))
}

/// Parses the config `str`, removing comments and preparing it as a format understood by
/// [`get_matches_from`].
///
/// [`get_matches_from`]: clap::builder::Command::get_matches_from
pub fn parse_config<'a>(config: &'a str) -> Vec<&'a str> {
pub fn parse<'a>(config: &'a str) -> Vec<&'a str> {
config
.lines()
.filter(|line| {
line.trim_start()
.chars()
.nth(0)
.map(|ch| ch != '#')
.unwrap_or(true)
.next()
.map_or(true, |ch| ch != '#')
})
.map(str::split_ascii_whitespace)
.flatten()
.flat_map(str::split_ascii_whitespace)
.collect::<Vec<&'a str>>()
}

Expand All @@ -53,8 +51,7 @@ fn config_from_config_path() -> Option<String> {
env::var_os(ERDTREE_CONFIG_PATH)
.map(PathBuf::from)
.map(fs::read_to_string)
.map(Result::ok)
.flatten()
.and_then(Result::ok)
}

/// Try to read in config from either one of the following locations:
Expand Down Expand Up @@ -92,6 +89,6 @@ fn config_from_xdg_path() -> Option<String> {
/// [`get_matches_from`].
///
/// [`get_matches_from`]: clap::builder::Command::get_matches_from
fn prepend_arg_prefix(config: String) -> String {
fn prepend_arg_prefix(config: &str) -> String {
format!("--\n{config}")
}
Loading

0 comments on commit 7d0f1dd

Please sign in to comment.