main
use serde::{Deserialize, Serialize};
use crate::loader::Section;
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct EmailConfig {
pub default_locale: Option<String>,
}
impl EmailConfig {
pub fn default_locale(&self) -> &str {
self.default_locale.as_deref().unwrap_or("en")
}
}
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
#[serde(default)]
pub struct EmailPoolConfig {
pub workers: usize,
pub capacity: usize,
pub enqueue_timeout_ms: u64,
pub retry_delay_ms: u64,
pub max_attempts: u32,
#[serde(default = "default_mail_topic")]
pub mail_topic: String,
#[serde(default = "default_consumer_group")]
pub consumer_group: String,
#[serde(default = "default_max_reconsume")]
pub max_reconsume: u32,
}
fn default_mail_topic() -> String {
"gitdata-mail".to_owned()
}
fn default_consumer_group() -> String {
"gitdata-mail".to_owned()
}
fn default_max_reconsume() -> u32 {
5
}
impl EmailPoolConfig {
pub fn is_valid(&self) -> Result<(), String> {
if self.workers == 0 {
return Err("email.workers must be greater than 0".to_string());
}
if self.capacity == 0 {
return Err("email.capacity must be greater than 0".to_string());
}
if self.enqueue_timeout_ms == 0 {
return Err("email.enqueue_timeout_ms must be greater than 0".to_string());
}
if !(1..=100).contains(&self.max_attempts) {
return Err("email.max_attempts must be between 1 and 100".to_string());
}
if self.mail_topic.trim().is_empty() {
return Err("email.mail_topic must not be blank".to_string());
}
if self.consumer_group.trim().is_empty() {
return Err("email.consumer_group must not be blank".to_string());
}
if !(1..=16).contains(&self.max_reconsume) {
return Err("email.max_reconsume must be between 1 and 16".to_string());
}
Ok(())
}
pub fn retry_delay(&self) -> std::time::Duration {
std::time::Duration::from_millis(self.retry_delay_ms)
}
}
impl Default for EmailPoolConfig {
fn default() -> Self {
Self {
workers: 4,
capacity: 1024,
enqueue_timeout_ms: 5_000,
retry_delay_ms: 2_000,
max_attempts: 3,
mail_topic: default_mail_topic(),
consumer_group: default_consumer_group(),
max_reconsume: default_max_reconsume(),
}
}
}
impl Section for EmailPoolConfig {
const KEY: &'static str = "email";
}