refactor: collapse nested if let

This commit is contained in:
Henrik Friedrichsen
2025-08-15 21:49:49 +02:00
parent 72c96df79e
commit 1f2c85f544
11 changed files with 37 additions and 57 deletions

View File

@@ -281,8 +281,8 @@ impl CommandManager {
Ok(None) Ok(None)
} }
Command::AddCurrent => { Command::AddCurrent => {
if let Some(track) = self.queue.get_current() { if let Some(track) = self.queue.get_current()
if let Some(track) = track.track() { && let Some(track) = track.track() {
let dialog = ContextMenu::add_track_dialog( let dialog = ContextMenu::add_track_dialog(
self.library.clone(), self.library.clone(),
self.queue.get_spotify(), self.queue.get_spotify(),
@@ -290,7 +290,6 @@ impl CommandManager {
); );
s.add_layer(dialog); s.add_layer(dialog);
} }
}
Ok(None) Ok(None)
} }
Command::SaveCurrent => { Command::SaveCurrent => {

View File

@@ -156,15 +156,14 @@ impl Library {
.iter() .iter()
.position(|i| i.id == id); .position(|i| i.id == id);
if let Some(position) = position { if let Some(position) = position
if self.spotify.api.delete_playlist(id).is_ok() { && self.spotify.api.delete_playlist(id).is_ok() {
self.playlists.write().unwrap().remove(position); self.playlists.write().unwrap().remove(position);
self.save_cache( self.save_cache(
&config::cache_path(CACHE_PLAYLISTS), &config::cache_path(CACHE_PLAYLISTS),
&self.playlists.read().unwrap(), &self.playlists.read().unwrap(),
); );
} }
}
} }
/// Set the playlist with `id` to contain only `tracks`. If the playlist already contains /// Set the playlist with `id` to contain only `tracks`. If the playlist already contains
@@ -694,8 +693,8 @@ impl Library {
return; return;
} }
if let Some(ref album_id) = album.id { if let Some(ref album_id) = album.id
if self && self
.spotify .spotify
.api .api
.current_user_saved_albums_add(vec![album_id.as_str()]) .current_user_saved_albums_add(vec![album_id.as_str()])
@@ -703,7 +702,6 @@ impl Library {
{ {
return; return;
} }
}
{ {
let mut store = self.albums.write().unwrap(); let mut store = self.albums.write().unwrap();
@@ -727,8 +725,8 @@ impl Library {
return; return;
} }
if let Some(ref album_id) = album.id { if let Some(ref album_id) = album.id
if self && self
.spotify .spotify
.api .api
.current_user_saved_albums_delete(vec![album_id.as_str()]) .current_user_saved_albums_delete(vec![album_id.as_str()])
@@ -736,7 +734,6 @@ impl Library {
{ {
return; return;
} }
}
{ {
let mut store = self.albums.write().unwrap(); let mut store = self.albums.write().unwrap();
@@ -765,8 +762,8 @@ impl Library {
return; return;
} }
if let Some(ref artist_id) = artist.id { if let Some(ref artist_id) = artist.id
if self && self
.spotify .spotify
.api .api
.user_follow_artists(vec![artist_id.as_str()]) .user_follow_artists(vec![artist_id.as_str()])
@@ -774,7 +771,6 @@ impl Library {
{ {
return; return;
} }
}
{ {
let mut store = self.artists.write().unwrap(); let mut store = self.artists.write().unwrap();
@@ -801,8 +797,8 @@ impl Library {
return; return;
} }
if let Some(ref artist_id) = artist.id { if let Some(ref artist_id) = artist.id
if self && self
.spotify .spotify
.api .api
.user_unfollow_artists(vec![artist_id.as_str()]) .user_unfollow_artists(vec![artist_id.as_str()])
@@ -810,7 +806,6 @@ impl Library {
{ {
return; return;
} }
}
{ {
let mut store = self.artists.write().unwrap(); let mut store = self.artists.write().unwrap();

View File

@@ -33,11 +33,10 @@ impl Artist {
} }
fn load_top_tracks(&mut self, spotify: Spotify) { fn load_top_tracks(&mut self, spotify: Spotify) {
if let Some(artist_id) = &self.id { if let Some(artist_id) = &self.id
if self.tracks.is_none() { && self.tracks.is_none() {
self.tracks = spotify.api.artist_top_tracks(artist_id).ok(); self.tracks = spotify.api.artist_top_tracks(artist_id).ok();
} }
}
} }
} }

View File

@@ -88,12 +88,10 @@ impl Playlist {
.api .api
.append_tracks(&self.id, new_tracks, None) .append_tracks(&self.id, new_tracks, None)
.is_ok() .is_ok()
{ && let Some(tracks) = &mut self.tracks {
if let Some(tracks) = &mut self.tracks {
tracks.append(&mut new_tracks.to_vec()); tracks.append(&mut new_tracks.to_vec());
has_modified = true; has_modified = true;
} }
}
if has_modified { if has_modified {
library.playlist_update(self); library.playlist_update(self);

View File

@@ -393,8 +393,8 @@ impl MprisPlayer {
let uri_type = spotify_url.map(|s| s.uri_type); let uri_type = spotify_url.map(|s| s.uri_type);
match uri_type { match uri_type {
Some(UriType::Album) => { Some(UriType::Album) => {
if let Ok(a) = self.spotify.api.album(&id) { if let Ok(a) = self.spotify.api.album(&id)
if let Some(t) = &Album::from(&a).tracks { && let Some(t) = &Album::from(&a).tracks {
let should_shuffle = self.queue.get_shuffle(); let should_shuffle = self.queue.get_shuffle();
self.queue.clear(); self.queue.clear();
let index = self.queue.append_next( let index = self.queue.append_next(
@@ -404,7 +404,6 @@ impl MprisPlayer {
); );
self.queue.play(index, should_shuffle, should_shuffle) self.queue.play(index, should_shuffle, should_shuffle)
} }
}
} }
Some(UriType::Track) => { Some(UriType::Track) => {
if let Ok(t) = self.spotify.api.track(&id) { if let Ok(t) = self.spotify.api.track(&id) {

View File

@@ -359,11 +359,10 @@ impl Queue {
let repeat = self.cfg.state().repeat; let repeat = self.cfg.state().repeat;
if repeat == RepeatSetting::RepeatTrack && !manual { if repeat == RepeatSetting::RepeatTrack && !manual {
if let Some(index) = current { if let Some(index) = current
if q[index].is_playable() { && q[index].is_playable() {
self.play(index, false, false); self.play(index, false, false);
} }
}
} else if let Some(index) = self.next_index() { } else if let Some(index) = self.next_index() {
self.play(index, false, false); self.play(index, false, false);
if repeat == RepeatSetting::RepeatTrack && manual { if repeat == RepeatSetting::RepeatTrack && manual {
@@ -492,11 +491,10 @@ pub fn send_notification(summary_txt: &str, body_txt: &str, cover_url: Option<St
// album cover image // album cover image
if let Some(u) = cover_url { if let Some(u) = cover_url {
let path = crate::utils::cache_path_for_url(u.to_string()); let path = crate::utils::cache_path_for_url(u.to_string());
if !path.exists() { if !path.exists()
if let Err(e) = crate::utils::download(u, path.clone()) { && let Err(e) = crate::utils::download(u, path.clone()) {
log::error!("Failed to download cover: {e}"); log::error!("Failed to download cover: {e}");
} }
}
n.icon(path.to_str().unwrap()); n.icon(path.to_str().unwrap());
} }

View File

@@ -37,12 +37,11 @@ impl ArtistView {
let id = artist.id.clone(); let id = artist.id.clone();
let library = library.clone(); let library = library.clone();
thread::spawn(move || { thread::spawn(move || {
if let Some(id) = id { if let Some(id) = id
if let Ok(tracks) = spotify.api.artist_top_tracks(&id) { && let Ok(tracks) = spotify.api.artist_top_tracks(&id) {
top_tracks.write().unwrap().extend(tracks); top_tracks.write().unwrap().extend(tracks);
library.trigger_redraw(); library.trigger_redraw();
} }
}
}); });
} }
@@ -52,12 +51,11 @@ impl ArtistView {
let id = artist.id.clone(); let id = artist.id.clone();
let library = library.clone(); let library = library.clone();
thread::spawn(move || { thread::spawn(move || {
if let Some(id) = id { if let Some(id) = id
if let Ok(artists) = spotify.api.artist_related_artists(&id) { && let Ok(artists) = spotify.api.artist_related_artists(&id) {
related.write().unwrap().extend(artists); related.write().unwrap().extend(artists);
library.trigger_redraw(); library.trigger_redraw();
} }
}
}); });
} }

View File

@@ -277,8 +277,8 @@ impl ContextMenu {
); );
} }
if let Some(ref a) = album { if let Some(ref a) = album
if let Some(savestatus) = a.is_saved(&library) { && let Some(savestatus) = a.is_saved(&library) {
content.add_item( content.add_item(
match savestatus { match savestatus {
true => "Unsave album", true => "Unsave album",
@@ -287,7 +287,6 @@ impl ContextMenu {
ContextMenuAction::ToggleSavedStatus(a.as_listitem()), ContextMenuAction::ToggleSavedStatus(a.as_listitem()),
); );
} }
}
// open detail view of artist/album // open detail view of artist/album
{ {

View File

@@ -165,11 +165,10 @@ impl Layout {
} }
fn get_result(&self) -> Result<Option<String>, String> { fn get_result(&self) -> Result<Option<String>, String> {
if let Some(t) = self.result_time { if let Some(t) = self.result_time
if t.elapsed().unwrap() > Duration::from_secs(5) { && t.elapsed().unwrap() > Duration::from_secs(5) {
return Ok(None); return Ok(None);
} }
}
self.result.clone() self.result.clone()
} }
@@ -468,11 +467,10 @@ impl ViewExt for Layout {
// Clear search results and return to search bar // Clear search results and return to search bar
// If trying to focus search screen while already on it // If trying to focus search screen while already on it
let search_view_name = "search"; let search_view_name = "search";
if view == search_view_name && self.focus == Some(search_view_name.into()) { if view == search_view_name && self.focus == Some(search_view_name.into())
if let Some(stack) = self.stack.get_mut(search_view_name) { && let Some(stack) = self.stack.get_mut(search_view_name) {
stack.clear(); stack.clear();
} }
}
if self.screens.keys().any(|k| k == view) { if self.screens.keys().any(|k| k == view) {
self.set_screen(view.clone()); self.set_screen(view.clone());

View File

@@ -275,8 +275,8 @@ impl<I: ListItem + Clone> ListView<I> {
let clicked_list_item = let clicked_list_item =
content.get(self.selected).map(ListItem::as_listitem); content.get(self.selected).map(ListItem::as_listitem);
if let Some(target) = clicked_list_item { if let Some(target) = clicked_list_item
if let Some(view) = && let Some(view) =
target.open(self.queue.clone(), self.library.clone()) target.open(self.queue.clone(), self.library.clone())
{ {
return MouseHandleResult::Handled(EventResult::Consumed(Some( return MouseHandleResult::Handled(EventResult::Consumed(Some(
@@ -285,7 +285,6 @@ impl<I: ListItem + Clone> ListView<I> {
}), }),
))); )));
} }
}
} }
} }
} }
@@ -544,8 +543,8 @@ impl<I: ListItem + Clone> ViewExt for ListView<I> {
content.get(self.selected).cloned() content.get(self.selected).cloned()
}; };
if let Some(track) = item { if let Some(track) = item
if let Some(track) = track.track() { && let Some(track) = track.track() {
let dialog = ContextMenu::add_track_dialog( let dialog = ContextMenu::add_track_dialog(
self.library.clone(), self.library.clone(),
self.queue.get_spotify(), self.queue.get_spotify(),
@@ -553,7 +552,6 @@ impl<I: ListItem + Clone> ViewExt for ListView<I> {
); );
return Ok(CommandResult::Modal(Box::new(dialog))); return Ok(CommandResult::Modal(Box::new(dialog)));
} }
}
return Ok(CommandResult::Consumed(None)); return Ok(CommandResult::Consumed(None));
} }

View File

@@ -226,13 +226,12 @@ impl View for StatusBar {
self.spotify.seek_relative(500); self.spotify.seek_relative(500);
} }
if event == MouseEvent::Press(MouseButton::Left) { if event == MouseEvent::Press(MouseButton::Left)
if let Some(playable) = self.queue.get_current() { && let Some(playable) = self.queue.get_current() {
let f: f32 = position.x as f32 / self.last_size.x as f32; let f: f32 = position.x as f32 / self.last_size.x as f32;
let new = playable.duration() as f32 * f; let new = playable.duration() as f32 * f;
self.spotify.seek(new as u32); self.spotify.seek(new as u32);
} }
}
} else if self.last_size.x - position.x < volume_len { } else if self.last_size.x - position.x < volume_len {
if event == MouseEvent::WheelUp { if event == MouseEvent::WheelUp {
let volume = self let volume = self