From 3d6cc9f7cda5f74cc00ca7a1f169efb106d8f984 Mon Sep 17 00:00:00 2001 From: Michel de Graaf Date: Tue, 11 Nov 2025 22:22:21 +0100 Subject: [PATCH] feat: client settings and defaults --- soulseek-rs-lib/README.md | 34 +++++++++++++----- soulseek-rs-lib/src/client.rs | 68 +++++++++++++++++++++++++---------- soulseek-rs-lib/src/lib.rs | 2 +- soulseek-rs/src/main.rs | 10 ++---- 4 files changed, 79 insertions(+), 35 deletions(-) diff --git a/soulseek-rs-lib/README.md b/soulseek-rs-lib/README.md index 607aecc..71f1bdb 100644 --- a/soulseek-rs-lib/README.md +++ b/soulseek-rs-lib/README.md @@ -18,19 +18,15 @@ soulseek-rs-lib = "0.1.0" ## Example +### Simple Usage + ```rust -use soulseek_rs::{Client, PeerAddress}; +use soulseek_rs::Client; use std::time::Duration; fn main() -> Result<(), Box> { // Create and connect to Soulseek server - let mut client = Client::new( - PeerAddress::new("server.slsknet.org".to_string(), 2242), - "username".to_string(), - "password".to_string(), - false, - None, - ); + let mut client = Client::new("username", "password"); client.connect(); client.login()?; @@ -53,3 +49,25 @@ fn main() -> Result<(), Box> { Ok(()) } ``` + +### Advanced Configuration + +```rust +use soulseek_rs::{Client, ClientSettings, PeerAddress}; + +fn main() -> Result<(), Box> { + // Create client with custom settings + let settings = ClientSettings { + server_address: PeerAddress::new("server.slsknet.org".to_string(), 2242), + enable_listen: true, + listen_port: 3000, + ..ClientSettings::new("username", "password") + }; + + let mut client = Client::with_settings(settings); + client.connect(); + client.login()?; + + Ok(()) +} +``` diff --git a/soulseek-rs-lib/src/client.rs b/soulseek-rs-lib/src/client.rs index 7563c8d..f96a031 100644 --- a/soulseek-rs-lib/src/client.rs +++ b/soulseek-rs-lib/src/client.rs @@ -1,6 +1,7 @@ use crate::actor::server_actor::{PeerAddress, ServerActor, ServerMessage}; use crate::actor::ActorHandle; use crate::types::{DownloadResult, DownloadStatus}; +use crate::utils::logger; use crate::{ actor::{peer_registry::PeerRegistry, ActorSystem}, error::{Result, SoulseekRs}, @@ -26,6 +27,43 @@ use std::{ use crate::{debug, error, info, trace}; const DEFALT_LISTEN_PORT: u32 = 2234; +#[derive(Debug, Clone)] +pub struct ClientSettings { + pub username: String, + pub password: String, + pub server_address: PeerAddress, + pub enable_listen: bool, + pub listen_port: u32, +} + +impl ClientSettings { + pub fn new( + username: impl Into, + password: impl Into, + ) -> Self { + Self { + username: username.into(), + password: password.into(), + ..Default::default() + } + } +} + +impl Default for ClientSettings { + fn default() -> Self { + Self { + username: String::new(), + password: String::new(), + server_address: PeerAddress::new( + "server.slsknet.org".to_string(), + 2416, + ), + enable_listen: false, + listen_port: DEFALT_LISTEN_PORT, + } + } +} + #[derive(Debug)] pub enum ClientOperation { @@ -92,30 +130,24 @@ pub struct Client { impl Client { pub fn new( - address: PeerAddress, - username: String, - password: String, - enable_listen: bool, - listen_port: Option, + username: impl Into, + password: impl Into, ) -> Self { - crate::utils::logger::init(); + Self::with_settings(ClientSettings::new(username, password)) + } + + pub fn with_settings(settings: ClientSettings) -> Self { + logger::init(); Self { - enable_listen, - listen_port: listen_port.unwrap_or(DEFALT_LISTEN_PORT), - address, - username, - password, + enable_listen: settings.enable_listen, + listen_port: settings.listen_port, + address: settings.server_address, + username: settings.username, + password: settings.password, context: Arc::new(RwLock::new(ClientContext::new())), server_handle: None, } } - pub fn with_defaults( - address: PeerAddress, - username: String, - password: String, - ) -> Self { - Self::new(address, username, password, false, None) - } pub fn connect(&mut self) { let (sender, message_reader): ( diff --git a/soulseek-rs-lib/src/lib.rs b/soulseek-rs-lib/src/lib.rs index 966c18e..874815a 100644 --- a/soulseek-rs-lib/src/lib.rs +++ b/soulseek-rs-lib/src/lib.rs @@ -20,7 +20,7 @@ pub mod prelude { // Re-export commonly used types pub use actor::server_actor::PeerAddress; -pub use client::Client; +pub use client::{Client, ClientSettings}; pub use error::{Result, SoulseekRs}; pub use types::{ DownloadResult, DownloadStatus, File, FileSearchResult, Transfer, diff --git a/soulseek-rs/src/main.rs b/soulseek-rs/src/main.rs index acfc30b..4b29cf3 100644 --- a/soulseek-rs/src/main.rs +++ b/soulseek-rs/src/main.rs @@ -1,15 +1,9 @@ -use soulseek_rs::{Client, PeerAddress}; +use soulseek_rs::Client; use std::time::Duration; fn main() -> Result<(), Box> { // Create and connect to Soulseek server - let mut client = Client::new( - PeerAddress::new("server.slsknet.org".to_string(), 2242), - "username".to_string(), - "password".to_string(), - false, - None, - ); + let mut client = Client::new("username", "password"); client.connect(); client.login()?;