1use serde::{Deserialize, Serialize};
14
15pub trait ToConnectionUrl {
19 fn to_connection_url(&self) -> String;
21}
22
23#[derive(Debug, Clone, Serialize, Deserialize, Default)]
27pub struct DatabaseConfig {
28 #[serde(default)]
30 pub mongodb: MongoDbConfig,
31 #[serde(default)]
33 pub mysql: MySqlConfig,
34 #[serde(default)]
36 pub postgresql: PostgreSqlConfig,
37 #[serde(default)]
39 pub sqlite: SqliteConfig,
40 #[serde(default)]
42 pub qdrant: QdrantConfig,
43 #[serde(default)]
45 pub seekdb: SeekDbConfig,
46 #[serde(default)]
48 pub neo4j: Neo4jConfig,
49}
50
51#[derive(Debug, Clone, Serialize, Deserialize)]
72pub struct MongoDbConfig {
73 pub host: String,
75 pub port: u16,
77 pub database: String,
79 pub username: String,
81 pub password: String,
83 pub max_pool_size: u32,
85 pub connect_timeout: u64,
87}
88
89impl Default for MongoDbConfig {
90 fn default() -> Self {
91 Self {
92 host: "localhost".to_string(),
93 port: 27017,
94 database: String::new(),
95 username: String::new(),
96 password: String::new(),
97 max_pool_size: 10,
98 connect_timeout: 10,
99 }
100 }
101}
102
103impl ToConnectionUrl for MongoDbConfig {
104 fn to_connection_url(&self) -> String {
105 if self.username.is_empty() {
106 format!("mongodb://{}:{}/{}", self.host, self.port, self.database)
107 } else {
108 format!(
109 "mongodb://{}:{}@{}:{}/{}",
110 self.username, self.password, self.host, self.port, self.database
111 )
112 }
113 }
114}
115
116#[derive(Debug, Clone, Serialize, Deserialize)]
127pub struct MySqlConfig {
128 pub host: String,
130 pub port: u16,
132 pub database: String,
134 pub username: String,
136 pub password: String,
138 pub max_pool_size: u32,
140 pub connect_timeout: u64,
142}
143
144impl Default for MySqlConfig {
145 fn default() -> Self {
146 Self {
147 host: "localhost".to_string(),
148 port: 3306,
149 database: String::new(),
150 username: String::new(),
151 password: String::new(),
152 max_pool_size: 10,
153 connect_timeout: 10,
154 }
155 }
156}
157
158impl ToConnectionUrl for MySqlConfig {
159 fn to_connection_url(&self) -> String {
160 let mut url = "mysql://".to_string();
161
162 if !self.username.is_empty() && !self.password.is_empty() {
163 url.push_str(&format!("{}:{}@", self.username, self.password));
164 } else if !self.username.is_empty() {
165 url.push_str(&format!("{}@", self.username));
166 }
167
168 url.push_str(&format!("{}:{}", self.host, self.port));
169
170 if !self.database.is_empty() {
171 url.push_str(&format!("/{}", self.database));
172 }
173
174 url
175 }
176}
177
178#[derive(Debug, Clone, Serialize, Deserialize)]
189pub struct PostgreSqlConfig {
190 pub host: String,
192 pub port: u16,
194 pub database: String,
196 pub username: String,
198 pub password: String,
200 pub max_pool_size: u32,
202 pub connect_timeout: u64,
204}
205
206impl Default for PostgreSqlConfig {
207 fn default() -> Self {
208 Self {
209 host: "localhost".to_string(),
210 port: 5432,
211 database: String::new(),
212 username: String::new(),
213 password: String::new(),
214 max_pool_size: 10,
215 connect_timeout: 10,
216 }
217 }
218}
219
220impl ToConnectionUrl for PostgreSqlConfig {
221 fn to_connection_url(&self) -> String {
222 let mut url = "postgresql://".to_string();
223
224 if !self.username.is_empty() && !self.password.is_empty() {
225 url.push_str(&format!("{}:{}@", self.username, self.password));
226 } else if !self.username.is_empty() {
227 url.push_str(&format!("{}@", self.username));
228 }
229
230 url.push_str(&format!("{}:{}", self.host, self.port));
231
232 if !self.database.is_empty() {
233 url.push_str(&format!("/{}", self.database));
234 }
235
236 url
237 }
238}
239
240#[derive(Debug, Clone, Serialize, Deserialize)]
251pub struct SqliteConfig {
252 pub path: String,
254 pub max_pool_size: u32,
256}
257
258impl Default for SqliteConfig {
259 fn default() -> Self {
260 Self {
261 path: "./data.db".to_string(),
262 max_pool_size: 10,
263 }
264 }
265}
266
267impl ToConnectionUrl for SqliteConfig {
268 fn to_connection_url(&self) -> String {
269 format!("sqlite://{}", self.path)
270 }
271}
272
273#[derive(Debug, Clone, Serialize, Deserialize)]
284pub struct QdrantConfig {
285 pub host: String,
287 pub port: u16,
289 pub api_key: Option<String>,
291 pub use_https: bool,
293}
294
295impl Default for QdrantConfig {
296 fn default() -> Self {
297 Self {
298 host: "localhost".to_string(),
299 port: 6333,
300 api_key: None,
301 use_https: false,
302 }
303 }
304}
305
306impl ToConnectionUrl for QdrantConfig {
307 fn to_connection_url(&self) -> String {
308 let protocol = if self.use_https { "https" } else { "http" };
309 format!("{}://{}:{}", protocol, self.host, self.port)
310 }
311}
312
313#[derive(Debug, Clone, Serialize, Deserialize)]
324pub struct SeekDbConfig {
325 pub host: String,
327 pub port: u16,
329 pub database: String,
331 pub username: String,
333 pub password: String,
335}
336
337impl Default for SeekDbConfig {
338 fn default() -> Self {
339 Self {
340 host: "localhost".to_string(),
341 port: 2881,
342 database: String::new(),
343 username: String::new(),
344 password: String::new(),
345 }
346 }
347}
348
349impl ToConnectionUrl for SeekDbConfig {
350 fn to_connection_url(&self) -> String {
351 if self.username.is_empty() {
352 format!("mysql://{}:{}/{}", self.host, self.port, self.database)
353 } else {
354 format!(
355 "mysql://{}:{}@{}:{}/{}",
356 self.username, self.password, self.host, self.port, self.database
357 )
358 }
359 }
360}
361
362#[derive(Debug, Clone, Serialize, Deserialize)]
374pub struct Neo4jConfig {
375 pub host: String,
377 pub port: u16,
379 pub database: String,
381 pub username: String,
383 pub password: String,
385 pub use_encryption: bool,
387 pub max_pool_size: u32,
389 pub connect_timeout: u64,
391}
392
393impl Default for Neo4jConfig {
394 fn default() -> Self {
395 Self {
396 host: "localhost".to_string(),
397 port: 7687,
398 database: "neo4j".to_string(),
399 username: String::new(),
400 password: String::new(),
401 use_encryption: false,
402 max_pool_size: 10,
403 connect_timeout: 10,
404 }
405 }
406}
407
408impl ToConnectionUrl for Neo4jConfig {
409 fn to_connection_url(&self) -> String {
410 let protocol = if self.use_encryption { "neo4j+s" } else { "neo4j" };
411
412 if self.username.is_empty() {
413 format!("{}://{}:{}", protocol, self.host, self.port)
414 } else {
415 format!(
416 "{}://{}:{}@{}:{}",
417 protocol, self.username, self.password, self.host, self.port
418 )
419 }
420 }
421}