From ff17ec1996cf344663e84e79d11b08dc84b97635 Mon Sep 17 00:00:00 2001 From: Jake Stanger Date: Sun, 6 Nov 2022 22:53:48 +0000 Subject: [PATCH] refactor: various changes based on rust 1.65 clippy --- src/config.rs | 45 ++++++++++++++++++++----------------- src/icon.rs | 23 ++++++------------- src/modules/launcher/mod.rs | 26 +++++++++++---------- src/modules/mpd/client.rs | 7 +++--- src/modules/mpd/mod.rs | 18 +++++++-------- src/modules/sysinfo.rs | 2 +- src/modules/tray/mod.rs | 2 +- 7 files changed, 59 insertions(+), 64 deletions(-) diff --git a/src/config.rs b/src/config.rs index 7256289..c30ed9d 100644 --- a/src/config.rs +++ b/src/config.rs @@ -101,20 +101,21 @@ impl Config { /// parse it and return a new instance of `Self`. #[instrument] pub fn load() -> Result { - let config_path = if let Ok(config_path) = env::var("IRONBAR_CONFIG") { - let path = PathBuf::from(config_path); - if path.exists() { - Ok(path) - } else { - Err(Report::msg(format!( - "Specified config file does not exist: {}", - path.display() - )) - .note("Config file was specified using `IRONBAR_CONFIG` environment variable")) - } - } else { - Self::try_find_config() - }?; + let config_path = env::var("IRONBAR_CONFIG").map_or_else( + |_| Self::try_find_config(), + |config_path| { + let path = PathBuf::from(config_path); + if path.exists() { + Ok(path) + } else { + Err(Report::msg(format!( + "Specified config file does not exist: {}", + path.display() + )) + .note("Config file was specified using `IRONBAR_CONFIG` environment variable")) + } + }, + )?; Self::load_file(&config_path) } @@ -141,13 +142,15 @@ impl Config { } }); - match file { - Some(file) => Ok(file), - None => Err(Report::msg("Could not find config file") - .suggestion("Ironbar does not include a configuration out of the box") - .suggestion("A guide on writing a config can be found on the wiki:") - .suggestion("https://github.com/JakeStanger/ironbar/wiki/configuration-guide")), - } + file.map_or_else( + || { + Err(Report::msg("Could not find config file") + .suggestion("Ironbar does not include a configuration out of the box") + .suggestion("A guide on writing a config can be found on the wiki:") + .suggestion("https://github.com/JakeStanger/ironbar/wiki/configuration-guide")) + }, + Ok, + ) } /// Loads the config file at the specified path diff --git a/src/icon.rs b/src/icon.rs index 31fdfbf..8890dc4 100644 --- a/src/icon.rs +++ b/src/icon.rs @@ -68,17 +68,12 @@ fn parse_desktop_file(path: PathBuf) -> io::Result> { /// Attempts to get the icon name from the app's `.desktop` file. fn get_desktop_icon_name(app_id: &str) -> Option { - match find_desktop_file(app_id) { - Some(file) => { - let map = parse_desktop_file(file); - - match map { - Ok(map) => map.get("Icon").map(std::string::ToString::to_string), - Err(_) => None, - } - } - None => None, - } + find_desktop_file(app_id).and_then(|file| { + let map = parse_desktop_file(file); + map.map_or(None, |map| { + map.get("Icon").map(std::string::ToString::to_string) + }) + }) } enum IconLocation { @@ -137,11 +132,7 @@ pub fn get_icon(theme: &IconTheme, app_id: &str, size: i32) -> Option { match icon_location { Some(IconLocation::Theme(icon_name)) => { let icon = theme.load_icon(&icon_name, size, IconLookupFlags::FORCE_SIZE); - - match icon { - Ok(icon) => icon, - Err(_) => None, - } + icon.map_or(None, |icon| icon) } Some(IconLocation::File(path)) => Pixbuf::from_file_at_scale(path, size, size, true).ok(), None => None, diff --git a/src/modules/launcher/mod.rs b/src/modules/launcher/mod.rs index 9cd2490..0ee238a 100644 --- a/src/modules/launcher/mod.rs +++ b/src/modules/launcher/mod.rs @@ -81,18 +81,20 @@ impl Module for LauncherModule { tx: Sender>, mut rx: Receiver, ) -> crate::Result<()> { - let items = match &self.favorites { - Some(favorites) => favorites - .iter() - .map(|app_id| { - ( - app_id.to_string(), - Item::new(app_id.to_string(), OpenState::Closed, true), - ) - }) - .collect::>(), - None => IndexMap::new(), - }; + let items = self + .favorites + .as_ref() + .map_or_else(IndexMap::new, |favorites| { + favorites + .iter() + .map(|app_id| { + ( + app_id.to_string(), + Item::new(app_id.to_string(), OpenState::Closed, true), + ) + }) + .collect::>() + }); let items = Arc::new(Mutex::new(items)); diff --git a/src/modules/mpd/client.rs b/src/modules/mpd/client.rs index 54a9a47..852750f 100644 --- a/src/modules/mpd/client.rs +++ b/src/modules/mpd/client.rs @@ -141,10 +141,9 @@ async fn try_get_mpd_conn(host: &str) -> Result { fn is_unix_socket(host: &str) -> bool { let path = PathBuf::from(host); path.exists() - && match path.metadata() { - Ok(metadata) => metadata.file_type().is_socket(), - Err(_) => false, - } + && path + .metadata() + .map_or(false, |metadata| metadata.file_type().is_socket()) } async fn connect_unix(host: &str) -> Result { diff --git a/src/modules/mpd/mod.rs b/src/modules/mpd/mod.rs index c2b500b..f873012 100644 --- a/src/modules/mpd/mod.rs +++ b/src/modules/mpd/mod.rs @@ -97,10 +97,7 @@ fn default_music_dir() -> PathBuf { /// Attempts to read the first value for a tag /// (since the MPD client returns a vector of tags, or None) pub fn try_get_first_tag(vec: Option<&Vec>) -> Option<&str> { - match vec { - Some(vec) => vec.first().map(String::as_str), - None => None, - } + vec.and_then(|vec| vec.first().map(String::as_str)) } /// Formats a duration given in seconds @@ -369,11 +366,14 @@ impl Module