use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CoreConfig {
pub max_message_size: usize,
pub timeout_ms: u64,
pub tracing_enabled: bool,
pub options: HashMap<String, serde_json::Value>,
}
impl Default for CoreConfig {
fn default() -> Self {
Self {
max_message_size: 64 * 1024 * 1024, timeout_ms: 30_000, tracing_enabled: true,
options: HashMap::new(),
}
}
}
#[derive(Debug)]
pub struct ConfigBuilder {
config: CoreConfig,
}
impl ConfigBuilder {
#[must_use]
pub fn new() -> Self {
Self {
config: CoreConfig::default(),
}
}
pub fn max_message_size(mut self, size: usize) -> Result<Self, String> {
if size == 0 {
return Err("Maximum message size cannot be zero".to_string());
}
if size > 1024 * 1024 * 1024 {
return Err("Maximum message size cannot exceed 1GB".to_string());
}
self.config.max_message_size = size;
Ok(self)
}
pub fn timeout_ms(mut self, timeout: u64) -> Result<Self, String> {
if timeout == 0 {
return Err("Timeout cannot be zero".to_string());
}
if timeout > 10 * 60 * 1000 {
return Err("Timeout cannot exceed 10 minutes".to_string());
}
self.config.timeout_ms = timeout;
Ok(self)
}
#[must_use]
pub const fn tracing_enabled(mut self, enabled: bool) -> Self {
self.config.tracing_enabled = enabled;
self
}
pub fn option<V: serde::Serialize>(
mut self,
key: &str,
value: V,
) -> Result<Self, serde_json::Error> {
let json_value = serde_json::to_value(value)?;
self.config.options.insert(key.to_string(), json_value);
Ok(self)
}
#[must_use]
pub fn build(self) -> CoreConfig {
self.config
}
}
impl Default for ConfigBuilder {
fn default() -> Self {
Self::new()
}
}