cf-gears-toolkit 0.10.0

Core ToolKit library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
//! Configuration gear for typed gear configuration access.
//!
//! This gear provides two distinct mechanisms for loading gear configuration:
//!
//! 1. **Lenient loading** (default): Falls back to `T::default()` when configuration is missing.
//!    - Used by `gear_config_or_default`
//!    - Allows gears to exist without configuration sections in the main config file
//!
//! 2. **Strict loading**: Requires configuration to be present and valid.
//!    - Used by `gear_config_required`
//!    - Returns errors when configuration is missing or invalid

use serde::de::DeserializeOwned;

/// Configuration error for typed config operations
#[derive(thiserror::Error, Debug)]
pub enum ConfigError {
    #[error("gear '{gear}' not found")]
    GearNotFound { gear: String },
    #[error("gear '{gear}' config must be an object")]
    InvalidGearStructure { gear: String },
    #[error("missing 'config' section in gear '{gear}'")]
    MissingConfigSection { gear: String },
    // Intentionally not named `source`; doing so would duplicate chained error output.
    #[error("invalid config for gear '{gear}': {cause}")]
    InvalidConfig {
        gear: String,
        cause: serde_json::Error,
    },
    #[error("variable expansion failed for gear '{gear}': {cause}")]
    VarExpand {
        gear: String,
        cause: toolkit_utils::var_expand::ExpandVarsError,
    },
}

/// Provider of gear-specific configuration (raw JSON sections only).
pub trait ConfigProvider: Send + Sync {
    /// Returns raw JSON section for the gear, if any.
    fn get_gear_config(&self, gear_name: &str) -> Option<&serde_json::Value>;
}

/// Lenient configuration loader that falls back to defaults.
///
/// This function provides forgiving behavior for gears that don't require configuration:
/// - If the gear is not present in config → returns `Ok(T::default())`
/// - If the gear value is not an object → returns `Ok(T::default())`
/// - If the gear has no "config" field → returns `Ok(T::default())`
/// - If "config" is present but invalid → returns `Err(ConfigError::InvalidConfig)`
///
/// Use this for gears that can operate with default configuration.
///
/// # Errors
/// Returns `ConfigError::InvalidConfig` if the config section exists but cannot be deserialized.
pub fn gear_config_or_default<T: DeserializeOwned + Default>(
    provider: &dyn ConfigProvider,
    gear_name: &str,
) -> Result<T, ConfigError> {
    // If gear not found, use defaults
    let Some(gear_raw) = provider.get_gear_config(gear_name) else {
        return Ok(T::default());
    };

    // If gear is not an object, use defaults
    let Some(obj) = gear_raw.as_object() else {
        return Ok(T::default());
    };

    // If no config section, use defaults
    let Some(config_section) = obj.get("config") else {
        return Ok(T::default());
    };

    // Config section exists, try to parse it
    let config: T =
        serde_json::from_value(config_section.clone()).map_err(|e| ConfigError::InvalidConfig {
            gear: gear_name.to_owned(),
            cause: e,
        })?;

    Ok(config)
}

/// Strict configuration loader that requires configuration to be present.
///
/// This function enforces that configuration must exist and be valid:
/// - If the gear is not present → returns `Err(ConfigError::GearNotFound)`
/// - If the gear value is not an object → returns `Err(ConfigError::InvalidGearStructure)`
/// - If the gear has no "config" field → returns `Err(ConfigError::MissingConfigSection)`
/// - If "config" is present but invalid → returns `Err(ConfigError::InvalidConfig)`
///
/// Use this for gears that cannot operate without explicit configuration.
///
/// # Errors
/// Returns `ConfigError` if the gear is not found, has invalid structure, or config is invalid.
pub fn gear_config_required<T: DeserializeOwned>(
    provider: &dyn ConfigProvider,
    gear_name: &str,
) -> Result<T, ConfigError> {
    let gear_raw =
        provider
            .get_gear_config(gear_name)
            .ok_or_else(|| ConfigError::GearNotFound {
                gear: gear_name.to_owned(),
            })?;

    // Extract config section from: gears.<name> = { database: ..., config: ... }
    let obj = gear_raw
        .as_object()
        .ok_or_else(|| ConfigError::InvalidGearStructure {
            gear: gear_name.to_owned(),
        })?;

    let config_section = obj
        .get("config")
        .ok_or_else(|| ConfigError::MissingConfigSection {
            gear: gear_name.to_owned(),
        })?;

    let config: T =
        serde_json::from_value(config_section.clone()).map_err(|e| ConfigError::InvalidConfig {
            gear: gear_name.to_owned(),
            cause: e,
        })?;

    Ok(config)
}

