From cfe045827d453eb72acec0c26740c9fd960105e8 Mon Sep 17 00:00:00 2001 From: Josh Megnauth Date: Mon, 27 Jan 2025 02:17:50 -0500 Subject: [PATCH] chore(clippy): `app.rs`, `mime_app.rs` --- src/app.rs | 204 +++++++++++++++++++++--------------------------- src/mime_app.rs | 10 ++- 2 files changed, 97 insertions(+), 117 deletions(-) diff --git a/src/app.rs b/src/app.rs index e18977e..e97b2d8 100644 --- a/src/app.rs +++ b/src/app.rs @@ -49,7 +49,7 @@ use std::{ collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecDeque}, env, fmt, fs, io, num::NonZeroU16, - path::PathBuf, + path::{Path, PathBuf}, process, sync::{Arc, Mutex}, time::{self, Instant}, @@ -678,7 +678,6 @@ impl App { let mut overlaps: HashMap<_, _> = self .windows .keys() - .into_iter() .map(|k| (*k, (0., 0., 0., 0.))) .collect(); let mut sorted_overlaps: Vec<_> = self.overlap.values().collect(); @@ -690,7 +689,7 @@ impl App { let tr = tr.intersects(overlap); let bl = bl.intersects(overlap); let br = br.intersects(overlap); - let Some((top, left, bottom, right)) = overlaps.get_mut(&w_id) else { + let Some((top, left, bottom, right)) = overlaps.get_mut(w_id) else { continue; }; if tl && tr { @@ -802,12 +801,9 @@ impl App { } fn remove_window(&mut self, id: &window::Id) { - match self.windows.remove(id) { - Some(WindowKind::Desktop(entity)) => { - // Remove the tab from the tab model - self.tab_model.remove(entity); - } - _ => {} + if let Some(WindowKind::Desktop(entity)) = self.windows.remove(id) { + // Remove the tab from the tab model + self.tab_model.remove(entity); } } @@ -817,7 +813,7 @@ impl App { let Some(tab) = self.tab_model.data::(entity) else { return Task::none(); }; - let Some(ref items) = tab.items_opt() else { + let Some(items) = tab.items_opt() else { return Task::none(); }; for item in items.iter() { @@ -1753,7 +1749,7 @@ impl Application for App { return self.update(message); } - if let Some(data) = self.nav_model.data::(entity).clone() { + if let Some(data) = self.nav_model.data::(entity) { if let Some(mounter) = MOUNTERS.get(&data.0) { return mounter.mount(data.1.clone()).map(|_| message::none()); } @@ -1770,14 +1766,11 @@ impl Application for App { } fn on_context_drawer(&mut self) -> Task { - match self.context_page { - ContextPage::Preview(..) => { - // Persist state of preview page - if self.core.window.show_context != self.config.show_details { - return self.update(Message::Preview(None)); - } + if let ContextPage::Preview(..) = self.context_page { + // Persist state of preview page + if self.core.window.show_context != self.config.show_details { + return self.update(Message::Preview(None)); } - _ => {} } Task::none() } @@ -1937,12 +1930,14 @@ impl Application for App { } } Message::DesktopViewOptions => { - let mut settings = window::Settings::default(); - settings.decorations = true; - settings.min_size = Some(Size::new(360.0, 180.0)); - settings.resizable = true; - settings.size = Size::new(480.0, 444.0); - settings.transparent = true; + let mut settings = window::Settings { + decorations: true, + min_size: Some(Size::new(360.0, 180.0)), + resizable: true, + size: Size::new(480.0, 444.0), + transparent: true, + ..Default::default() + }; #[cfg(target_os = "linux")] { @@ -2140,11 +2135,9 @@ impl Application for App { let mut still_mounted = false; for item in mounter_items.iter() { if let Some(path) = item.path() { - if path == old_path { - if item.is_mounted() { - still_mounted = true; - break; - } + if path == old_path && item.is_mounted() { + still_mounted = true; + break; } } } @@ -2289,7 +2282,7 @@ impl Application for App { let mut contains_change = false; for event in events.iter() { for event_path in event.paths.iter() { - if event_path.starts_with(&path) { + if event_path.starts_with(path) { match event.kind { notify::EventKind::Modify( notify::event::ModifyKind::Metadata(_), @@ -2303,16 +2296,17 @@ impl Application for App { for item in items.iter_mut() { if item.path_opt() == Some(event_path) { //TODO: reload more, like mime types? - match fs::metadata(&event_path) { - Ok(new_metadata) => match &mut item - .metadata - { - ItemMetadata::Path { + match fs::metadata(event_path) { + Ok(new_metadata) => { + if let ItemMetadata::Path { metadata, .. - } => *metadata = new_metadata, - _ => {} - }, + } = &mut item.metadata + { + *metadata = new_metadata + } + } + Err(err) => { log::warn!("failed to reload metadata for {:?}: {}", path, err); } @@ -2423,43 +2417,32 @@ impl Application for App { Message::OpenItemLocation(entity_opt) => { return Task::batch(self.selected_paths(entity_opt).into_iter().filter_map( |path| { - if let Some(parent) = path.parent() { - Some(self.open_tab( - Location::Path(parent.to_path_buf()), - true, - Some(vec![path]), - )) - } else { - None - } + path.parent().map(Path::to_path_buf).map(|parent| { + self.open_tab(Location::Path(parent), true, Some(vec![path])) + }) }, )) } Message::OpenWithBrowse => match self.dialog_pages.pop_front() { Some(DialogPage::OpenWith { - mime, store_opt, .. + mime, + store_opt: Some(app), + .. }) => { - if let Some(app) = store_opt { - let url = format!("mime:///{mime}"); - if let Some(mut command) = app.command(Some(url.clone().into())) { - match spawn_detached(&mut command) { - Ok(()) => {} - Err(err) => { - log::warn!( - "failed to open {:?} with {:?}: {}", - url, - app.id, - err - ) - } + let url = format!("mime:///{mime}"); + if let Some(mut command) = app.command(Some(url.clone().into())) { + match spawn_detached(&mut command) { + Ok(()) => {} + Err(err) => { + log::warn!("failed to open {:?} with {:?}: {}", url, app.id, err) } - } else { - log::warn!( - "failed to open {:?} with {:?}: failed to get command", - url, - app.id - ); } + } else { + log::warn!( + "failed to open {:?} with {:?}: failed to get command", + url, + app.id + ); } } Some(dialog_page) => { @@ -2542,7 +2525,7 @@ impl Application for App { Message::PendingCancelAll => { for (id, (_, controller)) in self.pending_operations.iter() { controller.cancel(); - self.progress_operations.remove(&id); + self.progress_operations.remove(id); } } Message::PendingComplete(id, op_sel) => { @@ -2636,12 +2619,14 @@ impl Application for App { let selected_paths = self.selected_paths(entity_opt); let mut commands = Vec::with_capacity(selected_paths.len()); for path in selected_paths { - let mut settings = window::Settings::default(); - settings.decorations = true; - settings.min_size = Some(Size::new(360.0, 180.0)); - settings.resizable = true; - settings.size = Size::new(480.0, 600.0); - settings.transparent = true; + let mut settings = window::Settings { + decorations: true, + min_size: Some(Size::new(360.0, 180.0)), + resizable: true, + size: Size::new(480.0, 600.0), + transparent: true, + ..Default::default() + }; #[cfg(target_os = "linux")] { @@ -2982,17 +2967,14 @@ impl Application for App { return self.open_tab(location, true, None); } Message::TabRescan(entity, location, parent_item_opt, items, selection_paths) => { - match self.tab_model.data_mut::(entity) { - Some(tab) => { - if location == tab.location { - tab.parent_item_opt = parent_item_opt; - tab.set_items(items); - if let Some(selection_paths) = selection_paths { - tab.select_paths(selection_paths); - } + if let Some(tab) = self.tab_model.data_mut::(entity) { + if location == tab.location { + tab.parent_item_opt = parent_item_opt; + tab.set_items(items); + if let Some(selection_paths) = selection_paths { + tab.select_paths(selection_paths); } } - _ => (), } } Message::TabView(entity_opt, view) => { @@ -3275,26 +3257,23 @@ impl Application for App { // Applies selected nav bar context menu operation. Message::NavMenuAction(action) => match action { NavMenuAction::Open(entity) => { - match self + if let Some(path) = self .nav_model .data::(entity) .and_then(|x| x.path_opt()) .map(|x| x.to_path_buf()) { - Some(path) => { - self.open_file(&path); - } - None => {} + self.open_file(&path); } } NavMenuAction::OpenWith(entity) => { - match self + if let Some(path) = self .nav_model .data::(entity) .and_then(|x| x.path_opt()) .map(|x| x.to_path_buf()) { - Some(path) => match tab::item_from_path(&path, IconSizes::default()) { + match tab::item_from_path(&path, IconSizes::default()) { Ok(item) => { return self.update(Message::DialogPush(DialogPage::OpenWith { path: path.to_path_buf(), @@ -3311,8 +3290,7 @@ impl Application for App { Err(err) => { log::warn!("failed to get item for path {:?}: {}", path, err); } - }, - None => {} + } } } NavMenuAction::OpenInNewTab(entity) => { @@ -3329,8 +3307,7 @@ impl Application for App { // Open the selected path in a new cosmic-files window. NavMenuAction::OpenInNewWindow(entity) => { - if let Some(&Location::Path(ref path)) = self.nav_model.data::(entity) - { + if let Some(Location::Path(path)) = self.nav_model.data::(entity) { match env::current_exe() { Ok(exe) => match process::Command::new(&exe).arg(path).spawn() { Ok(_child) => {} @@ -3391,16 +3368,15 @@ impl Application for App { log::info!("output {}: created", output.id()); let surface_id = WindowId::unique(); - match self.surface_ids.insert(output.clone(), surface_id) { - Some(old_surface_id) => { - //TODO: remove old surface? - log::warn!( - "output {}: already had surface ID {:?}", - output.id(), - old_surface_id - ); - } - None => {} + if let Some(old_surface_id) = + self.surface_ids.insert(output.clone(), surface_id) + { + //TODO: remove old surface? + log::warn!( + "output {}: already had surface ID {:?}", + output.id(), + old_surface_id + ); } let display = match output_info_opt { @@ -3470,7 +3446,7 @@ impl Application for App { } Message::Cosmic(cosmic) => { // Forward cosmic messages - return Task::perform(async move { cosmic }, |cosmic| message::cosmic(cosmic)); + return Task::perform(async move { cosmic }, message::cosmic); } Message::None => {} #[cfg(all(feature = "desktop", feature = "wayland"))] @@ -4148,7 +4124,7 @@ impl Application for App { let running = count; // Adjust the progress bar so it does not jump around when operations finish for id in self.progress_operations.iter() { - if self.complete_operations.contains_key(&id) { + if self.complete_operations.contains_key(id) { total_progress += 1.0; count += 1; } @@ -4241,8 +4217,7 @@ impl Application for App { self.tab_model.active_data::(), &self.config, &self.key_binds, - ) - .into()] + )] } fn header_end(&self) -> Vec> { @@ -4371,16 +4346,16 @@ impl Application for App { return if let Some(margin) = self.margin.get(&id) { if margin.0 >= 0. || margin.2 >= 0. { tab_column = widget::column::with_children(vec![ - vertical_space().height(margin.0 as f32).into(), + vertical_space().height(margin.0).into(), tab_column.into(), - vertical_space().height(margin.2 as f32).into(), + vertical_space().height(margin.2).into(), ]) } if margin.1 >= 0. || margin.3 >= 0. { Element::from(widget::row::with_children(vec![ - horizontal_space().width(margin.1 as f32).into(), + horizontal_space().width(margin.1).into(), tab_column.into(), - horizontal_space().width(margin.3 as f32).into(), + horizontal_space().width(margin.3).into(), ])) } else { tab_column.into() @@ -4748,7 +4723,6 @@ pub(crate) mod test_utils { use crate::{ config::{IconSizes, TabConfig}, - mounter::MounterMap, tab::Item, }; @@ -4986,8 +4960,8 @@ pub(crate) mod test_utils { assert!( entries .into_iter() - .zip(tab.items_opt().clone().unwrap_or(&empty)) - .all(|(a, b)| eq_path_item(&a, &b)), + .zip(tab.items_opt().unwrap_or(&empty)) + .all(|(a, b)| eq_path_item(&a, b)), "Path ({}) and Tab path ({}) don't have equal contents", path.display(), tab_path.display() diff --git a/src/mime_app.rs b/src/mime_app.rs index 5e2556b..ca6588f 100644 --- a/src/mime_app.rs +++ b/src/mime_app.rs @@ -278,12 +278,12 @@ impl MimeAppCache { pub fn get(&self, key: &Mime) -> &[MimeApp] { static EMPTY: Vec = Vec::new(); - self.cache.get(key).unwrap_or_else(|| &EMPTY) + self.cache.get(key).unwrap_or(&EMPTY) } pub fn icons(&self, key: &Mime) -> &[widget::icon::Handle] { static EMPTY: Vec = Vec::new(); - self.icons.get(key).unwrap_or_else(|| &EMPTY) + self.icons.get(key).unwrap_or(&EMPTY) } pub fn terminal(&self) -> Option<&MimeApp> { @@ -348,3 +348,9 @@ impl MimeAppCache { } } } + +impl Default for MimeAppCache { + fn default() -> Self { + Self::new() + } +}