Move config structs to corresponding modules

This commit is contained in:
Joscha 2024-12-27 14:51:51 +01:00
parent 8377695529
commit 63edcba9fa
4 changed files with 89 additions and 90 deletions

View file

@ -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,

View file

@ -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<Mutex<CookieJar>>,
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<String>,
pub force_username: bool,
pub password: Option<String>,
}
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,
}
}
}

View file

@ -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<Mutex<CookieJar>>,
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<String>,
pub force_username: bool,
pub password: Option<String>,
}
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,

View file

@ -1,5 +1,4 @@
mod bot;
mod config;
mod instance;
pub use crate::{bot::*, config::*, instance::*};
pub use crate::{bot::*, instance::*};