#[cfg(test)]
#[cfg_attr(coverage_nightly, coverage(off))]
mod tests {
    use super::*;
    use serde::Deserialize;
    use serde_json::json;
    use std::collections::HashMap;

    #[derive(Debug, PartialEq, Deserialize, Default)]
    struct TestConfig {
        #[serde(default)]
        api_key: String,
        #[serde(default)]
        timeout_ms: u64,
        #[serde(default)]
        enabled: bool,
    }

    struct MockConfigProvider {
        gears: HashMap<String, serde_json::Value>,
    }

    impl MockConfigProvider {
        fn new() -> Self {
            let mut gears = HashMap::new();

            // Valid gear config
            gears.insert(
                "test_gear".to_owned(),
                json!({
                    "database": {
                        "url": "postgres://localhost/test"
                    },
                    "config": {
                        "api_key": "secret123",
                        "timeout_ms": 5000,
                        "enabled": true
                    }
                }),
            );

            // Gear without config section
            gears.insert(
                "no_config_gear".to_owned(),
                json!({
                    "database": {
                        "url": "postgres://localhost/test"
                    }
                }),
            );

            // Gear with invalid structure (not an object)
            gears.insert("invalid_gear".to_owned(), json!("not an object"));

            Self { gears }
        }
    }

    impl ConfigProvider for MockConfigProvider {
        fn get_gear_config(&self, gear_name: &str) -> Option<&serde_json::Value> {
            self.gears.get(gear_name)
        }
    }

    // ========== Tests for lenient loading (gear_config_or_default) ==========

    #[test]
    fn test_lenient_success() {
        let provider = MockConfigProvider::new();
        let result: Result<TestConfig, ConfigError> =
            gear_config_or_default(&provider, "test_gear");

        assert!(result.is_ok());
        let config = result.unwrap();
        assert_eq!(config.api_key, "secret123");
        assert_eq!(config.timeout_ms, 5000);
        assert!(config.enabled);
    }

    #[test]
    fn test_lenient_gear_not_found_returns_default() {
        let provider = MockConfigProvider::new();
        let result: Result<TestConfig, ConfigError> =
            gear_config_or_default(&provider, "nonexistent");

        assert!(result.is_ok());
        let config = result.unwrap();
        assert_eq!(config, TestConfig::default());
    }

    #[test]
    fn test_lenient_missing_config_section_returns_default() {
        let provider = MockConfigProvider::new();
        let result: Result<TestConfig, ConfigError> =
            gear_config_or_default(&provider, "no_config_gear");

        assert!(result.is_ok());
        let config = result.unwrap();
        assert_eq!(config, TestConfig::default());
    }

    #[test]
    fn test_lenient_invalid_structure_returns_default() {
        let provider = MockConfigProvider::new();
        let result: Result<TestConfig, ConfigError> =
            gear_config_or_default(&provider, "invalid_gear");

        assert!(result.is_ok());
        let config = result.unwrap();
        assert_eq!(config, TestConfig::default());
    }

    #[test]
    fn test_lenient_invalid_config_returns_error() {
        let mut provider = MockConfigProvider::new();
        // Add gear with invalid config structure
        provider.gears.insert(
            "bad_config_gear".to_owned(),
            json!({
                "config": {
                    "api_key": "secret123",
                    "timeout_ms": "not_a_number", // Should be u64
                    "enabled": true
                }
            }),
        );

        let result: Result<TestConfig, ConfigError> =
            gear_config_or_default(&provider, "bad_config_gear");

        assert!(matches!(result, Err(ConfigError::InvalidConfig { .. })));
        if let Err(ConfigError::InvalidConfig { gear, .. }) = result {
            assert_eq!(gear, "bad_config_gear");
        }
    }

