From 9d4e24e8099a2cffa6df0c1c87c724c629f65838 Mon Sep 17 00:00:00 2001 From: electria Date: Fri, 31 Jul 2026 14:36:36 -0700 Subject: [PATCH] refactor: apply clippy lints cargo clippy --no-deps -- --deny clippy::nursery --deny clippy::pedantic (does not quite pass due to unused `&self` in State::subscription) I am considering ignoring events when the file picker is opened, but that isn't a very nice solution (should just make it async atp) --- src/main.rs | 95 ++++++++++++++++++++++++++-------------------------- src/utils.rs | 8 ++--- 2 files changed, 51 insertions(+), 52 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5ad2bf3..c951b31 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,7 +29,7 @@ struct State { } impl State { fn new() -> (Self, Task) { - let mut state = State::default(); + let mut state = Self::default(); if let Some(path) = env::args().nth(1) { match state.load_image(path) { @@ -43,22 +43,25 @@ impl State { fn view(&self) -> Element<'_, Message, Theme, Renderer> { let mut main = Vec::new(); - main.push(if let Some(allocation) = self.image_display.as_ref() { - widget::image::viewer(allocation.handle().clone()) - .filter_method(self.image_filter) - .max_scale(50.) - .min_scale(1.) - .width(Length::Fill) - .height(Length::Fill) - .into() - } else { - widget::container(widget::text(include_str!("usage.txt"))) - .height(Length::Fill) - .width(Length::Fill) - .align_x(Alignment::Center) - .align_y(Alignment::Center) - .into() - }); + main.push(self.image_display.as_ref().map_or_else( + || { + widget::container(widget::text(include_str!("usage.txt"))) + .height(Length::Fill) + .width(Length::Fill) + .align_x(Alignment::Center) + .align_y(Alignment::Center) + .into() + }, + |allocation| { + widget::image::viewer(allocation.handle().clone()) + .filter_method(self.image_filter) + .max_scale(50.) + .min_scale(1.) + .width(Length::Fill) + .height(Length::Fill) + .into() + }, + )); if let Some(error) = self.error.as_ref() { main.push(widget::text(error).style(widget::text::danger).into()); @@ -84,9 +87,8 @@ impl State { Key::Named(key::Named::Tab) => { if modifiers.shift() { return widget::operation::focus_previous(); - } else { - return widget::operation::focus_next(); } + return widget::operation::focus_next(); } Key::Character("o") => match self.pick_and_load_image() { @@ -97,26 +99,22 @@ impl State { Err(e) => self.error = Some(e), }, - Key::Character("r") => { - match self.image.as_ref() { - None => self.error = Some("no image to rotate".into()), - Some(image) => { - self.error = None; - self.image = Some(imageops::rotate90(image)); - return self.allocate_image(); - } - }; - } - Key::Character("i") => { - match self.image.as_mut() { - None => self.error = Some("no image to invert".into()), - Some(image) => { - self.error = None; - imageops::invert(image); - return self.allocate_image(); - } - }; - } + Key::Character("r") => match self.image.as_ref() { + None => self.error = Some("no image to rotate".into()), + Some(image) => { + self.error = None; + self.image = Some(imageops::rotate90(image)); + return self.allocate_image(); + } + }, + Key::Character("i") => match self.image.as_mut() { + None => self.error = Some("no image to invert".into()), + Some(image) => { + self.error = None; + imageops::invert(image); + return self.allocate_image(); + } + }, Key::Character("f") => { self.image_filter = match self.image_filter { widget::image::FilterMethod::Linear => widget::image::FilterMethod::Nearest, @@ -160,8 +158,7 @@ impl State { "{} {}x{}", path.as_ref() .file_name() - .map(OsStr::to_string_lossy) - .unwrap_or(Cow::Borrowed("[no file]")), + .map_or(Cow::Borrowed("[no file]"), OsStr::to_string_lossy), image.width(), image.height(), )); @@ -210,13 +207,15 @@ impl State { event::listen().map(Message::Event) } fn title(&self) -> String { - match self.info.as_ref() { - Some(info) => format!( - "imagey {info} {}", - utils::string_from_filter_type(self.image_filter), - ), - None => "imagey".into(), - } + self.info.as_ref().map_or_else( + || "imagey".into(), + |info| { + format!( + "imagey {info} {}", + utils::string_from_filter_type(self.image_filter), + ) + }, + ) } } diff --git a/src/utils.rs b/src/utils.rs index dafd1f2..cc523c4 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -5,9 +5,9 @@ use image::{DynamicImage, ImageDecoder, ImageReader, ImageResult, RgbaImage}; use rfd::FileDialog; pub fn load_image(path: impl AsRef) -> Result { - _load_image(path).map_err(|e| e.to_string()) + load_image_impl(path).map_err(|e| e.to_string()) } -fn _load_image(path: impl AsRef) -> ImageResult { +fn load_image_impl(path: impl AsRef) -> ImageResult { let mut decoder = ImageReader::open(path)? .with_guessed_format()? .into_decoder()?; @@ -48,12 +48,12 @@ pub fn save_image(image: Option<&RgbaImage>) -> Result<(), String> { if let Err(e) = image.save(&path) { return Err(e.to_string()); - }; + } Ok(()) } -pub fn string_from_filter_type(f: widget::image::FilterMethod) -> &'static str { +pub const fn string_from_filter_type(f: widget::image::FilterMethod) -> &'static str { match f { widget::image::FilterMethod::Linear => "bilinear", widget::image::FilterMethod::Nearest => "nearest neighbor",