diff --git a/src/util_env.rs b/src/util_env.rs index d81dd5c..83056a5 100644 --- a/src/util_env.rs +++ b/src/util_env.rs @@ -2,8 +2,5 @@ use std::env; pub fn is_env_on(var: &str) -> bool { - match env::var(var) { - Err(_) => false, - Ok(v) => (v == "TRUE" || v == "true" || v =="YES" || v == "yes" || v == "1"), - } + env::var(var).map(|v| v.to_lowercase()).map(|v| (v == "true" || v == "yes" || v == "1")).unwrap_or(false) } diff --git a/src/util_file.rs b/src/util_file.rs index f0199cc..5fc8223 100644 --- a/src/util_file.rs +++ b/src/util_file.rs @@ -6,6 +6,7 @@ use std::{ }; use super::{ + iff, util_os, util_io, new_box_ioerror, @@ -13,11 +14,7 @@ use super::{ }; pub fn get_home_str() -> Option { - if util_os::is_macos_or_linux() { - env::var("HOME").ok() - } else { - None - } + iff!(util_os::is_macos_or_linux(), env::var("HOME").ok(), None) } pub fn get_home_path() -> Option { @@ -25,12 +22,11 @@ pub fn get_home_path() -> Option { } pub fn get_absolute_path(path: &str) -> Option { - if path == "~" { - return Some(PathBuf::from(get_home_str()?)); - } else if path.starts_with("~/") { - return Some(PathBuf::from(&format!("{}/{}", get_home_str()?, &path[2..]))); + match path { + "~" => Some(PathBuf::from(get_home_str()?)), + path if path.starts_with("~/") => Some(PathBuf::from(&format!("{}/{}", get_home_str()?, &path[2..]))), + path => fs::canonicalize(path).ok(), } - fs::canonicalize(path).ok() } pub fn read_file_content(file: &str) -> XResult { @@ -41,10 +37,7 @@ pub fn read_file_content(file: &str) -> XResult { } pub fn is_symlink(path: &Path) -> bool { - match path.symlink_metadata() { - Err(_) => false, - Ok(meta) => meta.file_type().is_symlink(), - } + path.symlink_metadata().map(|meta| meta.file_type().is_symlink()).unwrap_or(false) } pub fn walk_dir(dir: &Path, diff --git a/src/util_msg.rs b/src/util_msg.rs index 0bb5951..3a15006 100644 --- a/src/util_msg.rs +++ b/src/util_msg.rs @@ -8,7 +8,7 @@ lazy_static! { pub enum MessageType { INFO, OK, WARN, ERROR, DEBUG, } -pub fn is_atty() -> bool{ +pub fn is_atty() -> bool { let stdout_fileno = unsafe { libc::isatty(libc::STDOUT_FILENO as i32) }; stdout_fileno != 0 } @@ -83,9 +83,9 @@ pub fn get_term_width_message(message: &str, left: usize) -> String { return message.to_string(); } let mut s = String::new(); - s.push_str(&message[0..find_char_boundary(&message, w-10-5-left)]); + s.push_str(&message[0..find_char_boundary(&message, w - 10 - 5 - left)]); s.push_str("[...]"); - s.push_str(&message[find_char_boundary(&message, len-10)..]); + s.push_str(&message[find_char_boundary(&message, len - 10)..]); s }, }