relay_knowledge/watcher/
config.rs1use std::time::Duration;
2
3#[derive(Debug, Clone, PartialEq, Eq)]
4pub struct WatcherConfig {
5 pub enabled: bool,
6 pub debounce: Duration,
7 pub max_watch_dirs: usize,
8 pub hash_cache_capacity: usize,
9}
10
11impl Default for WatcherConfig {
12 fn default() -> Self {
13 Self {
14 enabled: true,
15 debounce: Duration::from_secs(3),
16 max_watch_dirs: 1024,
17 hash_cache_capacity: 4096,
18 }
19 }
20}
21
22impl WatcherConfig {
23 pub const DEFAULT_DEBOUNCE_MS: u64 = 3000;
24 pub const DEFAULT_MAX_WATCH_DIRS: usize = 1024;
25 pub const DEFAULT_HASH_CACHE_CAPACITY: usize = 4096;
26
27 pub fn from_environment(overrides: &crate::env::WatcherEnvOverrides) -> Self {
28 Self {
29 enabled: overrides.enabled.unwrap_or(true),
30 debounce: Duration::from_millis(
31 overrides.debounce_ms.unwrap_or(Self::DEFAULT_DEBOUNCE_MS),
32 ),
33 max_watch_dirs: overrides
34 .max_watch_dirs
35 .unwrap_or(Self::DEFAULT_MAX_WATCH_DIRS),
36 hash_cache_capacity: overrides
37 .hash_cache_capacity
38 .unwrap_or(Self::DEFAULT_HASH_CACHE_CAPACITY),
39 }
40 }
41}
42
43#[cfg(test)]
44mod tests {
45 use super::*;
46
47 #[test]
48 fn default_config_has_sensible_defaults() {
49 let config = WatcherConfig::default();
50 assert!(config.enabled);
51 assert_eq!(config.debounce, Duration::from_secs(3));
52 assert_eq!(config.max_watch_dirs, 1024);
53 assert_eq!(config.hash_cache_capacity, 4096);
54 }
55
56 #[test]
57 fn from_environment_applies_overrides() {
58 let overrides = crate::env::WatcherEnvOverrides {
59 enabled: Some(false),
60 debounce_ms: Some(5000),
61 max_watch_dirs: Some(2048),
62 hash_cache_capacity: Some(8192),
63 };
64 let config = WatcherConfig::from_environment(&overrides);
65 assert!(!config.enabled);
66 assert_eq!(config.debounce, Duration::from_millis(5000));
67 assert_eq!(config.max_watch_dirs, 2048);
68 assert_eq!(config.hash_cache_capacity, 8192);
69 }
70
71 #[test]
72 fn from_environment_uses_defaults_when_no_overrides() {
73 let overrides = crate::env::WatcherEnvOverrides::default();
74 let config = WatcherConfig::from_environment(&overrides);
75 assert!(config.enabled);
76 assert_eq!(config.debounce, Duration::from_secs(3));
77 assert_eq!(config.max_watch_dirs, 1024);
78 assert_eq!(config.hash_cache_capacity, 4096);
79 }
80}