From 9f174c4c5e541c184c24fb783d0d51c20b49a4e7 Mon Sep 17 00:00:00 2001 From: Michel de Graaf Date: Wed, 19 Nov 2025 08:40:41 +0100 Subject: [PATCH] fix: linting --- soulseek-rs-lib/src/client.rs | 17 ++++++--- soulseek-rs-lib/src/lib.rs | 4 +-- soulseek-rs-lib/src/peer/download_peer.rs | 13 +++++-- soulseek-rs-lib/src/peer/listen.rs | 9 ++++- soulseek-rs-lib/src/types.rs | 13 +++++-- soulseek-rs-lib/src/utils/logger.rs | 5 ++- soulseek-rs/src/cli.rs | 6 +++- soulseek-rs/src/ui/downloads.rs | 44 +++++++++++++---------- soulseek-rs/src/ui/main_tui.rs | 39 +++++++++----------- 9 files changed, 93 insertions(+), 57 deletions(-) diff --git a/soulseek-rs-lib/src/client.rs b/soulseek-rs-lib/src/client.rs index 3c477a4..705694b 100644 --- a/soulseek-rs-lib/src/client.rs +++ b/soulseek-rs-lib/src/client.rs @@ -384,7 +384,7 @@ impl Client { username: String, size: u64, download_directory: String, - ) -> Result> { + ) -> Result<(Download, Receiver)> { info!("[client] Downloading {} from {}", filename, username); let hash = md5::md5(&filename); @@ -421,10 +421,13 @@ impl Client { if !download_initiated { let _ = download.sender.send(DownloadStatus::Failed); - self.context.write().unwrap().update_download_with_status(token, DownloadStatus::Failed); + self.context + .write() + .unwrap() + .update_download_with_status(token, DownloadStatus::Failed); } - Ok(download_receiver) + Ok((download, download_receiver)) } fn process_failed_uploads( @@ -811,7 +814,13 @@ impl Client { download.size ); let _ = download.sender.send(DownloadStatus::Completed); - client_context.write().unwrap().update_download_with_status(download.token, DownloadStatus::Completed); + client_context + .write() + .unwrap() + .update_download_with_status( + download.token, + DownloadStatus::Completed, + ); } Err(e) => { trace!("[client] failed to download: {}", e); diff --git a/soulseek-rs-lib/src/lib.rs b/soulseek-rs-lib/src/lib.rs index 7c9abb9..d4fd935 100644 --- a/soulseek-rs-lib/src/lib.rs +++ b/soulseek-rs-lib/src/lib.rs @@ -22,6 +22,4 @@ pub mod prelude { pub use actor::server_actor::PeerAddress; pub use client::{Client, ClientSettings}; pub use error::{Result, SoulseekRs}; -pub use types::{ - DownloadStatus, File, Search, SearchResult, Transfer, -}; +pub use types::{DownloadStatus, File, Search, SearchResult, Transfer}; diff --git a/soulseek-rs-lib/src/peer/download_peer.rs b/soulseek-rs-lib/src/peer/download_peer.rs index 3396cac..591efe5 100644 --- a/soulseek-rs-lib/src/peer/download_peer.rs +++ b/soulseek-rs-lib/src/peer/download_peer.rs @@ -242,7 +242,8 @@ impl DownloadPeer { let client_guard = client_context .read() .map_err(|_| DownloadError::LockPoisoned)?; - let download_info = client_guard.get_download_by_token(token_u32).cloned(); + let download_info = + client_guard.get_download_by_token(token_u32).cloned(); drop(client_guard); download_info.ok_or(DownloadError::TokenNotFound(token_u32)) @@ -323,7 +324,10 @@ impl DownloadPeer { speed_bytes_per_sec: speed, }; let _ = dl.sender.send(status.clone()); - client_context.write().unwrap().update_download_with_status(dl.token, status); + client_context + .write() + .unwrap() + .update_download_with_status(dl.token, status); last_update_time = Instant::now(); } @@ -421,7 +425,10 @@ impl DownloadPeer { if let Some(ref dl) = download { let _ = dl.sender.send(DownloadStatus::Queued); - client_context.write().unwrap().update_download_with_status(dl.token, DownloadStatus::Queued); + client_context + .write() + .unwrap() + .update_download_with_status(dl.token, DownloadStatus::Queued); } let mut stream = match stream { diff --git a/soulseek-rs-lib/src/peer/listen.rs b/soulseek-rs-lib/src/peer/listen.rs index c43bb48..36cd4ef 100644 --- a/soulseek-rs-lib/src/peer/listen.rs +++ b/soulseek-rs-lib/src/peer/listen.rs @@ -157,7 +157,14 @@ fn handle_file_connection( ) { Ok((download, filename)) => { let _ = download.sender.send(DownloadStatus::Completed); - context.client_context.write().unwrap().update_download_with_status(download.token, DownloadStatus::Completed); + context + .client_context + .write() + .unwrap() + .update_download_with_status( + download.token, + DownloadStatus::Completed, + ); info!( "Successfully downloaded {} bytes to {}", download.size, filename diff --git a/soulseek-rs-lib/src/types.rs b/soulseek-rs-lib/src/types.rs index 0536dac..3aefcfe 100644 --- a/soulseek-rs-lib/src/types.rs +++ b/soulseek-rs-lib/src/types.rs @@ -102,13 +102,17 @@ impl Download { pub fn is_finished(&self) -> bool { matches!( self.status, - DownloadStatus::Completed | DownloadStatus::Failed | DownloadStatus::TimedOut + DownloadStatus::Completed + | DownloadStatus::Failed + | DownloadStatus::TimedOut ) } pub fn bytes_downloaded(&self) -> u64 { match &self.status { - DownloadStatus::InProgress { bytes_downloaded, .. } => *bytes_downloaded, + DownloadStatus::InProgress { + bytes_downloaded, .. + } => *bytes_downloaded, DownloadStatus::Completed => self.size, _ => 0, } @@ -116,7 +120,10 @@ impl Download { pub fn speed_bytes_per_sec(&self) -> f64 { match &self.status { - DownloadStatus::InProgress { speed_bytes_per_sec, .. } => *speed_bytes_per_sec, + DownloadStatus::InProgress { + speed_bytes_per_sec, + .. + } => *speed_bytes_per_sec, _ => 0.0, } } diff --git a/soulseek-rs-lib/src/utils/logger.rs b/soulseek-rs-lib/src/utils/logger.rs index 24f745b..957f25e 100644 --- a/soulseek-rs-lib/src/utils/logger.rs +++ b/soulseek-rs-lib/src/utils/logger.rs @@ -54,7 +54,10 @@ pub fn init() { } } Err(e) => { - eprintln!("Failed to open log file '{}': {}", log_file_path, e); + eprintln!( + "Failed to open log file '{}': {}", + log_file_path, e + ); } } } diff --git a/soulseek-rs/src/cli.rs b/soulseek-rs/src/cli.rs index 7e221c2..4095ba4 100644 --- a/soulseek-rs/src/cli.rs +++ b/soulseek-rs/src/cli.rs @@ -33,7 +33,11 @@ pub struct Cli { #[arg(short, long, global = true, action = clap::ArgAction::Count)] pub verbose: u8, - #[arg(long, env = "LOG_FILE", help = "Write logs to file instead of stderr")] + #[arg( + long, + env = "LOG_FILE", + help = "Write logs to file instead of stderr" + )] pub log_file: Option, #[command(subcommand)] diff --git a/soulseek-rs/src/ui/downloads.rs b/soulseek-rs/src/ui/downloads.rs index 8247bee..c98dfc1 100644 --- a/soulseek-rs/src/ui/downloads.rs +++ b/soulseek-rs/src/ui/downloads.rs @@ -28,7 +28,8 @@ const VERSION: &str = env!("CARGO_PKG_VERSION"); pub struct MultiDownloadProgress { downloads: Vec, - receiver_channel: Receiver<(soulseek_rs::types::Download, Receiver)>, + receiver_channel: + Receiver<(soulseek_rs::types::Download, Receiver)>, list_state: TableState, max_concurrent: usize, active_count: usize, @@ -38,7 +39,10 @@ pub struct MultiDownloadProgress { impl MultiDownloadProgress { pub fn new( - receiver_channel: Receiver<(soulseek_rs::types::Download, Receiver)>, + receiver_channel: Receiver<( + soulseek_rs::types::Download, + Receiver, + )>, max_concurrent: usize, ) -> Self { let mut list_state = TableState::default(); @@ -63,12 +67,15 @@ impl MultiDownloadProgress { terminal.draw(|frame| self.render(frame))?; // Check for incoming downloads from background thread - while let Ok((download, receiver)) = self.receiver_channel.try_recv() { + while let Ok((download, receiver)) = + self.receiver_channel.try_recv() + { self.downloads.push(DownloadEntry { download, receiver: Some(receiver), }); - self.queuing_status = format!("{} downloads queued", self.downloads.len()); + self.queuing_status = + format!("{} downloads queued", self.downloads.len()); } // Poll all receivers for status updates @@ -229,8 +236,7 @@ impl MultiDownloadProgress { }; let progress = if download.size > 0 { - download.bytes_downloaded() as f64 - / download.size as f64 + download.bytes_downloaded() as f64 / download.size as f64 } else { 0.0 }; @@ -348,8 +354,10 @@ pub fn render_download_stats( .filter(|d| matches!(d.download.status, DownloadStatus::Queued)) .count(); - let total_downloaded: u64 = - downloads.iter().map(|d| d.download.bytes_downloaded()).sum(); + let total_downloaded: u64 = downloads + .iter() + .map(|d| d.download.bytes_downloaded()) + .sum(); let total_size: u64 = downloads.iter().map(|d| d.download.size).sum(); let overall_progress = if total_size > 0 { (total_downloaded as f64 / total_size as f64 * 100.0) as u8 @@ -363,7 +371,9 @@ pub fn render_download_stats( }; let total_speed: f64 = downloads .iter() - .filter(|d| matches!(d.download.status, DownloadStatus::InProgress { .. })) + .filter(|d| { + matches!(d.download.status, DownloadStatus::InProgress { .. }) + }) .map(|d| d.download.speed_bytes_per_sec()) .sum(); let speed_mb = (total_speed / 1_048_576.0 * 100.0).round() / 100.0; @@ -461,7 +471,6 @@ pub fn show_multi_download_progress( ) -> Result<()> { soulseek_rs::utils::logger::enable_buffering(); - // Create channel for sending downloads and receivers from background thread let (tx, rx) = mpsc::channel(); // Spawn background thread to initialize downloads @@ -475,21 +484,20 @@ pub fn show_multi_download_progress( size, download_dir.clone(), ) { - Ok(receiver) => { - // Get the download from client - if let Some(download) = init_client.get_all_downloads().iter().find(|d| d.filename == filename && d.username == username).cloned() { - let _ = tx.send((download, receiver)); - } + Ok((download, receiver)) => { + let _ = tx.send((download, receiver)); } Err(e) => { - eprintln!("Failed to start download for {}: {}", filename, e); + eprintln!( + "Failed to start download for {}: {}", + filename, e + ); } } } }); - let mut progress = - MultiDownloadProgress::new(rx, max_concurrent); + let mut progress = MultiDownloadProgress::new(rx, max_concurrent); let result = progress.run(terminal); ratatui::restore(); diff --git a/soulseek-rs/src/ui/main_tui.rs b/soulseek-rs/src/ui/main_tui.rs index 58393a6..1fa2e22 100644 --- a/soulseek-rs/src/ui/main_tui.rs +++ b/soulseek-rs/src/ui/main_tui.rs @@ -777,33 +777,26 @@ impl MainTui { let sender = self.state.downloads_sender_channel.clone().unwrap(); thread::spawn(move || { - for (filename, username, size) in selected_files.into_iter() { - match client.download( - filename.clone(), - username.clone(), - size, - download_dir.clone(), - ) { - Ok(rx) => { - if let Some(download) = client - .get_all_downloads() - .iter() - .find(|d| { - d.filename == filename && d.username == username - }) - .cloned() - { + thread::spawn(move || { + for (filename, username, size) in selected_files.into_iter() { + match client.download( + filename.clone(), + username.clone(), + size, + download_dir.clone(), + ) { + Ok((download, rx)) => { let _ = sender.send((download, rx)); } - } - Err(e) => { - eprintln!( - "Failed to start download for {}: {}", - filename, e - ); + Err(e) => { + eprintln!( + "Failed to start download for {}: {}", + filename, e + ); + } } } - } + }); }); // Clear selection