refactor(gui): replace unsafe unwrap with idiomatic if let Some in input handling (#22)

This commit addresses a code health issue in `src/gui/input.rs` where an `.is_some()` check was followed by an unsafe `.unwrap()` on `self.app_state.selected_file`.

The logic has been updated to use the idiomatic `if let Some(path) = self.app_state.selected_file.clone()` pattern. The `.clone()` is necessary because the subsequent methods (`self.play_file` and `self.stop`) require a mutable borrow (`&mut self`), which would conflict with an immutable borrow of `self.app_state.selected_file`. This change ensures the code is safe and panic-free while satisfying Rust's borrow checker rules. Behavior remains unchanged.

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
This commit is contained in:
Tarasov Aleksandr
2026-03-08 00:24:25 +03:00
committed by GitHub
parent c649ef5410
commit 39648f7781
+12 -11
View File
@@ -57,17 +57,18 @@ impl SoundpadGui {
} }
// Play selected file on Enter // Play selected file on Enter
if self.key_pressed(ctx, Key::Enter) && self.app_state.selected_file.is_some() { if self.key_pressed(ctx, Key::Enter) {
let path = &self.app_state.selected_file.clone().unwrap(); if let Some(path) = self.app_state.selected_file.clone() {
if modifiers.ctrl { if modifiers.ctrl {
self.play_file(path, true); self.play_file(&path, true);
} else if modifiers.shift } else if modifiers.shift
&& let Some(last_track) = self.audio_player_state.tracks.last() && let Some(last_track) = self.audio_player_state.tracks.last()
{ {
self.stop(Some(last_track.id)); self.stop(Some(last_track.id));
self.play_file(path, true); self.play_file(&path, true);
} else { } else {
self.play_file(path, false); self.play_file(&path, false);
}
} }
} }