-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathevent.rs
195 lines (169 loc) · 5.4 KB
/
event.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#![allow(dead_code)]
use super::WatcherState;
use notify::{Event as NotifyEvent, EventKind as NotifyEventKind};
use std::{
fmt,
path::PathBuf,
sync::{Arc, Mutex},
};
use wax::Any;
#[derive(Default, Debug)]
pub struct Event {
path: PathBuf,
file_name: String,
kind: EventKind,
last_path: Arc<Mutex<PathBuf>>,
}
#[derive(Debug)]
pub enum EventKind {
None,
FileCreated,
FolderCreated,
FolderRemoved,
FileUpdated,
FileRenamed,
FileRemoved,
Other(NotifyEventKind),
}
impl Default for EventKind {
fn default() -> Self {
Self::None
}
}
impl Event {
pub fn new<'a>(
ignore: &'a Any<'a>,
state: &WatcherState,
mut event: NotifyEvent,
) -> Option<Self> {
use notify::event::{CreateKind, DataChange, ModifyKind, RemoveKind};
use NotifyEventKind::*;
if event.paths.len() > 1 {
tracing::error!("More than one path! {:#?}", event)
}
let kind = match event.kind {
Create(CreateKind::File) => EventKind::FileCreated,
Create(CreateKind::Folder) => EventKind::FolderCreated,
Modify(ModifyKind::Data(DataChange::Content)) => EventKind::FileUpdated,
Modify(ModifyKind::Name(_)) => EventKind::FileRenamed,
Remove(RemoveKind::File) => EventKind::FileRemoved,
Remove(RemoveKind::Folder) => EventKind::FolderRemoved,
kind => EventKind::Other(kind),
};
let path = event.paths.swap_remove(0);
let file_name = match path.file_name() {
Some(name) => name.to_string_lossy().to_string(),
None => {
tracing::error!("Unable to get event file path name!!! {path:?}",);
Default::default()
}
};
let is_match = wax::Pattern::is_match;
// Skip ignore paths
if is_match(ignore, &*path.to_string_lossy()) {
tracing::trace!(r#""{file_name}" ignored"#);
return None;
}
// Skip if Unsupported event
if let EventKind::Other(kind) = kind {
tracing::trace!(r#"Skip {:?} of "{file_name}""#, kind,);
return None;
}
let event = Self {
path,
file_name,
kind,
last_path: state.last_path(),
};
// Skip when last run was less then 1 second agot
let last_run = state.last_run();
if !(last_run > 1) {
tracing::trace!("Skip [last_run: {last_run}] [{event}]");
return None;
}
Some(event)
}
/// Returns `true` if the watch event kind is [`EventKind::FileUpdated`]
pub fn is_content_update_event(&self) -> bool {
matches!(self.kind, EventKind::FileUpdated)
}
pub fn is_any_but_not_seen(&self) -> bool {
self.is_content_update_event()
|| self.is_rename_event()
|| self.is_create_event()
|| self.is_remove_event()
|| !(self.path().exists() || self.is_seen())
}
/// Returns `true` if the watch event kind is [`EventKind::FileCreated`] or
/// [`EventKind::FolderCretaed`].
pub fn is_create_event(&self) -> bool {
matches!(self.kind, EventKind::FileCreated) || matches!(self.kind, EventKind::FolderCreated)
}
/// Returns `true` if the watch event kind is [`EventKind::FileRemoved`] or
/// [`EventKind::FolderRemoved`].
pub fn is_remove_event(&self) -> bool {
matches!(self.kind, EventKind::FileRemoved) || matches!(self.kind, EventKind::FolderCreated)
}
/// Returns `true` if the watch event kind is [`EventKind::FileRenamed`].
pub fn is_rename_event(&self) -> bool {
matches!(self.kind, EventKind::FileRenamed)
}
/// Returns `true` if the watch event kind is [`EventKind::Other`].
#[must_use]
pub fn is_other_event(&self) -> bool {
matches!(self.kind, EventKind::Other(..))
}
/// Get a reference to the event's kind.
#[must_use]
pub fn kind(&self) -> &EventKind {
&self.kind
}
/// Get a mutable reference to the event's file name.
#[must_use]
pub fn file_name(&self) -> &String {
&self.file_name
}
/// Get a reference to the event's path.
#[must_use]
pub fn path(&self) -> &PathBuf {
&self.path
}
/// Get the event's is seen.
#[must_use]
pub fn is_seen(&self) -> bool {
tracing::trace!("{}", self.file_name);
if self.file_name.eq("project.yml") {
return false;
}
let mut last_path = match self.last_path.lock() {
Ok(path) => path,
Err(err) => {
tracing::error!("{err}");
err.into_inner()
}
};
if last_path.eq(self.path()) {
true
} else {
*last_path = self.path.clone();
false
}
}
}
impl fmt::Display for Event {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use EventKind::*;
let event_name = match &self.kind {
FolderCreated | FileCreated => "created",
FolderRemoved | FileRemoved => "removed",
FileUpdated => "modified",
FileRenamed => "renamed",
Other(event) => {
tracing::trace!("{:?}", event);
"other"
}
_ => "",
};
write!(f, "{:?} [{event_name}]", self.file_name)
}
}