feat #14740: configurable call detection delays
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
|
||||
use std::fs;
|
||||
use std::io;
|
||||
use std::ops::RangeInclusive;
|
||||
use std::path::Path;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
@@ -19,6 +20,12 @@ const DEFAULT_ALLOWLIST: [&str; 6] = [
|
||||
"firefox.exe",
|
||||
];
|
||||
|
||||
/// Allowed continuous mic use before a call counts as started, in seconds.
|
||||
pub const START_AFTER_SECS: RangeInclusive<u64> = 1..=30;
|
||||
/// Allowed mic release before a call counts as ended, in seconds. Too short
|
||||
/// splits a call, e.g. when the headset is switched.
|
||||
pub const END_AFTER_SECS: RangeInclusive<u64> = 3..=120;
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
|
||||
#[serde(default)]
|
||||
pub struct Settings {
|
||||
@@ -50,6 +57,25 @@ impl Default for Settings {
|
||||
}
|
||||
|
||||
impl Settings {
|
||||
/// Checks the values the user can type in; the error is shown as is.
|
||||
pub fn validate(&self) -> Result<(), String> {
|
||||
if !START_AFTER_SECS.contains(&self.start_after_secs) {
|
||||
return Err(format!(
|
||||
"Start nach: {}–{} Sekunden",
|
||||
START_AFTER_SECS.start(),
|
||||
START_AFTER_SECS.end()
|
||||
));
|
||||
}
|
||||
if !END_AFTER_SECS.contains(&self.end_after_secs) {
|
||||
return Err(format!(
|
||||
"Ende nach: {}–{} Sekunden",
|
||||
END_AFTER_SECS.start(),
|
||||
END_AFTER_SECS.end()
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Reads the settings file. A missing or unreadable file yields the
|
||||
/// defaults; fields missing from the file take their default value.
|
||||
pub fn load(path: &Path) -> Settings {
|
||||
@@ -100,6 +126,41 @@ mod tests {
|
||||
assert_eq!(s.end_after_secs, 10);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn validate_accepts_the_defaults_and_the_bounds() {
|
||||
assert_eq!(Settings::default().validate(), Ok(()));
|
||||
for (start, end) in [(1, 3), (30, 120)] {
|
||||
let s = Settings {
|
||||
start_after_secs: start,
|
||||
end_after_secs: end,
|
||||
..Settings::default()
|
||||
};
|
||||
assert_eq!(s.validate(), Ok(()), "start {start}, end {end}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn validate_rejects_start_after_out_of_range() {
|
||||
for start in [0, 31] {
|
||||
let s = Settings {
|
||||
start_after_secs: start,
|
||||
..Settings::default()
|
||||
};
|
||||
assert_eq!(s.validate(), Err("Start nach: 1–30 Sekunden".to_string()));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn validate_rejects_end_after_out_of_range() {
|
||||
for end in [2, 121] {
|
||||
let s = Settings {
|
||||
end_after_secs: end,
|
||||
..Settings::default()
|
||||
};
|
||||
assert_eq!(s.validate(), Err("Ende nach: 3–120 Sekunden".to_string()));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn settings_load_missing_file_returns_default() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
|
||||
Reference in New Issue
Block a user