1 | //! Configuration types |
2 | |
3 | use 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)] |
7 | pub 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 | |
15 | impl 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)] |
39 | pub struct Config { |
40 | /// See [Config::with_poll_interval] |
41 | poll_interval: Option<Duration>, |
42 | |
43 | /// See [Config::with_compare_contents] |
44 | compare_contents: bool, |
45 | |
46 | follow_symlinks: bool, |
47 | } |
48 | |
49 | impl Config { |
50 | /// For the [`PollWatcher`](crate::PollWatcher) backend. |
51 | /// |
52 | /// Interval between each re-scan attempt. This can be extremely expensive for large |
53 | /// file trees so it is recommended to measure and tune accordingly. |
54 | /// |
55 | /// The default poll frequency is 30 seconds. |
56 | /// |
57 | /// This will enable automatic polling, overwriting [`with_manual_polling()`](Config::with_manual_polling). |
58 | pub fn with_poll_interval(mut self, dur: Duration) -> Self { |
59 | // TODO: v7.0 break signature to option |
60 | self.poll_interval = Some(dur); |
61 | self |
62 | } |
63 | |
64 | /// Returns current setting |
65 | pub fn poll_interval(&self) -> Option<Duration> { |
66 | // Changed Signature to Option |
67 | self.poll_interval |
68 | } |
69 | |
70 | /// For the [`PollWatcher`](crate::PollWatcher) backend. |
71 | /// |
72 | /// Disable automatic polling. Requires calling [`crate::PollWatcher::poll()`] manually. |
73 | /// |
74 | /// This will disable automatic polling, overwriting [`with_poll_interval()`](Config::with_poll_interval). |
75 | pub fn with_manual_polling(mut self) -> Self { |
76 | self.poll_interval = None; |
77 | self |
78 | } |
79 | |
80 | /// For the [`PollWatcher`](crate::PollWatcher) backend. |
81 | /// |
82 | /// Optional feature that will evaluate the contents of changed files to determine if |
83 | /// they have indeed changed using a fast hashing algorithm. This is especially important |
84 | /// for pseudo filesystems like those on Linux under /sys and /proc which are not obligated |
85 | /// to respect any other filesystem norms such as modification timestamps, file sizes, etc. |
86 | /// By enabling this feature, performance will be significantly impacted as all files will |
87 | /// need to be read and hashed at each `poll_interval`. |
88 | /// |
89 | /// This can't be changed during runtime. Off by default. |
90 | pub fn with_compare_contents(mut self, compare_contents: bool) -> Self { |
91 | self.compare_contents = compare_contents; |
92 | self |
93 | } |
94 | |
95 | /// Returns current setting |
96 | pub fn compare_contents(&self) -> bool { |
97 | self.compare_contents |
98 | } |
99 | |
100 | /// For the [INotifyWatcher](crate::INotifyWatcher), [KqueueWatcher](crate::KqueueWatcher), |
101 | /// and [PollWatcher](crate::PollWatcher). |
102 | /// |
103 | /// Determine if sybolic links should be followed when recursively watching a directory. |
104 | /// |
105 | /// This can't be changed during runtime. On by default. |
106 | pub fn with_follow_symlinks(mut self, follow_symlinks: bool) -> Self { |
107 | self.follow_symlinks = follow_symlinks; |
108 | self |
109 | } |
110 | |
111 | /// Returns current setting |
112 | pub fn follow_symlinks(&self) -> bool { |
113 | self.follow_symlinks |
114 | } |
115 | } |
116 | |
117 | impl Default for Config { |
118 | fn default() -> Self { |
119 | Self { |
120 | poll_interval: Some(Duration::from_secs(30)), |
121 | compare_contents: false, |
122 | follow_symlinks: true, |
123 | } |
124 | } |
125 | } |
126 | |