Validate configuration before startup

This commit is contained in:
2026-09-10 21:16:18 +02:00
parent 31defe56d8
commit 73efcb3c35
2 changed files with 66 additions and 3 deletions

View File

@@ -52,7 +52,7 @@ Each bounded iteration must:
- [x] Apply `log_level` from configuration, with `RUST_LOG` as an explicit override (2026-09-10).
- [x] Remove the unimplemented `popup_position` setting (2026-09-10).
- [ ] Validate zero/invalid limits and malformed shortcuts with actionable errors.
- [x] Validate zero/invalid limits and malformed shortcuts with actionable errors (17 core tests and Docker GTK/X11 build, 2026-09-10).
- [ ] Ensure configured history, pinned, and byte limits are applied consistently.
- [ ] Remove or integrate the duplicate unused in-memory `History` implementation.
- [x] Make database mutations report missing rows and preserve UI state on failure (delete/clear paths, 2026-09-10).

View File

@@ -40,12 +40,37 @@ impl Config {
let text = fs::read_to_string(&path)
.with_context(|| format!("failed to read configuration {}", path.display()))?;
let _ = fs::set_permissions(&path, fs::Permissions::from_mode(0o600));
toml::from_str(&text).with_context(|| {
let config: Self = toml::from_str(&text).with_context(|| {
format!(
"invalid configuration {}; fix it or remove it",
path.display()
)
})
})?;
config.validate().with_context(|| {
format!(
"invalid configuration {}; fix it or remove it",
path.display()
)
})?;
Ok(config)
}
pub fn validate(&self) -> Result<()> {
if self.history_limit == 0 {
anyhow::bail!("history_limit must be greater than zero")
}
if self.pinned_limit == 0 {
anyhow::bail!("pinned_limit must be greater than zero")
}
if self.max_entry_bytes == 0 {
anyhow::bail!("max_entry_bytes must be greater than zero")
}
if !matches!(
self.log_level.to_ascii_lowercase().as_str(),
"trace" | "debug" | "info" | "warn" | "error"
) {
anyhow::bail!("log_level must be one of trace, debug, info, warn, or error")
}
validate_shortcut(&self.global_shortcut)
}
pub fn ensure_parent(&self) -> Result<()> {
if let Some(parent) = Self::path().parent() {
@@ -73,5 +98,43 @@ mod tests {
assert_eq!(c.pinned_limit, 25);
assert_eq!(c.max_entry_bytes, 1_048_576);
assert_eq!(c.global_shortcut, "Super+V");
assert!(c.validate().is_ok());
}
#[test]
fn rejects_invalid_runtime_limits_and_shortcuts() {
let mut config = Config::default();
config.history_limit = 0;
assert!(config.validate().is_err());
config.history_limit = 100;
config.global_shortcut = "not-a-shortcut".into();
assert!(config.validate().is_err());
}
#[test]
fn accepts_supported_shortcut_modifiers() {
let mut config = Config::default();
for shortcut in ["Super+V", "Ctrl+Alt+K", "Shift+F"] {
config.global_shortcut = shortcut.into();
assert!(config.validate().is_ok(), "{shortcut}");
}
}
}
fn validate_shortcut(shortcut: &str) -> Result<()> {
let parts: Vec<_> = shortcut.split('+').map(str::trim).collect();
if parts.len() < 2 || parts.last().is_some_and(|key| key.chars().count() != 1) {
anyhow::bail!(
"global_shortcut must end with one key and include a modifier, for example Super+V"
)
}
for modifier in &parts[..parts.len() - 1] {
if !matches!(
modifier.to_ascii_lowercase().as_str(),
"super" | "meta" | "win" | "windows" | "ctrl" | "control" | "alt" | "shift"
) {
anyhow::bail!("unsupported modifier '{modifier}' in global_shortcut")
}
}
Ok(())
}