1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, Serialize, Deserialize)]
5pub struct Config {
6 pub sensitivity: f32,
8
9 pub network_timeout: u64,
11
12 pub min_audio_duration: f32,
14
15 pub max_audio_duration: f32,
17
18 pub sample_rate: u32,
20
21 pub buffer_size: usize,
23
24 pub continuous_recognition: bool,
26
27 pub recognition_interval: f32,
29
30 pub quiet_mode: bool,
32
33 pub deduplicate_requests: bool,
35
36 pub deduplication_cache_duration: u64,
38}
39
40impl Default for Config {
41 fn default() -> Self {
42 Self {
43 sensitivity: 0.5,
44 network_timeout: 20,
45 min_audio_duration: 3.0,
46 max_audio_duration: 12.0,
47 sample_rate: 16000,
48 buffer_size: 4096,
49 continuous_recognition: false,
50 recognition_interval: 5.0,
51 quiet_mode: true, deduplicate_requests: true,
53 deduplication_cache_duration: 300, }
55 }
56}
57
58impl Config {
59 pub fn new() -> Self {
61 Self::default()
62 }
63
64 pub fn with_sensitivity(mut self, sensitivity: f32) -> Self {
66 self.sensitivity = sensitivity.clamp(0.0, 1.0);
67 self
68 }
69
70 pub fn with_network_timeout(mut self, timeout: u64) -> Self {
72 self.network_timeout = timeout;
73 self
74 }
75
76 pub fn with_min_audio_duration(mut self, duration: f32) -> Self {
78 self.min_audio_duration = duration;
79 self
80 }
81
82 pub fn with_max_audio_duration(mut self, duration: f32) -> Self {
84 self.max_audio_duration = duration;
85 self
86 }
87
88 pub fn with_sample_rate(mut self, sample_rate: u32) -> Self {
90 self.sample_rate = sample_rate;
91 self
92 }
93
94 pub fn with_buffer_size(mut self, buffer_size: usize) -> Self {
96 self.buffer_size = buffer_size;
97 self
98 }
99
100 pub fn with_continuous_recognition(mut self, enabled: bool) -> Self {
102 self.continuous_recognition = enabled;
103 self
104 }
105
106 pub fn with_recognition_interval(mut self, interval: f32) -> Self {
108 self.recognition_interval = interval;
109 self
110 }
111
112 pub fn with_quiet_mode(mut self, quiet: bool) -> Self {
114 self.quiet_mode = quiet;
115 self
116 }
117
118 pub fn with_deduplication(mut self, enabled: bool) -> Self {
120 self.deduplicate_requests = enabled;
121 self
122 }
123
124 pub fn with_deduplication_cache_duration(mut self, duration: u64) -> Self {
126 self.deduplication_cache_duration = duration;
127 self
128 }
129
130 pub fn from_file(path: &str) -> Result<Self, Box<dyn std::error::Error>> {
132 let content = std::fs::read_to_string(path)?;
133 let config: Config = toml::from_str(&content)?;
134 Ok(config)
135 }
136
137 pub fn to_file(&self, path: &str) -> Result<(), Box<dyn std::error::Error>> {
139 let content = toml::to_string_pretty(self)?;
140 std::fs::write(path, content)?;
141 Ok(())
142 }
143}