Skip to main content

vc_runtime/config/model/
app_config.rs

1//! Application configuration domain model.
2//!
3//! This module defines the root configuration type that aggregates
4//! global settings, profiles, and backend definitions.
5
6use super::{BackendDefinition, GlobalConfig, RuntimeProfile};
7use serde::{Deserialize, Serialize};
8use std::collections::HashMap;
9
10/// Complete application configuration.
11///
12/// Names are the keys in the HashMaps; there is no duplicate name field
13/// inside the structs themselves.
14#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
15pub struct AppConfig {
16    pub global: GlobalConfig,
17    pub profiles: HashMap<String, RuntimeProfile>,
18    pub backends: HashMap<String, BackendDefinition>,
19}
20
21impl AppConfig {
22    /// Retrieves a profile by name.
23    ///
24    /// This is a pure data lookup; no validation is performed.
25    pub fn profile(&self, name: &str) -> Option<&RuntimeProfile> {
26        self.profiles.get(name)
27    }
28
29    /// Retrieves a backend definition by name.
30    ///
31    /// This is a pure data lookup; no validation is performed.
32    pub fn backend(&self, name: &str) -> Option<&BackendDefinition> {
33        self.backends.get(name)
34    }
35
36    /// Resolves the backend selected by the specified profile.
37    ///
38    /// This is a pure data lookup.
39    /// No validation is performed.
40    pub fn resolve_backend(&self, profile_name: &str) -> Option<&BackendDefinition> {
41        let profile = self.profile(profile_name)?;
42        self.backend(&profile.backend_name)
43    }
44}
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49    use crate::config::model::{BackendDefinition, BackendOptions, DummyConfig, RuntimeOverrides};
50    use std::collections::HashMap;
51
52    #[test]
53    fn resolve_backend_found() {
54        let mut backends = HashMap::new();
55        backends.insert(
56            "test".to_owned(),
57            BackendDefinition {
58                enabled: true,
59                options: BackendOptions::Dummy(DummyConfig::default()),
60            },
61        );
62        let mut profiles = HashMap::new();
63        profiles.insert(
64            "local".to_owned(),
65            RuntimeProfile {
66                backend_name: "test".to_owned(),
67                overrides: RuntimeOverrides::default(),
68            },
69        );
70        let config = AppConfig { global: GlobalConfig::default(), profiles, backends };
71
72        let backend = config.resolve_backend("local");
73        assert!(backend.is_some());
74        assert_eq!(backend.unwrap().options, BackendOptions::Dummy(DummyConfig::default()));
75    }
76
77    #[test]
78    fn resolve_backend_profile_missing() {
79        let config = AppConfig::default();
80        assert!(config.resolve_backend("missing").is_none());
81    }
82
83    #[test]
84    fn resolve_backend_backend_missing() {
85        let mut profiles = HashMap::new();
86        profiles.insert(
87            "local".to_owned(),
88            RuntimeProfile {
89                backend_name: "missing".to_owned(),
90                overrides: RuntimeOverrides::default(),
91            },
92        );
93        let config =
94            AppConfig { global: GlobalConfig::default(), profiles, backends: HashMap::new() };
95        assert!(config.resolve_backend("local").is_none());
96    }
97}