1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, Serialize, Deserialize)]
5pub struct Config {
6 pub bind_address: String,
8
9 pub port: u16,
11
12 pub default_partitions: u32,
14
15 pub enable_persistence: bool,
17
18 pub data_dir: String,
20
21 pub max_segment_size: u64,
23
24 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, log_level: "info".to_string(),
38 }
39 }
40}
41
42impl Config {
43 pub fn new() -> Self {
45 Self::default()
46 }
47
48 pub fn with_bind_address(mut self, address: String) -> Self {
50 self.bind_address = address;
51 self
52 }
53
54 pub fn with_port(mut self, port: u16) -> Self {
56 self.port = port;
57 self
58 }
59
60 pub fn with_default_partitions(mut self, partitions: u32) -> Self {
62 self.default_partitions = partitions;
63 self
64 }
65
66 pub fn with_persistence(mut self, enabled: bool) -> Self {
68 self.enable_persistence = enabled;
69 self
70 }
71
72 pub fn with_data_dir(mut self, data_dir: String) -> Self {
74 self.data_dir = data_dir;
75 self
76 }
77
78 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}