Skip to main content

rivven_core/
config.rs

1use serde::{Deserialize, Serialize};
2
3/// Configuration for Rivven
4#[derive(Debug, Clone, Serialize, Deserialize)]
5pub struct Config {
6    /// Server bind address
7    pub bind_address: String,
8
9    /// Server port
10    pub port: u16,
11
12    /// Default number of partitions for new topics
13    pub default_partitions: u32,
14
15    /// Enable disk persistence
16    pub enable_persistence: bool,
17
18    /// Data directory for persistence
19    pub data_dir: String,
20
21    /// Maximum segment size in bytes
22    pub max_segment_size: u64,
23
24    /// Log level
25    pub log_level: String,
26}
27
28impl Default for Config {
29    fn default() -> Self {
30        Self {
31            bind_address: "127.0.0.1".to_string(),
32            port: 9092,
33            default_partitions: 3,
34            enable_persistence: false,
35            data_dir: "./data".to_string(),
36            max_segment_size: 1024 * 1024 * 1024, // 1GB
37            log_level: "info".to_string(),
38        }
39    }
40}
41
42impl Config {
43    /// Create a new configuration
44    pub fn new() -> Self {
45        Self::default()
46    }
47
48    /// Set the bind address
49    pub fn with_bind_address(mut self, address: String) -> Self {
50        self.bind_address = address;
51        self
52    }
53
54    /// Set the port
55    pub fn with_port(mut self, port: u16) -> Self {
56        self.port = port;
57        self
58    }
59
60    /// Set the default partitions
61    pub fn with_default_partitions(mut self, partitions: u32) -> Self {
62        self.default_partitions = partitions;
63        self
64    }
65
66    /// Enable or disable persistence
67    pub fn with_persistence(mut self, enabled: bool) -> Self {
68        self.enable_persistence = enabled;
69        self
70    }
71
72    /// Set the data directory
73    pub fn with_data_dir(mut self, data_dir: String) -> Self {
74        self.data_dir = data_dir;
75        self
76    }
77
78    /// Get the server address
79    pub fn server_address(&self) -> String {
80        format!("{}:{}", self.bind_address, self.port)
81    }
82}
83
84#[cfg(test)]
85mod tests {
86    use super::*;
87
88    #[test]
89    fn test_default_config() {
90        let config = Config::default();
91        assert_eq!(config.bind_address, "127.0.0.1");
92        assert_eq!(config.port, 9092);
93        assert_eq!(config.default_partitions, 3);
94        assert!(!config.enable_persistence);
95        assert_eq!(config.data_dir, "./data");
96        assert_eq!(config.max_segment_size, 1024 * 1024 * 1024);
97        assert_eq!(config.log_level, "info");
98    }
99
100    #[test]
101    fn test_builder_pattern() {
102        let config = Config::new()
103            .with_bind_address("0.0.0.0".to_string())
104            .with_port(9093)
105            .with_default_partitions(6)
106            .with_persistence(true)
107            .with_data_dir("/var/lib/rivven".to_string());
108
109        assert_eq!(config.bind_address, "0.0.0.0");
110        assert_eq!(config.port, 9093);
111        assert_eq!(config.default_partitions, 6);
112        assert!(config.enable_persistence);
113        assert_eq!(config.data_dir, "/var/lib/rivven");
114    }
115
116    #[test]
117    fn test_server_address() {
118        let config = Config::default();
119        assert_eq!(config.server_address(), "127.0.0.1:9092");
120
121        let custom = Config::new()
122            .with_bind_address("10.0.0.1".to_string())
123            .with_port(9999);
124        assert_eq!(custom.server_address(), "10.0.0.1:9999");
125    }
126
127    #[test]
128    fn test_serialization() {
129        let config = Config::default();
130        let json = serde_json::to_string(&config).unwrap();
131        let deserialized: Config = serde_json::from_str(&json).unwrap();
132
133        assert_eq!(config.bind_address, deserialized.bind_address);
134        assert_eq!(config.port, deserialized.port);
135        assert_eq!(config.default_partitions, deserialized.default_partitions);
136    }
137}