1
0
Fork 0
mirror of https://github.com/Zedfrigg/ironbar.git synced 2025-07-01 10:41:03 +02:00

refactor: various changes based on rust 1.65 clippy

This commit is contained in:
Jake Stanger 2022-11-06 22:53:48 +00:00
parent c48029664d
commit ff17ec1996
No known key found for this signature in database
GPG key ID: C51FC8F9CB0BEA61
7 changed files with 59 additions and 64 deletions

View file

@ -101,20 +101,21 @@ impl Config {
/// parse it and return a new instance of `Self`. /// parse it and return a new instance of `Self`.
#[instrument] #[instrument]
pub fn load() -> Result<Self> { pub fn load() -> Result<Self> {
let config_path = if let Ok(config_path) = env::var("IRONBAR_CONFIG") { let config_path = env::var("IRONBAR_CONFIG").map_or_else(
let path = PathBuf::from(config_path); |_| Self::try_find_config(),
if path.exists() { |config_path| {
Ok(path) let path = PathBuf::from(config_path);
} else { if path.exists() {
Err(Report::msg(format!( Ok(path)
"Specified config file does not exist: {}", } else {
path.display() Err(Report::msg(format!(
)) "Specified config file does not exist: {}",
.note("Config file was specified using `IRONBAR_CONFIG` environment variable")) path.display()
} ))
} else { .note("Config file was specified using `IRONBAR_CONFIG` environment variable"))
Self::try_find_config() }
}?; },
)?;
Self::load_file(&config_path) Self::load_file(&config_path)
} }
@ -141,13 +142,15 @@ impl Config {
} }
}); });
match file { file.map_or_else(
Some(file) => Ok(file), || {
None => Err(Report::msg("Could not find config file") Err(Report::msg("Could not find config file")
.suggestion("Ironbar does not include a configuration out of the box") .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("A guide on writing a config can be found on the wiki:")
.suggestion("https://github.com/JakeStanger/ironbar/wiki/configuration-guide")), .suggestion("https://github.com/JakeStanger/ironbar/wiki/configuration-guide"))
} },
Ok,
)
} }
/// Loads the config file at the specified path /// Loads the config file at the specified path

View file

