From c56bd12e4619140ec4c2b12f8fd62ed02475f0d3 Mon Sep 17 00:00:00 2001 From: vicrodh <103399283+vicrodh@users.noreply.github.com> Date: Wed, 8 Apr 2026 12:20:07 -0600 Subject: [PATCH] refactor(gapless): flatten local library check from 4 nested ifs Chain Option combinators to reduce nesting. The await boundary requires one outer if-let, but the rest flattens into a single and_then/map/filter pipeline. cc --- src-tauri/src/commands_v2.rs | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/src-tauri/src/commands_v2.rs b/src-tauri/src/commands_v2.rs index a44ab6861..de749de36 100644 --- a/src-tauri/src/commands_v2.rs +++ b/src-tauri/src/commands_v2.rs @@ -7982,23 +7982,22 @@ pub async fn v2_play_next_gapless( } // Check local library - if let Ok(track_id_i64) = track_id.try_into() { - if let Ok(tracks) = - v2_library_get_tracks_by_ids(vec![track_id_i64], library_state.clone()) - .await - { - if let Some(local_track) = tracks.into_iter().next() { - let path = std::path::Path::new(&local_track.file_path); - if path.exists() { - log::info!("[V2/GAPLESS] Track {} from LOCAL library", track_id); - let audio_data = std::fs::read(path) - .map_err(|e| RuntimeError::Internal(format!("Failed to read local file: {}", e)))?; - bridge.get().await.player() - .play_next(audio_data, track_id) - .map_err(RuntimeError::Internal)?; - return Ok(true); - } - } + if let Ok(track_id_i64) = i64::try_from(track_id) { + let local_path = v2_library_get_tracks_by_ids(vec![track_id_i64], library_state.clone()) + .await + .ok() + .and_then(|mut tracks| tracks.pop()) + .map(|track| std::path::PathBuf::from(track.file_path)) + .filter(|p| p.exists()); + + if let Some(path) = local_path { + log::info!("[V2/GAPLESS] Track {} from LOCAL library", track_id); + let audio_data = std::fs::read(&path) + .map_err(|e| RuntimeError::Internal(format!("Failed to read local file: {}", e)))?; + bridge.get().await.player() + .play_next(audio_data, track_id) + .map_err(RuntimeError::Internal)?; + return Ok(true); } }