foxy-io 0.3.4

A configuration-driven and hyper-extensible HTTP proxy 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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

//! File-based configuration provider implementation.

use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};
use serde_json;
use toml;

use super::ConfigProvider;
use super::ConfigError;

/// Supported file formats for configuration.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FileFormat {
    /// JSON format (.json)
    Json,
    /// TOML format (.toml)
    Toml,
    /// YAML format (.yaml, .yml)
    Yaml,
}

impl FileFormat {
    /// Detect the file format from the file extension.
    pub fn from_extension(path: &Path) -> Option<Self> {
        path.extension().and_then(|ext| {
            let ext_str = ext.to_string_lossy().to_lowercase();
            match ext_str.as_str() {
                "json" => Some(FileFormat::Json),
                "toml" => Some(FileFormat::Toml),
                "yaml" | "yml" => Some(FileFormat::Yaml),
                _ => None,
            }
        })
    }
}

/// File-based configuration provider.
#[derive(Debug)]
pub struct FileConfigProvider {
    #[allow(dead_code)]
    path: PathBuf,
    #[allow(dead_code)]
    format: FileFormat,
    data: HashMap<String, serde_json::Value>,
}

impl FileConfigProvider {
    /// Create a new file-based configuration provider.
    pub fn new(path: &str) -> Result<Self, ConfigError> {
        let path_buf = PathBuf::from(path);
        let format = FileFormat::from_extension(&path_buf)
            .ok_or_else(|| ConfigError::provider_error("file", "unsupported file format"))?;

        let data = Self::read_file(&path_buf, format)?;

        Ok(Self {
            path: path_buf,
            format,
            data,
        })
    }

    /// Read and parse a configuration file.
    fn read_file(path: &Path, format: FileFormat) -> Result<HashMap<String, serde_json::Value>, ConfigError> {
        let content = fs::read_to_string(path)
            .map_err(|e| ConfigError::provider_error("file", format!("failed to read file: {e}")))?;

        match format {
            FileFormat::Json => {
                serde_json::from_str(&content)
                    .map_err(|e| ConfigError::provider_error("file", format!("invalid JSON: {e}")))
            },
            FileFormat::Toml => {
                let toml_value: toml::Value = toml::from_str(&content)
                    .map_err(|e| ConfigError::provider_error("file", format!("invalid TOML: {e}")))?;

                // Convert toml::Value to serde_json::Value for unified internal representation
                let json_value = serde_json::to_value(toml_value)
                    .map_err(|e| ConfigError::provider_error("file", format!("failed to convert TOML: {e}")))?;

                match json_value {
                    serde_json::Value::Object(map) => {
                        // Convert the map to our format
                        Ok(map.into_iter().collect())
                    },
                    _ => Err(ConfigError::provider_error("file", "root configuration must be an object")),
                }
            },
            FileFormat::Yaml => {
                {
                    let yaml_value: serde_yaml::Value = serde_yaml::from_str(&content)
                        .map_err(|e| ConfigError::provider_error("file", format!("invalid YAML: {e}")))?;

                    let json_value = serde_json::to_value(yaml_value)
                        .map_err(|e| ConfigError::provider_error("file", format!("failed to convert YAML: {e}")))?;

                    match json_value {
                        serde_json::Value::Object(map) => {
                            Ok(map.into_iter().collect())
                        },
                        _ => Err(ConfigError::provider_error("file", "root configuration must be an object")),
                    }
                }
            },
        }
    }

    /// Get a nested value from the configuration by a dot-separated key path.
    fn get_nested_value(&self, key_path: &str) -> Option<&serde_json::Value> {
        let parts: Vec<&str> = key_path.split('.').collect();

        let mut current = self.data.get(parts[0])?;

        for part in parts.iter().skip(1) {
            current = current.get(part)?;
        }

        Some(current)
    }
}

