Skip to content

Commit 10ebeb7

Browse files
style: apply cargo fmt
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent e5c3cc0 commit 10ebeb7

38 files changed

Lines changed: 832 additions & 570 deletions

crates/mt-tauri/src/audio/engine.rs

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,10 @@ impl AudioEngine {
6363
}
6464

6565
let file = File::open(path)?;
66-
let source = Decoder::try_from(file)
67-
.map_err(|e| {
68-
error!(path, error = %e, "Failed to decode track");
69-
AudioError::Decode(e.to_string())
70-
})?;
66+
let source = Decoder::try_from(file).map_err(|e| {
67+
error!(path, error = %e, "Failed to decode track");
68+
AudioError::Decode(e.to_string())
69+
})?;
7170

7271
let sample_rate = source.sample_rate();
7372
let channels = source.channels();
@@ -90,13 +89,7 @@ impl AudioEngine {
9089
self.current_track = Some(track_info.clone());
9190
self.state = PlaybackState::Paused;
9291

93-
info!(
94-
path,
95-
duration_ms,
96-
sample_rate,
97-
channels,
98-
"Track loaded"
99-
);
92+
info!(path, duration_ms, sample_rate, channels, "Track loaded");
10093

10194
Ok(track_info)
10295
}
@@ -133,45 +126,44 @@ impl AudioEngine {
133126
}
134127

