1use serde::{Deserialize, Serialize};
6use toml;
7
8use crate::{stream::StreamConfig, Error};
9
10#[derive(Debug, Clone, Copy)]
12pub enum ConfigFormat {
13 YAML,
14 JSON,
15 TOML,
16}
17
18#[derive(Debug, Clone, Serialize, Deserialize)]
21pub struct LoggingConfig {
22 pub level: String,
24 pub file_output: Option<bool>,
26 pub file_path: Option<String>,
28}
29
30#[derive(Debug, Clone, Serialize, Deserialize)]
32pub struct EngineConfig {
33 pub streams: Vec<StreamConfig>,
35 pub logging: Option<LoggingConfig>,
37}
38
39impl EngineConfig {
40 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 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
61fn 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}