1//! Configuration types
2
3use std::time::Duration;
4
5/// Indicates whether only the provided directory or its sub-directories as well should be watched
6#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
7pub enum RecursiveMode {
8 /// Watch all sub-directories as well, including directories created after installing the watch
9 Recursive,
10
11 /// Watch only the provided directory
12 NonRecursive,
13}
14
15impl RecursiveMode {
16 pub(crate) fn is_recursive(&self) -> bool {
17 match *self {
18 RecursiveMode::Recursive => true,
19 RecursiveMode::NonRecursive => false,
20 }
21 }
22}
23
24/// Watcher Backend configuration
25///
26/// This contains multiple settings that may relate to only one specific backend,
27/// such as to correctly configure each backend regardless of what is selected during runtime.
28///
29/// ```rust
30/// # use std::time::Duration;
31/// # use notify::Config;
32/// let config = Config::default()
33/// .with_poll_interval(Duration::from_secs(2))
34/// .with_compare_contents(true);
35/// ```
36///
37/// Some options can be changed during runtime, others have to be set when creating the watcher backend.
38#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
39pub struct Config {
40 /// See [BackendConfig::with_poll_interval]
41 poll_interval: Option<Duration>,
42
43 /// See [BackendConfig::with_compare_contents]
44 compare_contents: bool,
45}
46
47impl Config {
48 /// For the [PollWatcher](crate::PollWatcher) backend.
49 ///
50 /// Interval between each re-scan attempt. This can be extremely expensive for large
51 /// file trees so it is recommended to measure and tune accordingly.
52 ///
53 /// The default poll frequency is 30 seconds.
54 ///
55 /// This will enable automatic polling, overwriting [with_manual_polling](Config::with_manual_polling).
56 pub fn with_poll_interval(mut self, dur: Duration) -> Self {
57 // TODO: v7.0 break signature to option
58 self.poll_interval = Some(dur);
59 self
60 }
61
62 /// Returns current setting
63 #[deprecated(
64 since = "6.1.0",
65 note = "use poll_interval_v2 to account for disabled automatic polling"
66 )]
67 pub fn poll_interval(&self) -> Duration {
68 // TODO: v7.0 break signature to option
69 self.poll_interval.unwrap_or_default()
70 }
71
72 /// Returns current setting
73 pub fn poll_interval_v2(&self) -> Option<Duration> {
74 // TODO: v7.0 break signature to option
75 self.poll_interval
76 }
77
78 /// For the [PollWatcher](crate::PollWatcher) backend.
79 ///
80 /// Disable automatic polling. Requires calling [crate::PollWatcher::poll] manually.
81 ///
82 /// This will disable automatic polling, overwriting [with_poll_interval](Config::with_poll_interval).
83 pub fn with_manual_polling(mut self) -> Self {
84 self.poll_interval = None;
85 self
86 }
87
88 /// For the [PollWatcher](crate::PollWatcher) backend.
89 ///
90 /// Optional feature that will evaluate the contents of changed files to determine if
91 /// they have indeed changed using a fast hashing algorithm. This is especially important
92 /// for pseudo filesystems like those on Linux under /sys and /proc which are not obligated
93 /// to respect any other filesystem norms such as modification timestamps, file sizes, etc.
94 /// By enabling this feature, performance will be significantly impacted as all files will
95 /// need to be read and hashed at each `poll_interval`.
96 ///
97 /// This can't be changed during runtime. Off by default.
98 pub fn with_compare_contents(mut self, compare_contents: bool) -> Self {
99 self.compare_contents = compare_contents;
100 self
101 }
102
103 /// Returns current setting
104 pub fn compare_contents(&self) -> bool {
105 self.compare_contents
106 }
107}
108
109impl Default for Config {
110 fn default() -> Self {
111 Self {
112 poll_interval: Some(Duration::from_secs(30)),
113 compare_contents: false,
114 }
115 }
116}
117