From 2c0e68f39296e5ab180e28d90c3076cf71dfc080 Mon Sep 17 00:00:00 2001 From: Benedikt Peetz Date: Fri, 18 Jul 2025 18:21:36 +0200 Subject: fix(crates/yt/db/insert/playlist): Account for playlist_len == 0 Previously, we always tried to mark a new video as next to focus, but that is obviously impossible with an empty playlist. --- crates/yt/src/storage/db/insert/playlist.rs | 48 ++++++++++++++++------------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/crates/yt/src/storage/db/insert/playlist.rs b/crates/yt/src/storage/db/insert/playlist.rs index dc474ce..2287db9 100644 --- a/crates/yt/src/storage/db/insert/playlist.rs +++ b/crates/yt/src/storage/db/insert/playlist.rs @@ -51,30 +51,34 @@ impl Playlist { let index = usize::from(current_index); let playlist_length = self.len(); - let index = match index.cmp(&playlist_length) { - Ordering::Greater => { - unreachable!( - "The index '{index}' cannot exceed the \ + if playlist_length == 0 { + // There are no new videos to mark focused. + } else { + let index = match index.cmp(&playlist_length) { + Ordering::Greater => { + unreachable!( + "The index '{index}' cannot exceed the \ playlist length '{playlist_length}' as indices are 0 based." - ); - } - Ordering::Less => { - // The index is still valid. - // Therefore, we keep the user at this position. - index - } - Ordering::Equal => { - // The index is pointing to the end of the playlist. We could either go the second - // to last entry (i.e., one entry back) or wrap around to the start. - // We wrap around. - 0 - } - }; + ); + } + Ordering::Less => { + // The index is still valid. + // Therefore, we keep the user at this position. + index + } + Ordering::Equal => { + // The index is pointing to the end of the playlist. We could either go the second + // to last entry (i.e., one entry back) or wrap around to the start. + // We wrap around. + 0 + } + }; - let next = self - .get_mut(PlaylistIndex::from(index)) - .expect("We checked that the index is still good"); - next.set_focused(true, ops); + let next = self + .get_mut(PlaylistIndex::from(index)) + .expect("We checked that the index is still good"); + next.set_focused(true, ops); + } // Tell mpv about our decision. self.resync_with_mpv(app, mpv)?; -- cgit 1.4.1