use std::time::Duration;
#[derive(Debug, Clone)]
pub struct InMemoryConfig {
pub max_queue_size: Option<usize>,
pub max_topics: Option<usize>,
pub enable_persistence: bool,
pub maintain_order: bool,
pub default_timeout: Duration,
pub enable_stats: bool,
pub max_subscribers_per_topic: Option<usize>,
pub default_message_ttl: Option<Duration>,
pub cleanup_interval: Duration,
}
impl Default for InMemoryConfig {
fn default() -> Self {
Self {
max_queue_size: Some(10000), max_topics: Some(1000), enable_persistence: true,
maintain_order: true,
default_timeout: Duration::from_secs(30),
enable_stats: false,
max_subscribers_per_topic: Some(100), default_message_ttl: None, cleanup_interval: Duration::from_secs(60), }
}
}
impl InMemoryConfig {
pub fn new() -> Self {
Self::default()
}
pub fn with_max_queue_size(mut self, size: Option<usize>) -> Self {
self.max_queue_size = size;
self
}
pub fn with_max_topics(mut self, max: Option<usize>) -> Self {
self.max_topics = max;
self
}
pub fn with_persistence(mut self, enabled: bool) -> Self {
self.enable_persistence = enabled;
self
}
pub fn with_ordering(mut self, enabled: bool) -> Self {
self.maintain_order = enabled;
self
}
pub fn with_maintain_order(self, enabled: bool) -> Self {
self.with_ordering(enabled)
}
pub fn with_timeout(mut self, timeout: Duration) -> Self {
self.default_timeout = timeout;
self
}
pub fn with_stats(mut self, enabled: bool) -> Self {
self.enable_stats = enabled;
self
}
pub fn with_max_subscribers_per_topic(mut self, max: Option<usize>) -> Self {
self.max_subscribers_per_topic = max;
self
}
pub fn with_message_ttl(mut self, ttl: Option<Duration>) -> Self {
self.default_message_ttl = ttl;
self
}
pub fn with_cleanup_interval(mut self, interval: Duration) -> Self {
self.cleanup_interval = interval;
self
}
pub fn for_testing() -> Self {
Self {
max_queue_size: Some(100),
max_topics: Some(10),
enable_persistence: true,
maintain_order: true,
default_timeout: Duration::from_millis(100),
enable_stats: true,
max_subscribers_per_topic: Some(5),
default_message_ttl: None, cleanup_interval: Duration::from_secs(60), }
}
pub fn for_high_performance() -> Self {
Self {
max_queue_size: None, max_topics: None, enable_persistence: true,
maintain_order: false, default_timeout: Duration::from_secs(1),
enable_stats: false, max_subscribers_per_topic: None, default_message_ttl: None, cleanup_interval: Duration::from_secs(300), }
}
pub fn validate(&self) -> Result<(), String> {
if let Some(queue_size) = self.max_queue_size {
if queue_size == 0 {
return Err("max_queue_size cannot be zero".to_string());
}
}
if let Some(max_topics) = self.max_topics {
if max_topics == 0 {
return Err("max_topics cannot be zero".to_string());
}
}
if let Some(max_subs) = self.max_subscribers_per_topic {
if max_subs == 0 {
return Err("max_subscribers_per_topic cannot be zero".to_string());
}
}
if self.default_timeout.is_zero() {
return Err("default_timeout cannot be zero".to_string());
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_config() {
let config = InMemoryConfig::default();
assert_eq!(config.max_queue_size, Some(10000));
assert_eq!(config.max_topics, Some(1000));
assert!(config.enable_persistence);
assert!(config.maintain_order);
assert!(!config.enable_stats);
}
#[test]
fn test_config_builder() {
let config = InMemoryConfig::new()
.with_max_queue_size(Some(5000))
.with_max_topics(Some(500))
.with_persistence(false)
.with_stats(true);
assert_eq!(config.max_queue_size, Some(5000));
assert_eq!(config.max_topics, Some(500));
assert!(!config.enable_persistence);
assert!(config.enable_stats);
}
#[test]
fn test_testing_config() {
let config = InMemoryConfig::for_testing();
assert_eq!(config.max_queue_size, Some(100));
assert_eq!(config.max_topics, Some(10));
assert!(config.enable_stats);
assert_eq!(config.default_timeout, Duration::from_millis(100));
}
#[test]
fn test_high_performance_config() {
let config = InMemoryConfig::for_high_performance();
assert_eq!(config.max_queue_size, None);
assert_eq!(config.max_topics, None);
assert!(!config.maintain_order);
assert!(!config.enable_stats);
}
#[test]
fn test_config_validation() {
let config = InMemoryConfig::default();
assert!(config.validate().is_ok());
let config = InMemoryConfig::default().with_max_queue_size(Some(0));
assert!(config.validate().is_err());
let config = InMemoryConfig::default().with_max_topics(Some(0));
assert!(config.validate().is_err());
let config = InMemoryConfig::default().with_timeout(Duration::ZERO);
assert!(config.validate().is_err());
}
}