notify_fork/
config.rs

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    pub fn poll_interval(&self) -> Option<Duration> {
64        // Changed Signature to Option
65        self.poll_interval
66    }
67
68    /// For the [PollWatcher](crate::PollWatcher) backend.
69    ///
70    /// Disable automatic polling. Requires calling [crate::PollWatcher::poll] manually.
71    ///
72    /// This will disable automatic polling, overwriting [with_poll_interval](Config::with_poll_interval).
73    pub fn with_manual_polling(mut self) -> Self {
74        self.poll_interval = None;
75        self
76    }
77
78    /// For the [PollWatcher](crate::PollWatcher) backend.
79    ///
80    /// Optional feature that will evaluate the contents of changed files to determine if
81    /// they have indeed changed using a fast hashing algorithm.  This is especially important
82    /// for pseudo filesystems like those on Linux under /sys and /proc which are not obligated
83    /// to respect any other filesystem norms such as modification timestamps, file sizes, etc.
84    /// By enabling this feature, performance will be significantly impacted as all files will
85    /// need to be read and hashed at each `poll_interval`.
86    ///
87    /// This can't be changed during runtime. Off by default.
88    pub fn with_compare_contents(mut self, compare_contents: bool) -> Self {
89        self.compare_contents = compare_contents;
90        self
91    }
92
93    /// Returns current setting
94    pub fn compare_contents(&self) -> bool {
95        self.compare_contents
96    }
97}
98
99impl Default for Config {
100    fn default() -> Self {
101        Self {
102            poll_interval: Some(Duration::from_secs(30)),
103            compare_contents: false,
104        }
105    }
106}