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`.
#[instrument]
pub fn load() -> Result<Self> {
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

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.
fn get_desktop_icon_name(app_id: &str) -> Option<String> {
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<Pixbuf> {
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,

View file

@ -81,18 +81,20 @@ impl Module<gtk::Box> for LauncherModule {
tx: Sender<ModuleUpdateEvent<Self::SendMessage>>,
mut rx: Receiver<Self::ReceiveMessage>,
) -> 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::<IndexMap<_, _>>(),
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::<IndexMap<_, _>>()
});
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 {
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<Connection, MpdProtocolError> {

View file

@ -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<String>>) -> 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<Button> for MpdModule {
.join("cover.jpg"),
);
if let Ok(pixbuf) = Pixbuf::from_file_at_scale(cover_path, 128, 128, true) {
album_image.set_from_pixbuf(Some(&pixbuf));
} else {
album_image.set_from_pixbuf(None);
}
Pixbuf::from_file_at_scale(cover_path, 128, 128, true).map_or_else(
|_| {
album_image.set_from_pixbuf(None);
},
|pixbuf| {
album_image.set_from_pixbuf(Some(&pixbuf));
},
);
}
title_label

View file

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

View file

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