From c89fa38d8926a5de41bd2fec91a4f0b1899326e2 Mon Sep 17 00:00:00 2001 From: Wojciech Kozlowski Date: Thu, 13 Apr 2023 14:08:31 +0200 Subject: [PATCH] lints --- src/library/mod.rs | 2 +- src/tui/app.rs | 32 ++++++++++---------------------- src/tui/event.rs | 22 +++++++++++----------- src/tui/handler.rs | 5 ++++- src/tui/listener.rs | 4 ++-- src/tui/mod.rs | 32 ++++++++++++++++---------------- 6 files changed, 44 insertions(+), 53 deletions(-) diff --git a/src/library/mod.rs b/src/library/mod.rs index 8136c22..3ade40e 100644 --- a/src/library/mod.rs +++ b/src/library/mod.rs @@ -1,6 +1,6 @@ //! Module for interacting with the music library. -use std::{num::ParseIntError, str::Utf8Error, fmt}; +use std::{fmt, num::ParseIntError, str::Utf8Error}; #[cfg(test)] use mockall::automock; diff --git a/src/tui/app.rs b/src/tui/app.rs index 95b1dd6..3dccf0c 100644 --- a/src/tui/app.rs +++ b/src/tui/app.rs @@ -22,7 +22,7 @@ impl TrackSelection { } } - fn increment(&mut self, tracks: &Vec) { + fn increment(&mut self, tracks: &[Track]) { if let Some(result) = self.index.checked_add(1) { if result < tracks.len() { self.index = result; @@ -30,7 +30,7 @@ impl TrackSelection { } } - fn decrement(&mut self, _tracks: &Vec) { + fn decrement(&mut self, _tracks: &[Track]) { if let Some(result) = self.index.checked_sub(1) { self.index = result; } @@ -54,7 +54,7 @@ impl AlbumSelection { } } - fn increment(&mut self, albums: &Vec) { + fn increment(&mut self, albums: &[Album]) { if let Some(result) = self.index.checked_add(1) { if result < albums.len() { self.index = result; @@ -63,7 +63,7 @@ impl AlbumSelection { } } - fn decrement(&mut self, albums: &Vec) { + fn decrement(&mut self, albums: &[Album]) { if let Some(result) = self.index.checked_sub(1) { self.index = result; self.track = TrackSelection::initialise(&albums[self.index].tracks); @@ -88,7 +88,7 @@ impl ArtistSelection { } } - fn increment(&mut self, artists: &Vec) { + fn increment(&mut self, artists: &[Artist]) { if let Some(result) = self.index.checked_add(1) { if result < artists.len() { self.index = result; @@ -97,7 +97,7 @@ impl ArtistSelection { } } - fn decrement(&mut self, artists: &Vec) { + fn decrement(&mut self, artists: &[Artist]) { if let Some(result) = self.index.checked_sub(1) { self.index = result; self.album = AlbumSelection::initialise(&artists[self.index].albums); @@ -231,7 +231,7 @@ impl App { } fn get_artists(&self) -> &Vec { - &self.collection_manager.get_collection() + self.collection_manager.get_collection() } fn get_albums(&self) -> Option<&Vec> { @@ -277,20 +277,12 @@ impl App { } pub fn selected_artist(&self) -> Option { - if let Some(ref artist_selection) = self.selection.artist { - Some(artist_selection.index) - } else { - None - } + self.selection.artist.as_ref().map(|s| s.index) } pub fn selected_album(&self) -> Option { if let Some(ref artist_selection) = self.selection.artist { - if let Some(ref album_selection) = artist_selection.album { - Some(album_selection.index) - } else { - None - } + artist_selection.album.as_ref().map(|s| s.index) } else { None } @@ -299,11 +291,7 @@ impl App { pub fn selected_track(&self) -> Option { if let Some(ref artist_selection) = self.selection.artist { if let Some(ref album_selection) = artist_selection.album { - if let Some(ref track_selection) = album_selection.track { - Some(track_selection.index) - } else { - None - } + album_selection.track.as_ref().map(|s| s.index) } else { None } diff --git a/src/tui/event.rs b/src/tui/event.rs index e491b2f..0cb188f 100644 --- a/src/tui/event.rs +++ b/src/tui/event.rs @@ -4,17 +4,17 @@ use std::sync::mpsc; #[derive(Debug)] pub enum EventError { - SendError(Event), - RecvError, - IoError(std::io::Error), + Send(Event), + Recv, + Io(std::io::Error), } impl fmt::Display for EventError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { - Self::SendError(ref e) => write!(f, "failed to send event: {e:?}"), - Self::RecvError => write!(f, "receive event call failed"), - Self::IoError(ref e) => { + Self::Send(ref e) => write!(f, "failed to send event: {e:?}"), + Self::Recv => write!(f, "receive event call failed"), + Self::Io(ref e) => { write!(f, "an I/O error was triggered during event handling: {e}") } } @@ -23,13 +23,13 @@ impl fmt::Display for EventError { impl From> for EventError { fn from(err: mpsc::SendError) -> EventError { - EventError::SendError(err.0) + EventError::Send(err.0) } } impl From for EventError { fn from(_: mpsc::RecvError) -> EventError { - EventError::RecvError + EventError::Recv } } @@ -125,14 +125,14 @@ mod tests { #[test] fn errors() { - let send_err = EventError::SendError(Event::Key(KeyEvent { + let send_err = EventError::Send(Event::Key(KeyEvent { code: KeyCode::Up, modifiers: KeyModifiers::empty(), kind: KeyEventKind::Press, state: KeyEventState::NONE, })); - let recv_err = EventError::RecvError; - let io_err = EventError::IoError(io::Error::new(io::ErrorKind::Interrupted, "interrupted")); + let recv_err = EventError::Recv; + let io_err = EventError::Io(io::Error::new(io::ErrorKind::Interrupted, "interrupted")); assert!(!send_err.to_string().is_empty()); assert!(!recv_err.to_string().is_empty()); diff --git a/src/tui/handler.rs b/src/tui/handler.rs index 0a3d4fc..8de4cb7 100644 --- a/src/tui/handler.rs +++ b/src/tui/handler.rs @@ -3,7 +3,10 @@ use crossterm::event::{KeyCode, KeyEvent, KeyModifiers}; #[cfg(test)] use mockall::automock; -use super::{app::App, event::{Event, EventReceiver, EventError}}; +use super::{ + app::App, + event::{Event, EventError, EventReceiver}, +}; #[cfg_attr(test, automock)] pub trait EventHandler { diff --git a/src/tui/listener.rs b/src/tui/listener.rs index c66ef52..53c78da 100644 --- a/src/tui/listener.rs +++ b/src/tui/listener.rs @@ -4,7 +4,7 @@ use std::thread; #[cfg(test)] use mockall::automock; -use super::event::{EventSender, EventError, Event}; +use super::event::{Event, EventError, EventSender}; #[cfg_attr(test, automock)] pub trait EventListener { @@ -40,7 +40,7 @@ impl EventListener for TuiEventListener { return err; } } - Err(err) => return EventError::IoError(err), + Err(err) => return EventError::Io(err), }; } }) diff --git a/src/tui/mod.rs b/src/tui/mod.rs index 81fc958..b3c3313 100644 --- a/src/tui/mod.rs +++ b/src/tui/mod.rs @@ -19,27 +19,27 @@ use self::ui::Ui; #[derive(Debug, PartialEq, Eq)] pub enum Error { - CollectionError(String), - IoError(String), - EventError(String), + Collection(String), + Io(String), + Event(String), ListenerPanic, } impl From for Error { fn from(err: collection::Error) -> Error { - Error::CollectionError(err.to_string()) + Error::Collection(err.to_string()) } } impl From for Error { fn from(err: io::Error) -> Error { - Error::IoError(err.to_string()) + Error::Io(err.to_string()) } } impl From for Error { fn from(err: EventError) -> Error { - Error::EventError(err.to_string()) + Error::Event(err.to_string()) } } @@ -159,7 +159,7 @@ impl Tui { mod tests { use std::{io, thread}; - use musichoard::collection::{Collection, self}; + use musichoard::collection::{self, Collection}; use ratatui::{backend::TestBackend, Terminal}; use crate::tests::{MockCollectionManager, COLLECTION}; @@ -192,7 +192,7 @@ mod tests { listener.expect_spawn().return_once(|| { thread::spawn(|| { thread::park(); - return EventError::IoError(io::Error::new(io::ErrorKind::Interrupted, "unparked")); + return EventError::Io(io::Error::new(io::ErrorKind::Interrupted, "unparked")); }) }); listener @@ -231,13 +231,13 @@ mod tests { let mut handler = MockEventHandler::new(); handler .expect_handle_next_event() - .return_once(|_| Err(EventError::RecvError)); + .return_once(|_| Err(EventError::Recv)); let result = Tui::main(terminal, app, ui, handler, listener); assert!(result.is_err()); assert_eq!( result.unwrap_err(), - Error::EventError(EventError::RecvError.to_string()) + Error::Event(EventError::Recv.to_string()) ); } @@ -247,7 +247,7 @@ mod tests { let app = app(COLLECTION.to_owned()); let ui = Ui::new(); - let error = EventError::IoError(io::Error::new(io::ErrorKind::Interrupted, "error")); + let error = EventError::Io(io::Error::new(io::ErrorKind::Interrupted, "error")); let listener_handle: thread::JoinHandle = thread::spawn(|| error); while !listener_handle.is_finished() {} @@ -257,13 +257,13 @@ mod tests { let mut handler = MockEventHandler::new(); handler .expect_handle_next_event() - .return_once(|_| Err(EventError::RecvError)); + .return_once(|_| Err(EventError::Recv)); let result = Tui::main(terminal, app, ui, handler, listener); assert!(result.is_err()); - let error = EventError::IoError(io::Error::new(io::ErrorKind::Interrupted, "error")); - assert_eq!(result.unwrap_err(), Error::EventError(error.to_string())); + let error = EventError::Io(io::Error::new(io::ErrorKind::Interrupted, "error")); + assert_eq!(result.unwrap_err(), Error::Event(error.to_string())); } #[test] @@ -281,7 +281,7 @@ mod tests { let mut handler = MockEventHandler::new(); handler .expect_handle_next_event() - .return_once(|_| Err(EventError::RecvError)); + .return_once(|_| Err(EventError::Recv)); let result = Tui::main(terminal, app, ui, handler, listener); assert!(result.is_err()); @@ -292,7 +292,7 @@ mod tests { fn errors() { let collection_err: Error = collection::Error::DatabaseError(String::from("")).into(); let io_err: Error = io::Error::new(io::ErrorKind::Interrupted, "error").into(); - let event_err: Error = EventError::RecvError.into(); + let event_err: Error = EventError::Recv.into(); let listener_err = Error::ListenerPanic; assert!(!format!("{:?}", collection_err).is_empty());