use std::time::Duration;
#[derive(Debug, Clone)]
pub struct TapConfig {
pub hostname: String,
pub admin_password: Option<String>,
pub send_acks: bool,
pub user_agent: String,
pub max_reconnect_attempts: Option<u32>,
pub initial_reconnect_delay: Duration,
pub max_reconnect_delay: Duration,
pub reconnect_backoff_multiplier: f64,
pub channel_buffer_size: usize,
}
impl Default for TapConfig {
fn default() -> Self {
Self {
hostname: "localhost:2480".to_string(),
admin_password: None,
send_acks: true,
user_agent: format!("atproto-tap/{}", env!("CARGO_PKG_VERSION")),
max_reconnect_attempts: None,
initial_reconnect_delay: Duration::from_secs(1),
max_reconnect_delay: Duration::from_secs(60),
reconnect_backoff_multiplier: 2.0,
channel_buffer_size: 32,
}
}
}
impl TapConfig {
pub fn builder() -> TapConfigBuilder {
TapConfigBuilder::default()
}
pub fn new(hostname: impl Into<String>) -> Self {
Self {
hostname: hostname.into(),
..Default::default()
}
}
pub fn ws_url(&self) -> String {
format!("ws://{}/channel", self.hostname)
}
pub fn http_base_url(&self) -> String {
format!("http://{}", self.hostname)
}
}
#[derive(Debug, Clone, Default)]
pub struct TapConfigBuilder {
config: TapConfig,
}
impl TapConfigBuilder {
pub fn hostname(mut self, hostname: impl Into<String>) -> Self {
self.config.hostname = hostname.into();
self
}
pub fn admin_password(mut self, password: impl Into<String>) -> Self {
self.config.admin_password = Some(password.into());
self
}
pub fn send_acks(mut self, send_acks: bool) -> Self {
self.config.send_acks = send_acks;
self
}
pub fn user_agent(mut self, user_agent: impl Into<String>) -> Self {
self.config.user_agent = user_agent.into();
self
}
pub fn max_reconnect_attempts(mut self, max: Option<u32>) -> Self {
self.config.max_reconnect_attempts = max;
self
}
pub fn initial_reconnect_delay(mut self, delay: Duration) -> Self {
self.config.initial_reconnect_delay = delay;
self
}
pub fn max_reconnect_delay(mut self, delay: Duration) -> Self {
self.config.max_reconnect_delay = delay;
self
}
pub fn reconnect_backoff_multiplier(mut self, multiplier: f64) -> Self {
self.config.reconnect_backoff_multiplier = multiplier;
self
}
pub fn channel_buffer_size(mut self, size: usize) -> Self {
self.config.channel_buffer_size = size;
self
}
pub fn build(self) -> TapConfig {
self.config
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_config() {
let config = TapConfig::default();
assert_eq!(config.hostname, "localhost:2480");
assert!(config.admin_password.is_none());
assert!(config.send_acks);
assert!(config.max_reconnect_attempts.is_none());
assert_eq!(config.initial_reconnect_delay, Duration::from_secs(1));
assert_eq!(config.max_reconnect_delay, Duration::from_secs(60));
assert!((config.reconnect_backoff_multiplier - 2.0).abs() < f64::EPSILON);
assert_eq!(config.channel_buffer_size, 32);
}
#[test]
fn test_builder() {
let config = TapConfig::builder()
.hostname("tap.example.com:2480")
.admin_password("secret123")
.send_acks(false)
.max_reconnect_attempts(Some(5))
.initial_reconnect_delay(Duration::from_millis(500))
.max_reconnect_delay(Duration::from_secs(30))
.reconnect_backoff_multiplier(1.5)
.build();
assert_eq!(config.hostname, "tap.example.com:2480");
assert_eq!(config.admin_password, Some("secret123".to_string()));
assert!(!config.send_acks);
assert_eq!(config.max_reconnect_attempts, Some(5));
assert_eq!(config.initial_reconnect_delay, Duration::from_millis(500));
assert_eq!(config.max_reconnect_delay, Duration::from_secs(30));
assert!((config.reconnect_backoff_multiplier - 1.5).abs() < f64::EPSILON);
}
#[test]
fn test_channel_buffer_size() {
let config = TapConfig::builder().channel_buffer_size(128).build();
assert_eq!(config.channel_buffer_size, 128);
}
#[test]
fn test_ws_url() {
let config = TapConfig::new("localhost:2480");
assert_eq!(config.ws_url(), "ws://localhost:2480/channel");
let config = TapConfig::new("tap.example.com:8080");
assert_eq!(config.ws_url(), "ws://tap.example.com:8080/channel");
}
#[test]
fn test_http_base_url() {
let config = TapConfig::new("localhost:2480");
assert_eq!(config.http_base_url(), "http://localhost:2480");
}
}