Skip to content

Commit cb24938

Browse files
fix(rust): resolve clippy linting warnings
- Replace min().max() with clamp() for cleaner bounds checking - Implement FromStr trait for LibrarySortColumn instead of custom from_str - Use matches! macro instead of match expression for boolean returns - Allow clippy::too_many_arguments for Tauri command (unavoidable) - Update tests to use FromStr trait implementation - Remove unused import Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent fa24f06 commit cb24938

4 files changed

Lines changed: 27 additions & 30 deletions

File tree

src-tauri/src/commands/favorites.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub fn favorites_get(
5151
limit: Option<i64>,
5252
offset: Option<i64>,
5353
) -> Result<FavoritesResponse, String> {
54-
let limit = limit.unwrap_or(100).min(1000).max(1);
54+
let limit = limit.unwrap_or(100).clamp(1, 1000);
5555
let offset = offset.unwrap_or(0).max(0);
5656

5757
let conn = db.conn().map_err(|e| e.to_string())?;
@@ -146,8 +146,8 @@ pub fn favorites_get_recently_played(
146146
days: Option<i64>,
147147
limit: Option<i64>,
148148
) -> Result<RecentTracksResponse, String> {
149-
let days = days.unwrap_or(14).min(365).max(1);
150-
let limit = limit.unwrap_or(100).min(1000).max(1);
149+
let days = days.unwrap_or(14).clamp(1, 365);
150+
let limit = limit.unwrap_or(100).clamp(1, 1000);
151151

152152
let conn = db.conn().map_err(|e| e.to_string())?;
153153
let tracks = favorites::get_recently_played(&conn, days, limit).map_err(|e| e.to_string())?;
@@ -162,8 +162,8 @@ pub fn favorites_get_recently_added(
162162
days: Option<i64>,
163163
limit: Option<i64>,
164164
) -> Result<RecentTracksResponse, String> {
165-
let days = days.unwrap_or(14).min(365).max(1);
166-
let limit = limit.unwrap_or(100).min(1000).max(1);
165+
let days = days.unwrap_or(14).clamp(1, 365);
166+
let limit = limit.unwrap_or(100).clamp(1, 1000);
167167

168168
let conn = db.conn().map_err(|e| e.to_string())?;
169169
let tracks = favorites::get_recently_added(&conn, days, limit).map_err(|e| e.to_string())?;

src-tauri/src/commands/lastfm.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ use tauri::{AppHandle, Emitter, State};
1414

1515
/// Helper to check if a setting is truthy
1616
fn is_setting_truthy(value: Option<String>) -> bool {
17-
match value.as_deref() {
18-
Some("1") | Some("true") | Some("yes") | Some("on") => true,
19-
_ => false,
20-
}
17+
matches!(value.as_deref(), Some("1") | Some("true") | Some("yes") | Some("on"))
2118
}
2219

2320
/// Helper to parse setting as u8

src-tauri/src/db/models.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -178,16 +178,13 @@ pub struct PaginatedResult<T> {
178178

179179
/// Sort order for queries
180180
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
181+
#[derive(Default)]
181182
pub enum SortOrder {
182183
Asc,
184+
#[default]
183185
Desc,
184186
}
185187

186-
impl Default for SortOrder {
187-
fn default() -> Self {
188-
SortOrder::Desc
189-
}
190-
}
191188

192189
impl SortOrder {
193190
pub fn as_sql(&self) -> &'static str {
@@ -200,21 +197,18 @@ impl SortOrder {
200197

201198
/// Valid sort columns for library queries
202199
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
200+
#[derive(Default)]
203201
pub enum LibrarySortColumn {
204202
Title,
205203
Artist,
206204
Album,
205+
#[default]
207206
AddedDate,
208207
PlayCount,
209208
Duration,
210209
LastPlayed,
211210
}
212211

213-
impl Default for LibrarySortColumn {
214-
fn default() -> Self {
215-
LibrarySortColumn::AddedDate
216-
}
217-
}
218212

219213
impl LibrarySortColumn {
220214
pub fn as_sql(&self) -> &'static str {
@@ -228,9 +222,13 @@ impl LibrarySortColumn {
228222
LibrarySortColumn::LastPlayed => "last_played",
229223
}
230224
}
225+
}
226+
227+
impl std::str::FromStr for LibrarySortColumn {
228+
type Err = ();
231229

232-
pub fn from_str(s: &str) -> Self {
233-
match s.to_lowercase().as_str() {
230+
fn from_str(s: &str) -> Result<Self, Self::Err> {
231+
Ok(match s.to_lowercase().as_str() {
234232
"title" => LibrarySortColumn::Title,
235233
"artist" => LibrarySortColumn::Artist,
236234
"album" => LibrarySortColumn::Album,
@@ -239,7 +237,7 @@ impl LibrarySortColumn {
239237
"duration" => LibrarySortColumn::Duration,
240238
"last_played" => LibrarySortColumn::LastPlayed,
241239
_ => LibrarySortColumn::AddedDate,
242-
}
240+
})
243241
}
244242
}
245243

@@ -268,16 +266,18 @@ mod tests {
268266

269267
#[test]
270268
fn test_sort_column_from_str() {
269+
use std::str::FromStr;
270+
271271
assert_eq!(
272-
LibrarySortColumn::from_str("title"),
272+
LibrarySortColumn::from_str("title").unwrap(),
273273
LibrarySortColumn::Title
274274
);
275275
assert_eq!(
276-
LibrarySortColumn::from_str("ARTIST"),
276+
LibrarySortColumn::from_str("ARTIST").unwrap(),
277277
LibrarySortColumn::Artist
278278
);
279279
assert_eq!(
280-
LibrarySortColumn::from_str("invalid"),
280+
LibrarySortColumn::from_str("invalid").unwrap(),
281281
LibrarySortColumn::AddedDate
282282
);
283283
}

src-tauri/src/library/commands.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::path::Path;
77
use tauri::{AppHandle, State};
88

99
use crate::db::{
10-
library, Database, LibrarySortColumn, LibraryStats, SortOrder, Track, TrackMetadata,
10+
library, Database, LibraryStats, SortOrder, Track, TrackMetadata,
1111
};
1212
use crate::events::{EventEmitter, LibraryUpdatedEvent};
1313
use crate::scanner::artwork::Artwork;
@@ -32,6 +32,7 @@ pub struct MissingTracksResponse {
3232
}
3333

3434
/// Get all tracks with filtering, sorting, and pagination
35+
#[allow(clippy::too_many_arguments)]
3536
#[tauri::command]
3637
pub fn library_get_all(
3738
db: State<'_, Database>,
@@ -54,7 +55,7 @@ pub fn library_get_all(
5455
album,
5556
sort_by: sort_by
5657
.as_ref()
57-
.map(|s| LibrarySortColumn::from_str(s))
58+
.and_then(|s| s.parse().ok())
5859
.unwrap_or_default(),
5960
sort_order: sort_order
6061
.as_ref()
@@ -260,8 +261,8 @@ pub fn library_locate_track(
260261
// 2. The watcher detected the file at new location and added it as a "new" track
261262
// 3. User uses "Locate" to point the missing track to the same file
262263
let mut deleted_duplicate_id: Option<i64> = None;
263-
if let Ok(Some(existing_track)) = library::get_track_by_filepath(&conn, &new_path) {
264-
if existing_track.id != track_id {
264+
if let Ok(Some(existing_track)) = library::get_track_by_filepath(&conn, &new_path)
265+
&& existing_track.id != track_id {
265266
// There's a duplicate track at this path - remove it
266267
// The original track (being located) takes precedence to preserve play history
267268
println!(
@@ -271,7 +272,6 @@ pub fn library_locate_track(
271272
library::delete_track(&conn, existing_track.id).map_err(|e| e.to_string())?;
272273
deleted_duplicate_id = Some(existing_track.id);
273274
}
274-
}
275275

276276
// Update the filepath (also clears missing flag and updates last_seen_at)
277277
library::update_track_filepath(&conn, track_id, &new_path).map_err(|e| e.to_string())?;

0 commit comments

Comments
 (0)