    #[test]
    fn test_lenient_helper_with_multiple_scenarios() {
        let provider = MockConfigProvider::new();

        // Gear not found should return default
        let result: Result<TestConfig, ConfigError> =
            gear_config_or_default(&provider, "nonexistent");
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), TestConfig::default());

        // Valid config should parse correctly
        let result: Result<TestConfig, ConfigError> =
            gear_config_or_default(&provider, "test_gear");
        assert!(result.is_ok());
        let config = result.unwrap();
        assert_eq!(config.api_key, "secret123");
    }

    // ========== Tests for strict loading (gear_config_required) ==========

    #[test]
    fn test_strict_success() {
        let provider = MockConfigProvider::new();
        let result: Result<TestConfig, ConfigError> = gear_config_required(&provider, "test_gear");

        assert!(result.is_ok());
        let config = result.unwrap();
        assert_eq!(config.api_key, "secret123");
        assert_eq!(config.timeout_ms, 5000);
        assert!(config.enabled);
    }

    #[test]
    fn test_strict_gear_not_found() {
        let provider = MockConfigProvider::new();
        let result: Result<TestConfig, ConfigError> =
            gear_config_required(&provider, "nonexistent");

        assert!(matches!(result, Err(ConfigError::GearNotFound { .. })));
        if let Err(ConfigError::GearNotFound { gear }) = result {
            assert_eq!(gear, "nonexistent");
        }
    }

    #[test]
    fn test_strict_missing_config_section() {
        let provider = MockConfigProvider::new();
        let result: Result<TestConfig, ConfigError> =
            gear_config_required(&provider, "no_config_gear");

        assert!(matches!(
            result,
            Err(ConfigError::MissingConfigSection { .. })
        ));
        if let Err(ConfigError::MissingConfigSection { gear }) = result {
            assert_eq!(gear, "no_config_gear");
        }
    }

    #[test]
    fn test_strict_invalid_structure() {
        let provider = MockConfigProvider::new();
        let result: Result<TestConfig, ConfigError> =
            gear_config_required(&provider, "invalid_gear");

        assert!(matches!(
            result,
            Err(ConfigError::InvalidGearStructure { .. })
        ));
        if let Err(ConfigError::InvalidGearStructure { gear }) = result {
            assert_eq!(gear, "invalid_gear");
        }
    }

    #[test]
    fn test_strict_invalid_config() {
        let mut provider = MockConfigProvider::new();
        // Add gear with invalid config structure
        provider.gears.insert(
            "bad_config_gear".to_owned(),
            json!({
                "config": {
                    "api_key": "secret123",
                    "timeout_ms": "not_a_number", // Should be u64
                    "enabled": true
                }
            }),
        );

        let result: Result<TestConfig, ConfigError> =
            gear_config_required(&provider, "bad_config_gear");

        assert!(matches!(result, Err(ConfigError::InvalidConfig { .. })));
        if let Err(ConfigError::InvalidConfig { gear, .. }) = result {
            assert_eq!(gear, "bad_config_gear");
        }
    }

    // ========== Tests for ConfigError display messages ==========

    #[test]
    fn test_config_error_messages() {
        let gear_not_found = ConfigError::GearNotFound {
            gear: "test".to_owned(),
        };
        assert_eq!(gear_not_found.to_string(), "gear 'test' not found");

        let invalid_structure = ConfigError::InvalidGearStructure {
            gear: "test".to_owned(),
        };
        assert_eq!(
            invalid_structure.to_string(),
            "gear 'test' config must be an object"
        );

        let missing_config = ConfigError::MissingConfigSection {
            gear: "test".to_owned(),
        };
        assert_eq!(
            missing_config.to_string(),
            "missing 'config' section in gear 'test'"
        );
    }

    #[test]
    fn test_invalid_config_message_is_not_duplicated_by_source_chain() {
        let mut provider = MockConfigProvider::new();
        provider.gears.insert(
            "bad_config_gear".to_owned(),
            json!({
                "config": {
                    "api_key": "secret123",
                    "timeout_ms": "not_a_number",
                    "enabled": true
                }
            }),
        );
        let result: Result<TestConfig, ConfigError> =
            gear_config_required(&provider, "bad_config_gear");
        let err = result.unwrap_err();

        assert_eq!(
            err.to_string(),
            "invalid config for gear 'bad_config_gear': invalid type: string \"not_a_number\", expected u64"
        );

        assert!(std::error::Error::source(&err).is_none());
        let anyhow_err: anyhow::Error = err.into();
        assert_eq!(
            format!("{anyhow_err:#}"),
            "invalid config for gear 'bad_config_gear': invalid type: string \"not_a_number\", expected u64"
        );
    }

    #[test]
    fn test_var_expand_message_is_not_duplicated_by_source_chain() {
        let err = ConfigError::VarExpand {
            gear: "test".to_owned(),
            cause: toolkit_utils::var_expand::ExpandVarsError::Regex("boom".to_owned()),
        };

        assert_eq!(
            err.to_string(),
            "variable expansion failed for gear 'test': env expansion regex error: boom"
        );
        assert!(std::error::Error::source(&err).is_none());
    }
}