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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
|
use std::path::{Path, PathBuf};
use tracing::warn;
use super::{model::LoadedConfig, Config, CONFIG_PATH_ENV_VAR};
pub fn app_dir_name() -> &'static str {
if cfg!(debug_assertions) {
"herdr-dev"
} else {
"herdr"
}
}
pub fn config_dir() -> PathBuf {
if let Ok(dir) = std::env::var("XDG_CONFIG_HOME") {
PathBuf::from(dir).join(app_dir_name())
} else if let Ok(home) = std::env::var("HOME") {
PathBuf::from(home).join(format!(".config/{}", app_dir_name()))
} else {
PathBuf::from(format!("/tmp/{}", app_dir_name()))
}
}
pub fn state_dir() -> PathBuf {
if let Ok(dir) = std::env::var("XDG_STATE_HOME") {
PathBuf::from(dir).join(app_dir_name())
} else if let Ok(home) = std::env::var("HOME") {
PathBuf::from(home).join(format!(".local/state/{}", app_dir_name()))
} else {
PathBuf::from(format!("/tmp/{}-state", app_dir_name()))
}
}
impl Config {
pub fn load() -> LoadedConfig {
let path = config_path();
if path.exists() {
match std::fs::read_to_string(&path) {
Ok(content) => match toml::from_str::<Config>(&content) {
Ok(config) => {
let diagnostics = config.collect_diagnostics();
return LoadedConfig {
config,
diagnostics,
invalid_sections: Vec::new(),
};
}
Err(err) => {
warn!(err = %err, "config parse error, using defaults");
return LoadedConfig {
config: Self::default(),
diagnostics: vec![format!("config parse error: {err}; using defaults")],
invalid_sections: Vec::new(),
};
}
},
Err(err) => {
warn!(err = %err, "config read error, using defaults");
return LoadedConfig {
config: Self::default(),
diagnostics: vec![format!("config read error: {err}; using defaults")],
invalid_sections: Vec::new(),
};
}
}
}
LoadedConfig {
config: Self::default(),
diagnostics: Vec::new(),
invalid_sections: Vec::new(),
}
}
}
pub(super) fn resolve_config_relative_path(path: &Path) -> PathBuf {
if path.is_absolute() {
return path.to_path_buf();
}
config_path()
.parent()
.unwrap_or_else(|| Path::new("."))
.join(path)
}
pub fn config_path() -> PathBuf {
if let Ok(path) = std::env::var(CONFIG_PATH_ENV_VAR) {
return PathBuf::from(path);
}
config_dir().join("config.toml")
}
pub fn config_diagnostic_summary(diagnostics: &[String]) -> Option<String> {
if diagnostics.is_empty() {
None
} else if diagnostics.len() == 1 {
Some(diagnostics[0].clone())
} else {
Some(format!(
"{} (and {} more)",
diagnostics[0],
diagnostics.len() - 1
))
}
}
pub fn load_live_config() -> Result<LoadedConfig, Vec<String>> {
let path = config_path();
if !path.exists() {
return Ok(LoadedConfig {
config: Config::default(),
diagnostics: Vec::new(),
invalid_sections: Vec::new(),
});
}
let content = std::fs::read_to_string(&path)
.map_err(|err| vec![format!("config read error: {err}; keeping current config")])?;
load_live_config_from_str(&content)
}
fn load_live_config_from_str(content: &str) -> Result<LoadedConfig, Vec<String>> {
let value = content
.parse::<toml::Value>()
.map_err(|err| vec![format!("config parse error: {err}; keeping current config")])?;
let table = value.as_table().ok_or_else(|| {
vec![
"config parse error: top-level config must be a table; keeping current config"
.to_string(),
]
})?;
let mut config = Config::default();
let mut diagnostics = Vec::new();
let mut invalid_sections = Vec::new();
if let Some(value) = table.get("onboarding") {
match value.clone().try_into::<Option<bool>>() {
Ok(onboarding) => config.onboarding = onboarding,
Err(err) => diagnostics.push(format!(
"invalid onboarding setting: {err}; keeping current onboarding state"
)),
}
}
load_live_section(
table,
"theme",
"theme config",
&mut diagnostics,
&mut invalid_sections,
|section| config.theme = section,
);
load_live_section(
table,
"keys",
"keybinding config",
&mut diagnostics,
&mut invalid_sections,
|section| config.keys = section,
);
load_live_section(
table,
"terminal",
"terminal config",
&mut diagnostics,
&mut invalid_sections,
|section| config.terminal = section,
);
load_live_section(
table,
"ui",
"ui config",
&mut diagnostics,
&mut invalid_sections,
|section| config.ui = section,
);
load_live_section(
table,
"advanced",
"advanced config",
&mut diagnostics,
&mut invalid_sections,
|section| config.advanced = section,
);
load_live_section(
table,
"experimental",
"experimental config",
&mut diagnostics,
&mut invalid_sections,
|section| config.experimental = section,
);
Ok(LoadedConfig {
config,
diagnostics,
invalid_sections,
})
}
fn load_live_section<T>(
table: &toml::map::Map<String, toml::Value>,
section: &'static str,
label: &str,
diagnostics: &mut Vec<String>,
invalid_sections: &mut Vec<String>,
apply: impl FnOnce(T),
) where
T: serde::de::DeserializeOwned,
{
let Some(value) = table.get(section) else {
return;
};
match value.clone().try_into::<T>() {
Ok(section_config) => apply(section_config),
Err(err) => {
diagnostics.push(format!(
"invalid {label}: {err}; keeping current {section} settings"
));
invalid_sections.push(section.to_string());
}
}
}
pub(crate) fn upsert_top_level_bool(content: &str, key: &str, value: bool) -> String {
let replacement = format!("{key} = {value}");
let mut lines: Vec<String> = content.lines().map(|line| line.to_string()).collect();
let mut in_section = false;
for line in &mut lines {
let trimmed = line.trim();
if trimmed.starts_with('[') && trimmed.ends_with(']') {
in_section = true;
continue;
}
if in_section {
continue;
}
if trimmed.starts_with(&format!("{key} ")) || trimmed.starts_with(&format!("{key}=")) {
*line = replacement.clone();
return lines.join("\n") + "\n";
}
}
if lines.is_empty() {
format!("{replacement}\n")
} else {
format!("{replacement}\n{}\n", lines.join("\n").trim_end())
}
}
/// Write a key = value pair in a TOML section (creates section if missing).
pub fn upsert_section_value(content: &str, section: &str, key: &str, value: &str) -> String {
upsert_section_raw(content, section, key, value)
}
pub fn upsert_section_bool(content: &str, section: &str, key: &str, value: bool) -> String {
upsert_section_raw(content, section, key, &value.to_string())
}
pub fn remove_section_key(content: &str, section: &str, key: &str) -> String {
let header = format!("[{section}]");
let lines: Vec<&str> = content.lines().collect();
let mut result = Vec::new();
let mut i = 0;
let mut in_section = false;
while i < lines.len() {
let line = lines[i];
let trimmed = line.trim();
if trimmed.starts_with('[') && trimmed.ends_with(']') {
in_section = trimmed == header;
result.push(line.to_string());
i += 1;
continue;
}
if in_section
&& (trimmed.starts_with(&format!("{key} ")) || trimmed.starts_with(&format!("{key}=")))
{
i += 1;
continue;
}
result.push(line.to_string());
i += 1;
}
result.join("\n") + "\n"
}
fn upsert_section_raw(content: &str, section: &str, key: &str, value: &str) -> String {
let header = format!("[{section}]");
let assignment = format!("{key} = {value}");
let lines: Vec<&str> = content.lines().collect();
let mut result = Vec::new();
let mut i = 0;
let mut found_section = false;
let mut inserted = false;
while i < lines.len() {
let line = lines[i];
let trimmed = line.trim();
if trimmed == header {
found_section = true;
result.push(line.to_string());
i += 1;
while i < lines.len() {
let current = lines[i];
let current_trimmed = current.trim();
if current_trimmed.starts_with('[') && current_trimmed.ends_with(']') {
if !inserted {
result.push(assignment.clone());
inserted = true;
}
break;
}
if current_trimmed.starts_with(&format!("{key} "))
|| current_trimmed.starts_with(&format!("{key}="))
{
result.push(assignment.clone());
inserted = true;
} else {
result.push(current.to_string());
}
i += 1;
}
continue;
}
result.push(line.to_string());
i += 1;
}
if !found_section {
if !result.is_empty() && !result.last().is_some_and(|line| line.trim().is_empty()) {
result.push(String::new());
}
result.push(header);
result.push(assignment);
} else if !inserted {
result.push(assignment);
}
result.join("\n") + "\n"
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn upsert_top_level_bool_replaces_existing_value() {
let content = "onboarding = true\n[keys]\nprefix = \"ctrl+b\"\n";
let updated = upsert_top_level_bool(content, "onboarding", false);
assert!(updated.contains("onboarding = false"));
assert!(!updated.contains("onboarding = true"));
}
#[test]
fn upsert_section_bool_adds_missing_section() {
let updated = upsert_section_bool("", "ui.toast", "enabled", true);
assert!(updated.contains("[ui.toast]"));
assert!(updated.contains("enabled = true"));
}
#[test]
fn remove_section_key_removes_matching_key_from_section() {
let content =
"[ui.toast]\nenabled = true\ndelivery = \"herdr\"\n[ui.sound]\nenabled = true\n";
let updated = remove_section_key(content, "ui.toast", "enabled");
assert!(!updated.contains("[ui.toast]\nenabled = true"));
assert!(updated.contains("delivery = \"herdr\""));
assert!(updated.contains("[ui.sound]\nenabled = true"));
}
}
|