@ -68,17 +68,12 @@ fn parse_desktop_file(path: PathBuf) -> io::Result<HashMap<String, String>> {
/// Attempts to get the icon name from the app's `.desktop` file. /// Attempts to get the icon name from the app's `.desktop` file.
fn get_desktop_icon_name(app_id: &str) -> Option<String> { fn get_desktop_icon_name(app_id: &str) -> Option<String> {
match find_desktop_file(app_id) { find_desktop_file(app_id).and_then(|file| {
Some(file) => { let map = parse_desktop_file(file);
let map = parse_desktop_file(file); map.map_or(None, |map| {
map.get("Icon").map(std::string::ToString::to_string)
match map { })
Ok(map) => map.get("Icon").map(std::string::ToString::to_string), })
Err(_) => None,
}
}
None => None,
}
} }
enum IconLocation { enum IconLocation {
@ -137,11 +132,7 @@ pub fn get_icon(theme: &IconTheme, app_id: &str, size: i32) -> Option<Pixbuf> {
match icon_location { match icon_location {
Some(IconLocation::Theme(icon_name)) => { Some(IconLocation::Theme(icon_name)) => {
let icon = theme.load_icon(&icon_name, size, IconLookupFlags::FORCE_SIZE); let icon = theme.load_icon(&icon_name, size, IconLookupFlags::FORCE_SIZE);
icon.map_or(None, |icon| icon)
match icon {
Ok(icon) => icon,
Err(_) => None,
}
} }
Some(IconLocation::File(path)) => Pixbuf::from_file_at_scale(path, size, size, true).ok(), Some(IconLocation::File(path)) => Pixbuf::from_file_at_scale(path, size, size, true).ok(),
None => None, None => None,

View file

@ -81,18 +81,20 @@ impl Module<gtk::Box> for LauncherModule {
tx: Sender<ModuleUpdateEvent<Self::SendMessage>>, tx: Sender<ModuleUpdateEvent<Self::SendMessage>>,
mut rx: Receiver<Self::ReceiveMessage>, mut rx: Receiver<Self::ReceiveMessage>,
) -> crate::Result<()> { ) -> crate::Result<()> {
let items = match &self.favorites { let items = self
Some(favorites) => favorites .favorites
.iter() .as_ref()
.map(|app_id| { .map_or_else(IndexMap::new, |favorites| {
( favorites
app_id.to_string(), .iter()
Item::new(app_id.to_string(), OpenState::Closed, true), .map(|app_id| {
) (
}) app_id.to_string(),
.collect::<IndexMap<_, _>>(), Item::new(app_id.to_string(), OpenState::Closed, true),
None => IndexMap::new(), )
}; })
.collect::<IndexMap<_, _>>()
});
let items = Arc::new(Mutex::new(items)); let items = Arc::new(Mutex::new(items));

View file

@ -141,10 +141,9 @@ async fn try_get_mpd_conn(host: &str) -> Result<Connection, MpdProtocolError> {
fn is_unix_socket(host: &str) -> bool { fn is_unix_socket(host: &str) -> bool {
let path = PathBuf::from(host); let path = PathBuf::from(host);
path.exists() path.exists()
&& match path.metadata() { && path
Ok(metadata) => metadata.file_type().is_socket(), .metadata()
Err(_) => false, .map_or(false, |metadata| metadata.file_type().is_socket())
}
} }
async fn connect_unix(host: &str) -> Result<Connection, MpdProtocolError> { async fn connect_unix(host: &str) -> Result<Connection, MpdProtocolError> {

View file

@ -97,10 +97,7 @@ fn default_music_dir() -> PathBuf {
/// Attempts to read the first value for a tag /// Attempts to read the first value for a tag
/// (since the MPD client returns a vector of tags, or None) /// (since the MPD client returns a vector of tags, or None)
pub fn try_get_first_tag(vec: Option<&Vec<String>>) -> Option<&str> { pub fn try_get_first_tag(vec: Option<&Vec<String>>) -> Option<&str> {
match vec { vec.and_then(|vec| vec.first().map(String::as_str))
Some(vec) => vec.first().map(String::as_str),
None => None,
}
} }
/// Formats a duration given in seconds /// Formats a duration given in seconds
@ -369,11 +366,14 @@ impl Module<Button> for MpdModule {
.join("cover.jpg"), .join("cover.jpg"),
); );
if let Ok(pixbuf) = Pixbuf::from_file_at_scale(cover_path, 128, 128, true) { Pixbuf::from_file_at_scale(cover_path, 128, 128, true).map_or_else(
album_image.set_from_pixbuf(Some(&pixbuf)); |_| {
} else { album_image.set_from_pixbuf(None);
album_image.set_from_pixbuf(None); },
} |pixbuf| {
album_image.set_from_pixbuf(Some(&pixbuf));
},
);
} }
title_label title_label

View file

@ -359,7 +359,7 @@ fn refresh_disk_tokens(format_info: &mut HashMap<String, String>, sys: &mut Syst
let key = disk let key = disk
.mount_point() .mount_point()
.to_str() .to_str()
.map(|s| s.replace('{', "").replace('}', "")); .map(|s| s.replace(['{', '}'], ""));
if let Some(key) = key { if let Some(key) = key {
let total = disk.total_space(); let total = disk.total_space();

View file

@ -23,7 +23,7 @@ pub struct TrayModule;
fn get_icon(item: &StatusNotifierItem) -> Option<Image> { fn get_icon(item: &StatusNotifierItem) -> Option<Image> {
item.icon_theme_path.as_ref().and_then(|path| { item.icon_theme_path.as_ref().and_then(|path| {
let theme = IconTheme::new(); let theme = IconTheme::new();
theme.append_search_path(&path); theme.append_search_path(path);
item.icon_name.as_ref().and_then(|icon_name| { item.icon_name.as_ref().and_then(|icon_name| {
let icon_info = theme.lookup_icon(icon_name, 16, IconLookupFlags::empty()); let icon_info = theme.lookup_icon(icon_name, 16, IconLookupFlags::empty());