impl ConfigProvider for FileConfigProvider {
    fn has(&self, key: &str) -> bool {
        self.get_nested_value(key).is_some()
    }

    fn provider_name(&self) -> &str {
        "file"
    }

    fn get_raw(&self, key: &str) -> Result<Option<serde_json::Value>, ConfigError> {
        match self.get_nested_value(key) {
            Some(value) => Ok(Some(value.clone())),
            None => Ok(None),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs::File;
    use std::io::Write;
    use tempfile::tempdir;
    use crate::config::ConfigProviderExt;

    #[test]
    fn test_file_format_detection() {
        assert_eq!(FileFormat::from_extension(Path::new("config.json")), Some(FileFormat::Json));
        assert_eq!(FileFormat::from_extension(Path::new("config.toml")), Some(FileFormat::Toml));
        assert_eq!(FileFormat::from_extension(Path::new("config.yaml")), Some(FileFormat::Yaml));
        assert_eq!(FileFormat::from_extension(Path::new("config.yml")), Some(FileFormat::Yaml));
        assert_eq!(FileFormat::from_extension(Path::new("config.txt")), None);
    }

    #[test]
    fn test_json_config() {
        let dir = tempdir().unwrap();
        let file_path = dir.path().join("config.json");

        let content = r#"{
            "server": {
                "host": "127.0.0.1",
                "port": 8080
            },
            "timeout": 30
        }"#;

        let mut file = File::create(&file_path).unwrap();
        file.write_all(content.as_bytes()).unwrap();

        let provider = FileConfigProvider::new(file_path.to_str().unwrap()).unwrap();

        assert_eq!(provider.has("server.host"), true);
        assert_eq!(provider.has("server.nonexistent"), false);

        let host: String = provider.get("server.host").unwrap().unwrap();
        assert_eq!(host, "127.0.0.1");

        let port: u16 = provider.get("server.port").unwrap().unwrap();
        assert_eq!(port, 8080);

        let timeout: u32 = provider.get("timeout").unwrap().unwrap();
        assert_eq!(timeout, 30);
    }

    #[test]
    fn test_toml_config() {
        let dir = tempdir().unwrap();
        let file_path = dir.path().join("config.toml");

        let content = r#"
            [server]
            host = "127.0.0.1"
            port = 8080

            timeout = 30
        "#;

        let mut file = File::create(&file_path).unwrap();
        file.write_all(content.as_bytes()).unwrap();

        let provider = FileConfigProvider::new(file_path.to_str().unwrap()).unwrap();

        assert_eq!(provider.has("server.host"), true);
        let host: String = provider.get("server.host").unwrap().unwrap();
        assert_eq!(host, "127.0.0.1");

        let port: u16 = provider.get("server.port").unwrap().unwrap();
        assert_eq!(port, 8080);
    }

    #[test]
    fn test_yaml_config() {
        let dir = tempdir().unwrap();
        let file_path = dir.path().join("config.yaml");

        let content = r#"
server:
  host: "127.0.0.1"
  port: 8080
timeout: 30
debug: true
"#;

        let mut file = File::create(&file_path).unwrap();
        file.write_all(content.as_bytes()).unwrap();

        let provider = FileConfigProvider::new(file_path.to_str().unwrap()).unwrap();

        assert_eq!(provider.has("server.host"), true);
        let host: String = provider.get("server.host").unwrap().unwrap();
        assert_eq!(host, "127.0.0.1");

        let port: u16 = provider.get("server.port").unwrap().unwrap();
        assert_eq!(port, 8080);

        let timeout: u32 = provider.get("timeout").unwrap().unwrap();
        assert_eq!(timeout, 30);

        let debug: bool = provider.get("debug").unwrap().unwrap();
        assert_eq!(debug, true);
    }

    #[test]
    fn test_yml_extension() {
        let dir = tempdir().unwrap();
        let file_path = dir.path().join("config.yml");

        let content = r#"
server:
  host: "127.0.0.1"
  port: 8080
"#;

        let mut file = File::create(&file_path).unwrap();
        file.write_all(content.as_bytes()).unwrap();

        let provider = FileConfigProvider::new(file_path.to_str().unwrap()).unwrap();

        assert_eq!(provider.has("server.host"), true);
        let host: String = provider.get("server.host").unwrap().unwrap();
        assert_eq!(host, "127.0.0.1");
    }

    #[test]
    fn test_unsupported_file_format() {
        let dir = tempdir().unwrap();
        let file_path = dir.path().join("config.txt");

        let mut file = File::create(&file_path).unwrap();
        file.write_all(b"some content").unwrap();

        let result = FileConfigProvider::new(file_path.to_str().unwrap());
        assert!(result.is_err());
        if let Err(ConfigError::ProviderError { provider, message }) = result {
            assert_eq!(provider, "file");
            assert!(message.contains("unsupported file format"));
        } else {
            panic!("Expected ProviderError for unsupported file format");
        }
    }

    #[test]
    fn test_nonexistent_file() {
        let result = FileConfigProvider::new("/nonexistent/path/config.json");
        assert!(result.is_err());
        if let Err(ConfigError::ProviderError { provider, message }) = result {
            assert_eq!(provider, "file");
            assert!(message.contains("failed to read file"));
        } else {
            panic!("Expected ProviderError for nonexistent file");
        }
    }

    #[test]
    fn test_invalid_json() {
        let dir = tempdir().unwrap();
        let file_path = dir.path().join("config.json");

        let content = r#"{ invalid json }"#;

        let mut file = File::create(&file_path).unwrap();
        file.write_all(content.as_bytes()).unwrap();

        let result = FileConfigProvider::new(file_path.to_str().unwrap());
        assert!(result.is_err());
        if let Err(ConfigError::ProviderError { provider, message }) = result {
            assert_eq!(provider, "file");
            assert!(message.contains("invalid JSON"));
        } else {
            panic!("Expected ProviderError for invalid JSON");
        }
    }

    #[test]
    fn test_invalid_toml() {
        let dir = tempdir().unwrap();
        let file_path = dir.path().join("config.toml");

        let content = r#"
            [server
            host = "127.0.0.1"
        "#; // Missing closing bracket

        let mut file = File::create(&file_path).unwrap();
        file.write_all(content.as_bytes()).unwrap();

        let result = FileConfigProvider::new(file_path.to_str().unwrap());
        assert!(result.is_err());
        if let Err(ConfigError::ProviderError { provider, message }) = result {
            assert_eq!(provider, "file");
            assert!(message.contains("invalid TOML"));
        } else {
            panic!("Expected ProviderError for invalid TOML");
        }
    }

    #[test]
    fn test_invalid_yaml() {
        let dir = tempdir().unwrap();
        let file_path = dir.path().join("config.yaml");

        let content = r#"
server:
  host: "127.0.0.1"
  port: 8080
    invalid_indentation: true
"#; // Invalid YAML indentation

        let mut file = File::create(&file_path).unwrap();
        file.write_all(content.as_bytes()).unwrap();

        let result = FileConfigProvider::new(file_path.to_str().unwrap());
        assert!(result.is_err());
        if let Err(ConfigError::ProviderError { provider, message }) = result {
            assert_eq!(provider, "file");
            assert!(message.contains("invalid YAML"));
        } else {
            panic!("Expected ProviderError for invalid YAML");
        }
    }

    #[test]
    fn test_complex_nested_structure() {
        let dir = tempdir().unwrap();
        let file_path = dir.path().join("config.json");

        let content = r#"{
            "database": {
                "connections": {
                    "primary": {
                        "host": "db1.example.com",
                        "port": 5432,
                        "ssl": true
                    },
                    "secondary": {
                        "host": "db2.example.com",
                        "port": 5433,
                        "ssl": false
                    }
                },
                "pool": {
                    "min_size": 5,
                    "max_size": 20
                }
            },
            "cache": {
                "redis": {
                    "url": "redis://localhost:6379",
                    "timeout": 5000
                }
            }
        }"#;

        let mut file = File::create(&file_path).unwrap();
        file.write_all(content.as_bytes()).unwrap();

        let provider = FileConfigProvider::new(file_path.to_str().unwrap()).unwrap();

        // Test deeply nested values
        assert!(provider.has("database.connections.primary.host"));
        let primary_host: String = provider.get("database.connections.primary.host").unwrap().unwrap();
        assert_eq!(primary_host, "db1.example.com");

        let primary_port: u16 = provider.get("database.connections.primary.port").unwrap().unwrap();
        assert_eq!(primary_port, 5432);

        let primary_ssl: bool = provider.get("database.connections.primary.ssl").unwrap().unwrap();
        assert_eq!(primary_ssl, true);

        let secondary_ssl: bool = provider.get("database.connections.secondary.ssl").unwrap().unwrap();
        assert_eq!(secondary_ssl, false);

        let min_pool_size: u32 = provider.get("database.pool.min_size").unwrap().unwrap();
        assert_eq!(min_pool_size, 5);

        let redis_timeout: u32 = provider.get("cache.redis.timeout").unwrap().unwrap();
        assert_eq!(redis_timeout, 5000);

        // Test non-existent nested keys
        assert!(!provider.has("database.connections.tertiary.host"));
        assert!(!provider.has("cache.memcached.url"));
    }

    #[test]
    fn test_array_values() {
        let dir = tempdir().unwrap();
        let file_path = dir.path().join("config.json");

        let content = r#"{
            "servers": ["server1", "server2", "server3"],
            "ports": [8080, 8081, 8082],
            "features": {
                "enabled": ["auth", "logging", "metrics"],
                "disabled": ["debug", "profiling"]
            }
        }"#;

        let mut file = File::create(&file_path).unwrap();
        file.write_all(content.as_bytes()).unwrap();

        let provider = FileConfigProvider::new(file_path.to_str().unwrap()).unwrap();

        let servers: Vec<String> = provider.get("servers").unwrap().unwrap();
        assert_eq!(servers, vec!["server1", "server2", "server3"]);

        let ports: Vec<u16> = provider.get("ports").unwrap().unwrap();
        assert_eq!(ports, vec![8080, 8081, 8082]);

        let enabled_features: Vec<String> = provider.get("features.enabled").unwrap().unwrap();
        assert_eq!(enabled_features, vec!["auth", "logging", "metrics"]);
    }

    #[test]
    fn test_empty_file() {
        let dir = tempdir().unwrap();
        let file_path = dir.path().join("config.json");

        let content = r#"{}"#;

        let mut file = File::create(&file_path).unwrap();
        file.write_all(content.as_bytes()).unwrap();

        let provider = FileConfigProvider::new(file_path.to_str().unwrap()).unwrap();

        assert!(!provider.has("any.key"));
        let result: Option<String> = provider.get("any.key").unwrap();
        assert!(result.is_none());
    }

    #[test]
    fn test_file_format_case_insensitive() {
        assert_eq!(FileFormat::from_extension(Path::new("config.JSON")), Some(FileFormat::Json));
        assert_eq!(FileFormat::from_extension(Path::new("config.TOML")), Some(FileFormat::Toml));
        assert_eq!(FileFormat::from_extension(Path::new("config.YAML")), Some(FileFormat::Yaml));
        assert_eq!(FileFormat::from_extension(Path::new("config.YML")), Some(FileFormat::Yaml));
    }

    #[test]
    fn test_file_without_extension() {
        assert_eq!(FileFormat::from_extension(Path::new("config")), None);
        assert_eq!(FileFormat::from_extension(Path::new("config.")), None);
    }
}