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
//! The unified configuration object representation.
use std::{collections::BTreeMap, fmt::Display};
use libc::geteuid;
use log::{debug, error, trace};
use serde::{Deserialize, Serialize};
use crate::{
TechnologySettings,
config::technology::TechnologySettingsDefaults,
core::{Context, Os, Purpose},
file::{ConfigLoader, LoadMode},
};
/// An amalgamation of [`Os`], [`Purpose`] and [`Context`].
#[derive(Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
pub struct OsPurposeContext {
os: Os,
/// The target purpose for this override.
purpose: Purpose,
/// The context for this override.
context: Context,
}
impl OsPurposeContext {
/// Creates a new [`OsPurposeContext`]
pub fn new(os: Os, purpose: Purpose, context: Context) -> Self {
Self {
os,
purpose,
context,
}
}
}
impl Display for OsPurposeContext {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}/{}/{}", self.os, self.purpose, self.context)
}
}
/// The unified configuration view for a system.
///
/// Contains the fully resolved OS-level and context-level technology settings.
#[derive(Debug)]
pub struct VoaConfig {
/// The built-in default settings for all supported backend technologies.
defaults: TechnologySettings,
/// Per [`Os`] settings for all supported backend technologies.
oses: BTreeMap<Os, TechnologySettings>,
/// Per [`Os`]/[`Purpose`]/[`Context`] settings for all supported backend technologies.
contexts: BTreeMap<OsPurposeContext, TechnologySettings>,
}
impl VoaConfig {
/// Creates a new [`VoaConfig`] by loading all available configuration files from system-wide
/// and possible user-specific locations.
///
/// # Note
///
/// Depending on the effective uid of the calling user, the configurations are loaded only from
/// system mode locations (i.e. uid < 1000), or from system mode and user mode locations (i.e.
/// uid >= 1000).
pub fn load() -> Self {
let euid = unsafe { geteuid() };
trace!("Config::load called with process user id {euid}");
let loader = if euid < 1000 {
debug!("⤷ Using system mode configuration file locations");
ConfigLoader::load(LoadMode::System)
} else {
debug!("⤷ Using user mode configuration file locations");
ConfigLoader::load(LoadMode::User)
};
Self::from(loader)
}
/// Returns the technology settings for an OS, or the built-in default.
pub fn settings_for_os_or_default(&self, os: &Os) -> &TechnologySettings {
self.oses.get(os).unwrap_or(&self.defaults)
}
/// Returns the technology settings for a specific context, an OS or the built-in default.
pub fn settings_for_context_or_default(
&self,
os: &Os,
purpose: &Purpose,
context: &Context,
) -> &TechnologySettings {
self.contexts
.get(&OsPurposeContext::new(
os.clone(),
purpose.clone(),
context.clone(),
))
.unwrap_or(self.oses.get(os).unwrap_or(&self.defaults))
}
/// Returns a reference to the map of [`Os`] and [`TechnologySettings`].
pub fn oses(&self) -> &BTreeMap<Os, TechnologySettings> {
&self.oses
}
/// Returns a reference to the map of [`OsPurposeContext`] and [`TechnologySettings`].
pub fn contexts(&self) -> &BTreeMap<OsPurposeContext, TechnologySettings> {
&self.contexts
}
}
impl From<ConfigLoader> for VoaConfig {
fn from(value: ConfigLoader) -> Self {
let defaults = TechnologySettings::default();
let mut oses: BTreeMap<Os, TechnologySettings> = BTreeMap::new();
let mut contexts: BTreeMap<OsPurposeContext, TechnologySettings> = BTreeMap::new();
// Extract all OS default technology settings from default configuration files.
for (os, config_file) in value.defaults() {
if let Some(config_technology_settings) = config_file.default_technology_settings() {
match TechnologySettings::from_config_with_defaults(
config_technology_settings,
TechnologySettingsDefaults {
built_in_defaults: &defaults,
os_defaults: None,
},
) {
Ok(technology_settings) => {
oses.insert(os.clone(), technology_settings);
}
Err(error) => {
error!(
"Unable to create default technology settings for OS {os} due to error: {error}"
);
}
}
}
}
// Extract all OS default technology settings from drop-in configuration files.
//
// Here, any OS default settings found in drop-ins will have a higher priority and will
// override previously found ones.
// However, for populating unset fields in these drop-ins, previously found OS default
// technology settings will be considered.
for (os, config_files) in value.drop_ins() {
for config_file in config_files.iter() {
if let Some(config_technology_settings) = config_file.default_technology_settings()
{
match TechnologySettings::from_config_with_defaults(
config_technology_settings,
TechnologySettingsDefaults {
built_in_defaults: &defaults,
os_defaults: oses.get(os),
},
) {
Ok(technology_settings) => {
oses.insert(os.clone(), technology_settings);
}
Err(error) => {
error!(
"Unable to create default technology settings for OS {os} due to error: {error}"
);
}
}
}
}
}
// Extract all context-level technology settings from default configuration files.
for (os, config_file) in value.defaults() {
// NOTE: Each ConfigOsSettings can only contain a list of unique context overrides.
for context_settings in config_file.contexts().iter() {
let os_purpose_context = OsPurposeContext::new(
os.clone(),
context_settings.purpose().clone(),
context_settings.context().clone(),
);
let technology_settings = match TechnologySettings::from_config_with_defaults(
context_settings.config_technology_settings(),
TechnologySettingsDefaults {
built_in_defaults: &defaults,
os_defaults: oses.get(os),
},
) {
Ok(settings) => settings,
Err(error) => {
error!(
"Unable to create technology settings for OS/purpose/context {os_purpose_context} due to an error: {error}"
);
continue;
}
};
contexts.insert(os_purpose_context, technology_settings);
}
}
// Extract all context-level technology settings from default configuration files.
//
// Context-level settings found in drop-in configuration files have a higher
// priority than those found in default configuration files or previously found drop-in
// configuration files (with lower priority).
// Context-level settings of lower priority are overridden.
for (os, config_files) in value.drop_ins() {
for config_file in config_files.iter() {
// NOTE: Each ConfigOsSettings can only contain a list of unique context overrides.
for context_settings in config_file.contexts().iter() {
let os_purpose_context = OsPurposeContext::new(
os.clone(),
context_settings.purpose().clone(),
context_settings.context().clone(),
);
let technology_settings = match TechnologySettings::from_config_with_defaults(
context_settings.config_technology_settings(),
TechnologySettingsDefaults {
built_in_defaults: &defaults,
os_defaults: oses.get(os),
},
) {
Ok(technology_settings) => technology_settings,
Err(error) => {
error!(
"Unable to create technology settings for OS/purpose/context {os_purpose_context} due to an error: {error}"
);
continue;
}
};
contexts.insert(os_purpose_context, technology_settings);
}
}
}
Self {
defaults,
oses,
contexts,
}
}
}