omni_director/core/
config.rs

1//! # Configuration Management
2//!
3//! Centralized configuration for the OmniDirector system.
4
5use serde::{Deserialize, Serialize};
6use std::collections::HashMap;
7
8/// Main system configuration
9#[derive(Debug, Clone, Serialize, Deserialize)]
10pub struct SystemConfig {
11    /// Server configuration
12    pub server: ServerConfig,
13    /// Provider configuration
14    pub providers: ProviderConfig,
15    /// Feature configuration  
16    pub features: FeatureConfig,
17    /// Logging configuration
18    pub logging: LoggingConfig,
19}
20
21/// Server configuration
22#[derive(Debug, Clone, Serialize, Deserialize)]
23pub struct ServerConfig {
24    /// Server host address
25    pub host: String,
26    /// Server port
27    pub port: u16,
28    /// Number of worker threads
29    pub workers: usize,
30    /// Request timeout in seconds
31    pub timeout: u64,
32}
33
34/// Provider configuration
35#[derive(Debug, Clone, Serialize, Deserialize)]
36pub struct ProviderConfig {
37    /// Providers directory path
38    pub providers_dir: String,
39    /// Auto-discovery enabled
40    pub auto_discovery: bool,
41    /// Provider-specific settings
42    pub settings: HashMap<String, serde_json::Value>,
43}
44
45/// Feature configuration
46#[derive(Debug, Clone, Serialize, Deserialize)]
47pub struct FeatureConfig {
48    /// Features directory path
49    pub features_dir: String,
50    /// Auto-discovery enabled
51    pub auto_discovery: bool,
52    /// Feature-specific settings
53    pub settings: HashMap<String, serde_json::Value>,
54}
55
56/// Logging configuration
57#[derive(Debug, Clone, Serialize, Deserialize)]
58pub struct LoggingConfig {
59    /// Log level
60    pub level: String,
61    /// Enable timestamps
62    pub timestamps: bool,
63    /// Log format
64    pub format: String,
65}
66
67impl Default for SystemConfig {
68    fn default() -> Self {
69        Self {
70            server: ServerConfig {
71                host: "0.0.0.0".to_string(),
72                port: 8081,
73                workers: 32,
74                timeout: 30,
75            },
76            providers: ProviderConfig {
77                providers_dir: "./providers".to_string(),
78                auto_discovery: true,
79                settings: HashMap::new(),
80            },
81            features: FeatureConfig {
82                features_dir: "./features".to_string(),
83                auto_discovery: true,
84                settings: HashMap::new(),
85            },
86            logging: LoggingConfig {
87                level: "info".to_string(),
88                timestamps: true,
89                format: "human".to_string(),
90            },
91        }
92    }
93}
94
95impl SystemConfig {
96    /// Load configuration from file or environment
97    pub fn load() -> Result<Self, Box<dyn std::error::Error>> {
98        // Try to load from config file first
99        if let Ok(content) = std::fs::read_to_string("omni-director.toml") {
100            return Ok(toml::from_str(&content)?);
101        }
102        
103        // Fall back to environment variables or defaults
104        let mut config = Self::default();
105        
106        if let Ok(host) = std::env::var("OMNI_HOST") {
107            config.server.host = host;
108        }
109        
110        if let Ok(port) = std::env::var("OMNI_PORT") {
111            config.server.port = port.parse().unwrap_or(8081);
112        }
113        
114        if let Ok(providers_dir) = std::env::var("OMNI_PROVIDERS_DIR") {
115            config.providers.providers_dir = providers_dir;
116        }
117        
118        if let Ok(features_dir) = std::env::var("OMNI_FEATURES_DIR") {
119            config.features.features_dir = features_dir;
120        }
121        
122        Ok(config)
123    }
124}