Skip to main content

recoco_core/
settings.rs

1// ReCoco is a Rust-only fork of CocoIndex, by [CocoIndex](https://CocoIndex)
2// Original code from CocoIndex is copyrighted by CocoIndex
3// SPDX-FileCopyrightText: 2025-2026 CocoIndex (upstream)
4// SPDX-FileContributor: CocoIndex Contributors
5//
6// All modifications from the upstream for ReCoco are copyrighted by Knitli Inc.
7// SPDX-FileCopyrightText: 2026 Knitli Inc. (ReCoco)
8// SPDX-FileContributor: Adam Poulemanos <adam@knit.li>
9//
10// Both the upstream CocoIndex code and the ReCoco modifications are licensed under the Apache-2.0 License.
11// SPDX-License-Identifier: Apache-2.0
12
13use serde::Deserialize;
14
15#[derive(Deserialize, Debug)]
16pub struct DatabaseConnectionSpec {
17    pub url: String,
18    pub user: Option<String>,
19    pub password: Option<String>,
20    pub max_connections: u32,
21    pub min_connections: u32,
22}
23
24#[derive(Deserialize, Debug, Default)]
25pub struct GlobalExecutionOptions {
26    pub source_max_inflight_rows: Option<usize>,
27    pub source_max_inflight_bytes: Option<usize>,
28}
29
30#[derive(Deserialize, Debug, Default)]
31pub struct Settings {
32    #[serde(default)]
33    pub database: Option<DatabaseConnectionSpec>,
34    #[serde(default)]
35    #[allow(dead_code)] // Used via serialization/deserialization to Python
36    pub app_namespace: String,
37    #[serde(default)]
38    pub global_execution_options: GlobalExecutionOptions,
39    #[serde(default)]
40    pub ignore_target_drop_failures: bool,
41}
42
43#[cfg(test)]
44mod tests {
45    use super::*;
46
47    #[test]
48    fn test_settings_deserialize_with_database() {
49        let json = r#"{
50            "database": {
51                "url": "postgresql://localhost:5432/test",
52                "user": "testuser",
53                "password": "testpass",
54                "min_connections": 1,
55                "max_connections": 10
56            },
57            "app_namespace": "test_app"
58        }"#;
59
60        let settings: Settings = serde_json::from_str(json).unwrap();
61
62        assert!(settings.database.is_some());
63        let db = settings.database.unwrap();
64        assert_eq!(db.url, "postgresql://localhost:5432/test");
65        assert_eq!(db.user, Some("testuser".to_string()));
66        assert_eq!(db.password, Some("testpass".to_string()));
67        assert_eq!(db.min_connections, 1);
68        assert_eq!(db.max_connections, 10);
69        assert_eq!(settings.app_namespace, "test_app");
70    }
71
72    #[test]
73    fn test_settings_deserialize_without_database() {
74        let json = r#"{
75            "app_namespace": "test_app"
76        }"#;
77
78        let settings: Settings = serde_json::from_str(json).unwrap();
79
80        assert!(settings.database.is_none());
81        assert_eq!(settings.app_namespace, "test_app");
82    }
83
84    #[test]
85    fn test_settings_deserialize_empty_object() {
86        let json = r#"{}"#;
87
88        let settings: Settings = serde_json::from_str(json).unwrap();
89
90        assert!(settings.database.is_none());
91        assert_eq!(settings.app_namespace, "");
92    }
93
94    #[test]
95    fn test_settings_deserialize_database_without_user_password() {
96        let json = r#"{
97            "database": {
98                "url": "postgresql://localhost:5432/test",
99                "min_connections": 1,
100                "max_connections": 10
101            }
102        }"#;
103
104        let settings: Settings = serde_json::from_str(json).unwrap();
105
106        assert!(settings.database.is_some());
107        let db = settings.database.unwrap();
108        assert_eq!(db.url, "postgresql://localhost:5432/test");
109        assert_eq!(db.user, None);
110        assert_eq!(db.password, None);
111        assert_eq!(db.min_connections, 1);
112        assert_eq!(db.max_connections, 10);
113        assert_eq!(settings.app_namespace, "");
114    }
115
116    #[test]
117    fn test_database_connection_spec_deserialize() {
118        let json = r#"{
119            "url": "postgresql://localhost:5432/test",
120            "user": "testuser",
121            "password": "testpass",
122            "min_connections": 1,
123            "max_connections": 10
124        }"#;
125
126        let db_spec: DatabaseConnectionSpec = serde_json::from_str(json).unwrap();
127
128        assert_eq!(db_spec.url, "postgresql://localhost:5432/test");
129        assert_eq!(db_spec.user, Some("testuser".to_string()));
130        assert_eq!(db_spec.password, Some("testpass".to_string()));
131        assert_eq!(db_spec.min_connections, 1);
132        assert_eq!(db_spec.max_connections, 10);
133    }
134}