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

View File

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

View File

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

View File

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

View File

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

View File

@@ -359,11 +359,10 @@ impl Queue {
let repeat = self.cfg.state().repeat;
if repeat == RepeatSetting::RepeatTrack && !manual {
if let Some(index) = current {
if q[index].is_playable() {
if let Some(index) = current
&& q[index].is_playable() {
self.play(index, false, false);
}
}
} else if let Some(index) = self.next_index() {
self.play(index, false, false);
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
if let Some(u) = cover_url {
let path = crate::utils::cache_path_for_url(u.to_string());
if !path.exists() {
if let Err(e) = crate::utils::download(u, path.clone()) {
if !path.exists()
&& let Err(e) = crate::utils::download(u, path.clone()) {
log::error!("Failed to download cover: {e}");
}
}
n.icon(path.to_str().unwrap());
}

View File

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

View File

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

View File

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

View File

@@ -275,8 +275,8 @@ impl<I: ListItem + Clone> ListView<I> {
let clicked_list_item =
content.get(self.selected).map(ListItem::as_listitem);
if let Some(target) = clicked_list_item {
if let Some(view) =
if let Some(target) = clicked_list_item
&& let Some(view) =
target.open(self.queue.clone(), self.library.clone())
{
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()
};
if let Some(track) = item {
if let Some(track) = track.track() {
if let Some(track) = item
&& let Some(track) = track.track() {
let dialog = ContextMenu::add_track_dialog(
self.library.clone(),
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::Consumed(None));
}

View File

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