From 63edcba9fa7e6bd85a6a2712d5b00b999b3775d1 Mon Sep 17 00:00:00 2001 From: Joscha Date: Fri, 27 Dec 2024 14:51:51 +0100 Subject: [PATCH] Move config structs to corresponding modules --- euphoxide-bot/src/bot.rs | 18 +++++++- euphoxide-bot/src/config.rs | 82 ----------------------------------- euphoxide-bot/src/instance.rs | 76 +++++++++++++++++++++++++++++--- euphoxide-bot/src/lib.rs | 3 +- 4 files changed, 89 insertions(+), 90 deletions(-) delete mode 100644 euphoxide-bot/src/config.rs diff --git a/euphoxide-bot/src/bot.rs b/euphoxide-bot/src/bot.rs index aba1ec9..63d1175 100644 --- a/euphoxide-bot/src/bot.rs +++ b/euphoxide-bot/src/bot.rs @@ -1,6 +1,7 @@ use std::{ collections::HashMap, sync::{Arc, RwLock}, + time::Duration, }; use euphoxide::{ @@ -9,7 +10,7 @@ use euphoxide::{ }; use tokio::sync::mpsc; -use crate::{BotConfig, Instance, InstanceConfig, InstanceEvent}; +use crate::{Instance, InstanceConfig, InstanceEvent}; #[derive(Debug)] pub enum BotEvent { @@ -87,6 +88,21 @@ impl BotEvent { } } +#[non_exhaustive] +pub struct BotConfig { + pub event_timeout: Duration, + pub event_channel_bufsize: usize, +} + +impl Default for BotConfig { + fn default() -> Self { + Self { + event_timeout: Duration::from_secs(1), + event_channel_bufsize: 10, + } + } +} + pub struct Bot { config: BotConfig, next_id: usize, diff --git a/euphoxide-bot/src/config.rs b/euphoxide-bot/src/config.rs deleted file mode 100644 index 18880b0..0000000 --- a/euphoxide-bot/src/config.rs +++ /dev/null @@ -1,82 +0,0 @@ -use std::{ - sync::{Arc, Mutex}, - time::Duration, -}; - -use cookie::CookieJar; -use euphoxide::client::conn::ClientConnConfig; - -#[derive(Debug, Clone)] -pub struct ServerConfig { - pub client: ClientConnConfig, - pub cookies: Arc>, - pub join_attempts: usize, - pub reconnect_delay: Duration, - pub cmd_channel_bufsize: usize, -} - -impl ServerConfig { - pub fn instance(self, room: impl ToString) -> InstanceConfig { - InstanceConfig { - server: self, - room: room.to_string(), - human: false, - username: None, - force_username: false, - password: None, - } - } -} - -impl Default for ServerConfig { - fn default() -> Self { - Self { - client: ClientConnConfig::default(), - cookies: Arc::new(Mutex::new(CookieJar::new())), - join_attempts: 5, - reconnect_delay: Duration::from_secs(30), - cmd_channel_bufsize: 1, - } - } -} - -#[derive(Debug, Clone)] -pub struct InstanceConfig { - pub server: ServerConfig, - pub room: String, - pub human: bool, - pub username: Option, - pub force_username: bool, - pub password: Option, -} - -impl InstanceConfig { - pub fn with_username(mut self, username: impl ToString) -> Self { - self.username = Some(username.to_string()); - self - } - - pub fn with_force_username(mut self, enabled: bool) -> Self { - self.force_username = enabled; - self - } - - pub fn with_password(mut self, password: impl ToString) -> Self { - self.password = Some(password.to_string()); - self - } -} - -pub struct BotConfig { - pub event_timeout: Duration, - pub event_channel_bufsize: usize, -} - -impl Default for BotConfig { - fn default() -> Self { - Self { - event_timeout: Duration::from_secs(1), - event_channel_bufsize: 10, - } - } -} diff --git a/euphoxide-bot/src/instance.rs b/euphoxide-bot/src/instance.rs index a82ad57..f43d77c 100644 --- a/euphoxide-bot/src/instance.rs +++ b/euphoxide-bot/src/instance.rs @@ -1,10 +1,15 @@ -use std::{fmt, result, str::FromStr}; +use std::{ + fmt, result, + str::FromStr, + sync::{Arc, Mutex}, + time::Duration, +}; -use cookie::Cookie; +use cookie::{Cookie, CookieJar}; use euphoxide::{ api::{Auth, AuthOption, BounceEvent, Data, Nick, ParsedPacket}, client::{ - conn::{ClientConn, ClientConnHandle}, + conn::{ClientConn, ClientConnConfig, ClientConnHandle}, state::State, }, }; @@ -18,8 +23,6 @@ use tokio_tungstenite::tungstenite::{ http::{HeaderValue, StatusCode}, }; -use crate::InstanceConfig; - enum Error { Stopped, NoReferences, @@ -117,6 +120,69 @@ impl InstanceEvent { } } +#[derive(Debug, Clone)] +#[non_exhaustive] +pub struct ServerConfig { + pub client: ClientConnConfig, + pub cookies: Arc>, + pub join_attempts: usize, + pub reconnect_delay: Duration, + pub cmd_channel_bufsize: usize, +} + +impl ServerConfig { + pub fn instance(self, room: impl ToString) -> InstanceConfig { + InstanceConfig { + server: self, + room: room.to_string(), + human: false, + username: None, + force_username: false, + password: None, + } + } +} + +impl Default for ServerConfig { + fn default() -> Self { + Self { + client: ClientConnConfig::default(), + cookies: Arc::new(Mutex::new(CookieJar::new())), + join_attempts: 5, + reconnect_delay: Duration::from_secs(30), + cmd_channel_bufsize: 1, + } + } +} + +#[derive(Debug, Clone)] +#[non_exhaustive] +pub struct InstanceConfig { + pub server: ServerConfig, + pub room: String, + pub human: bool, + pub username: Option, + pub force_username: bool, + pub password: Option, +} + +impl InstanceConfig { + pub fn with_username(mut self, username: impl ToString) -> Self { + self.username = Some(username.to_string()); + self + } + + pub fn with_force_username(mut self, enabled: bool) -> Self { + self.force_username = enabled; + self + } + + pub fn with_password(mut self, password: impl ToString) -> Self { + self.password = Some(password.to_string()); + self + } +} + struct InstanceTask { id: usize, config: InstanceConfig, diff --git a/euphoxide-bot/src/lib.rs b/euphoxide-bot/src/lib.rs index 1a02484..f07d986 100644 --- a/euphoxide-bot/src/lib.rs +++ b/euphoxide-bot/src/lib.rs @@ -1,5 +1,4 @@ mod bot; -mod config; mod instance; -pub use crate::{bot::*, config::*, instance::*}; +pub use crate::{bot::*, instance::*};