arkflow_core/
config.rs

1//! Configuration module
2//!
3//! Provide configuration management for the stream processing engine.
4
5use serde::{Deserialize, Serialize};
6use toml;
7
8use crate::{stream::StreamConfig, Error};
9
10/// Configuration file format
11#[derive(Debug, Clone, Copy)]
12pub enum ConfigFormat {
13    YAML,
14    JSON,
15    TOML,
16}
17
18/// Log configuration
19
20#[derive(Debug, Clone, Serialize, Deserialize)]
21pub struct LoggingConfig {
22    /// Log level
23    pub level: String,
24    /// Output to file?
25    pub file_output: Option<bool>,
26    /// Log file path
27    pub file_path: Option<String>,
28}
29
30/// Engine configuration
31#[derive(Debug, Clone, Serialize, Deserialize)]
32pub struct EngineConfig {
33    /// Streams configuration
34    pub streams: Vec<StreamConfig>,
35    /// Logging configuration (optional)
36    pub logging: Option<LoggingConfig>,
37}
38
39impl EngineConfig {
40    /// Load configuration from file
41    pub fn from_file(path: &str) -> Result<Self, Error> {
42        let content = std::fs::read_to_string(path)
43            .map_err(|e| Error::Config(format!("Unable to read configuration file: {}", e)))?;
44
45        // Determine the format based on the file extension.
46        if let Some(format) = get_format_from_path(path) {
47            return match format {
48                ConfigFormat::YAML => serde_yaml::from_str(&content)
49                    .map_err(|e| Error::Config(format!("YAML parsing error: {}", e))),
50                ConfigFormat::JSON => serde_json::from_str(&content)
51                    .map_err(|e| Error::Config(format!("JSON parsing error: {}", e))),
52                ConfigFormat::TOML => toml::from_str(&content)
53                    .map_err(|e| Error::Config(format!("TOML parsing error: {}", e))),
54            };
55        };
56
57        Err(Error::Config("The configuration file format cannot be determined. Please use YAML, JSON, or TOML format.".to_string()))
58    }
59}
60
61/// Get configuration format from file path.
62fn get_format_from_path(path: &str) -> Option<ConfigFormat> {
63    let path = path.to_lowercase();
64    if path.ends_with(".yaml") || path.ends_with(".yml") {
65        Some(ConfigFormat::YAML)
66    } else if path.ends_with(".json") {
67        Some(ConfigFormat::JSON)
68    } else if path.ends_with(".toml") {
69        Some(ConfigFormat::TOML)
70    } else {
71        None
72    }
73}