135128
pub fn seek(&mut self, position_ms: u64) -> Result<(), AudioError> {
136-
let current_pos = self.player_handle
129+
let current_pos = self
130+
.player_handle
137131
.as_ref()
138132
.map(|h| h.sink.get_pos().as_millis() as u64)
139133
.unwrap_or(0);
140-
134+
141135
let is_backward = position_ms < current_pos;
142-
136+
143137
if is_backward {
144138
self.seek_by_reload(position_ms)
145139
} else {
146140
self.seek_forward(position_ms)
147141
}
148142
}
149-
143+
150144
fn seek_forward(&mut self, position_ms: u64) -> Result<(), AudioError> {
151145
if let Some(ref handle) = self.player_handle {
152146
let duration = Duration::from_millis(position_ms);
153-
handle.sink.try_seek(duration)
154-
.map_err(|e| {
155-
error!(position_ms, error = ?e, "Seek forward failed");
156-
AudioError::Seek(format!("{:?}", e))
157-
})?;
147+
handle.sink.try_seek(duration).map_err(|e| {
148+
error!(position_ms, error = ?e, "Seek forward failed");
149+
AudioError::Seek(format!("{:?}", e))
150+
})?;
158151
debug!(position_ms, "Seeked forward");
159152
Ok(())
160153
} else {
161154
Err(AudioError::NoTrack)
162155
}
163156
}
164-
157+
165158
fn seek_by_reload(&mut self, position_ms: u64) -> Result<(), AudioError> {
166159
let track_info = self.current_track.clone().ok_or(AudioError::NoTrack)?;
167160
let was_playing = self.state == PlaybackState::Playing;
168161

169162
let file = File::open(&track_info.path)?;
170-
let source = Decoder::try_from(file)
171-
.map_err(|e| {
172-
error!(path = %track_info.path, error = %e, "Decode failed during seek-by-reload");
173-
AudioError::Decode(e.to_string())
174-
})?;
163+
let source = Decoder::try_from(file).map_err(|e| {
164+
error!(path = %track_info.path, error = %e, "Decode failed during seek-by-reload");
165+
AudioError::Decode(e.to_string())
166+
})?;
175167

176168
if let Some(handle) = self.player_handle.take() {
177169
handle.sink.stop();
@@ -182,11 +174,10 @@ impl AudioEngine {
182174
sink.append(source);
183175

184176
let duration = Duration::from_millis(position_ms);
185-
sink.try_seek(duration)
186-
.map_err(|e| {
187-
error!(position_ms, error = ?e, "Seek-by-reload seek failed");
188-
AudioError::Seek(format!("{:?}", e))
189-
})?;
177+
sink.try_seek(duration).map_err(|e| {
178+
error!(position_ms, error = ?e, "Seek-by-reload seek failed");
179+
AudioError::Seek(format!("{:?}", e))
180+
})?;
190181

191182
if was_playing {
192183
sink.play();
@@ -215,7 +206,11 @@ impl AudioEngine {
215206
pub fn get_progress(&self) -> Progress {
216207
let (position_ms, duration_ms) = if let Some(ref handle) = self.player_handle {
217208
let pos = handle.sink.get_pos();
218-
let dur = self.current_track.as_ref().map(|t| t.duration_ms).unwrap_or(0);
209+
let dur = self
210+
.current_track
211+
.as_ref()
212+
.map(|t| t.duration_ms)
213+
.unwrap_or(0);
219214
(pos.as_millis() as u64, dur)
220215
} else {
221216
(0, 0)

crates/mt-tauri/src/audio/engine_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ mod tests {
268268
#[test]
269269
fn test_progress_percentage_calculation() {
270270
let progress = Progress {
271-
position_ms: 90000, // 1.5 minutes
271+
position_ms: 90000, // 1.5 minutes
272272
duration_ms: 180000, // 3 minutes
273273
state: PlaybackState::Playing,
274274
};

crates/mt-tauri/src/commands/audio.rs

Lines changed: 51 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub struct PlaybackStatus {
1616
}
1717

1818
enum AudioCommand {
19-
Load(String, Option<i64>, Sender<Result<TrackInfo, String>>), // Add track_id
19+
Load(String, Option<i64>, Sender<Result<TrackInfo, String>>), // Add track_id
2020
LoadAndPlay(String, Option<i64>, Sender<Result<TrackInfo, String>>),
2121
Play(Sender<Result<(), String>>),
2222
Pause(Sender<Result<(), String>>),
@@ -176,53 +176,57 @@ fn audio_thread(rx: Receiver<AudioCommand>, app: AppHandle) {
176176

177177
// Check play count threshold (75%)
178178
if !play_count_state.threshold_reached
179-
&& progress.duration_ms > 0
180-
&& play_count_state.track_id.is_some() {
179+
&& progress.duration_ms > 0
180+
&& play_count_state.track_id.is_some()
181+
{
181182
let ratio = progress.position_ms as f64 / progress.duration_ms as f64;
182183

183184
if ratio >= 0.75
184-
&& let Some(track_id) = play_count_state.track_id {
185-
// Spawn async task to avoid blocking audio thread
186-
let app_handle = app.clone();
187-
std::thread::spawn(move || {
188-
use crate::db::Database;
189-
use crate::db::library;
190-
191-
let db = app_handle.state::<Database>();
192-
if let Ok(conn) = db.conn() {
193-
let _ = library::update_play_count(&conn, track_id);
194-
debug!(track_id, "Play count updated");
195-
}
196-
});
197-
play_count_state.threshold_reached = true;
198-
}
185+
&& let Some(track_id) = play_count_state.track_id
186+
{
187+
// Spawn async task to avoid blocking audio thread
188+
let app_handle = app.clone();
189+
std::thread::spawn(move || {
190+
use crate::db::Database;
191+
use crate::db::library;
192+
193+
let db = app_handle.state::<Database>();
194+
if let Ok(conn) = db.conn() {
195+
let _ = library::update_play_count(&conn, track_id);
196+
debug!(track_id, "Play count updated");
197+
}
198+
});
199+
play_count_state.threshold_reached = true;
200+
}
199201
}
200202

201203
// Check scrobble threshold (90% default, configurable)
202204
if !scrobble_state.threshold_reached
203-
&& progress.duration_ms > 0
204-
&& scrobble_state.track_id.is_some() {
205+
&& progress.duration_ms > 0
206+
&& scrobble_state.track_id.is_some()
207+
{
205208
let ratio = progress.position_ms as f64 / progress.duration_ms as f64;
206209

207210
if ratio >= scrobble_state.threshold_percent
208-
&& let Some(track_id) = scrobble_state.track_id {
209-
// Spawn async task to avoid blocking audio thread
210-
let app_handle = app.clone();
211-
std::thread::spawn(move || {
212-
use crate::commands::lastfm;
213-
use crate::db::Database;
214-
215-
let db = app_handle.state::<Database>();
216-
if let Ok(conn) = db.conn() {
217-
// Queue scrobble from audio thread
218-
match lastfm::scrobble_from_audio_thread(&app_handle, &conn, track_id) {
219-
Ok(_) => debug!(track_id, "Scrobble queued"),
220-
Err(e) => error!(track_id, error = %e, "Failed to queue scrobble"),
221-
}
211+
&& let Some(track_id) = scrobble_state.track_id
212+
{
213+
// Spawn async task to avoid blocking audio thread
214+
let app_handle = app.clone();
215+
std::thread::spawn(move || {
216+
use crate::commands::lastfm;
217+
use crate::db::Database;
218+
219+
let db = app_handle.state::<Database>();
220+
if let Ok(conn) = db.conn() {
221+
// Queue scrobble from audio thread
222+
match lastfm::scrobble_from_audio_thread(&app_handle, &conn, track_id) {
223+
Ok(_) => debug!(track_id, "Scrobble queued"),
224+
Err(e) => error!(track_id, error = %e, "Failed to queue scrobble"),
222225
}
223-
});
224-
scrobble_state.threshold_reached = true;
225-
}
226+
}
227+
});
228+
scrobble_state.threshold_reached = true;
229+
}
226230
}
227231
}
228232

@@ -235,15 +239,23 @@ fn audio_thread(rx: Receiver<AudioCommand>, app: AppHandle) {
235239

236240
#[tracing::instrument(skip(state))]
237241
#[tauri::command]
238-
pub fn audio_load(path: String, track_id: Option<i64>, state: State<AudioState>) -> Result<TrackInfo, String> {
242+
pub fn audio_load(
243+
path: String,
244+
track_id: Option<i64>,
245+
state: State<AudioState>,
246+
) -> Result<TrackInfo, String> {
239247
let (tx, rx) = mpsc::channel();
240248
state.send_command(AudioCommand::Load(path, track_id, tx));
241249
rx.recv().map_err(|_| "Channel closed".to_string())?
242250
}
243251

244252
#[tracing::instrument(skip(state))]
245253
#[tauri::command]
246-
pub fn audio_load_and_play(path: String, track_id: Option<i64>, state: State<AudioState>) -> Result<TrackInfo, String> {
254+
pub fn audio_load_and_play(
255+
path: String,
256+
track_id: Option<i64>,
257+
state: State<AudioState>,
258+
) -> Result<TrackInfo, String> {
247259
let (tx, rx) = mpsc::channel();
248260
state.send_command(AudioCommand::LoadAndPlay(path, track_id, tx));
249261
rx.recv().map_err(|_| "Channel closed".to_string())?

crates/mt-tauri/src/commands/favorites.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use tauri::{AppHandle, State};
77
use tracing::{debug, warn};
88

9-
use crate::db::{favorites, library, settings, Database, FavoriteTrack, PaginatedResult, Track};
9+
use crate::db::{Database, FavoriteTrack, PaginatedResult, Track, favorites, library, settings};
1010
use crate::events::{EventEmitter, FavoritesUpdatedEvent};
1111
use crate::lastfm::LastFmClient;
1212

@@ -120,7 +120,10 @@ pub fn favorites_get(
120120
/// Check if a track is favorited
121121
#[tracing::instrument(skip(db))]
122122
#[tauri::command]
123-
pub fn favorites_check(db: State<'_, Database>, track_id: i64) -> Result<FavoriteCheckResponse, String> {
123+
pub fn favorites_check(
124+
db: State<'_, Database>,
125+
track_id: i64,
126+
) -> Result<FavoriteCheckResponse, String> {
124127
let conn = db.conn().map_err(|e| e.to_string())?;
125128
let (is_favorite, favorited_date) =
126129
favorites::is_favorite(&conn, track_id).map_err(|e| e.to_string())?;

0 commit comments

Comments
 (0)