Skip to content

Commit eaf02e9

Browse files
authored
feat: report file types for create / remove events by polling backend (#66)
`CreateKind` and `RemoveKind` are set to values other than `Any` now.
1 parent 0e6df2e commit eaf02e9

File tree

1 file changed

+43
-16
lines changed

1 file changed

+43
-16
lines changed

notify/src/poll.rs

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ mod data {
7979
cell::RefCell,
8080
collections::{HashMap, hash_map::RandomState},
8181
fmt::{self, Debug},
82-
fs::{File, Metadata},
82+
fs::{File, FileType, Metadata},
8383
hash::{BuildHasher, Hasher},
8484
io::{self, Read},
8585
path::{Path, PathBuf},
@@ -403,6 +403,8 @@ mod data {
403403
/// File updated time.
404404
mtime: i64,
405405

406+
file_type: FileType,
407+
406408
/// Content's hash value, only available if user request compare file
407409
/// contents and read successful.
408410
hash: Option<u64>,
@@ -418,6 +420,7 @@ mod data {
418420

419421
PathData {
420422
mtime: metadata.modified().map_or(0, system_time_to_seconds),
423+
file_type: metadata.file_type(),
421424
hash: data_builder
422425
.build_hasher
423426
.as_ref()
@@ -450,6 +453,30 @@ mod data {
450453
Ok(hasher.finish())
451454
}
452455

456+
/// Get `CreateKind` by file type.
457+
fn get_create_kind(&self) -> CreateKind {
458+
#[expect(clippy::filetype_is_file)]
459+
if self.file_type.is_dir() {
460+
CreateKind::Folder
461+
} else if self.file_type.is_file() {
462+
CreateKind::File
463+
} else {
464+
CreateKind::Any
465+
}
466+
}
467+
468+
/// Get `RemoveKind` by file type.
469+
fn get_remove_kind(&self) -> RemoveKind {
470+
#[expect(clippy::filetype_is_file)]
471+
if self.file_type.is_dir() {
472+
RemoveKind::Folder
473+
} else if self.file_type.is_file() {
474+
RemoveKind::File
475+
} else {
476+
RemoveKind::Any
477+
}
478+
}
479+
453480
/// Get [`Event`] by compare two optional [`PathData`].
454481
fn compare_to_event<P>(
455482
path: P,
@@ -471,8 +498,8 @@ mod data {
471498
None
472499
}
473500
}
474-
(None, Some(_new)) => Some(EventKind::Create(CreateKind::Any)),
475-
(Some(_old), None) => Some(EventKind::Remove(RemoveKind::Any)),
501+
(None, Some(new)) => Some(EventKind::Create(new.get_create_kind())),
502+
(Some(old), None) => Some(EventKind::Remove(old.get_remove_kind())),
476503
(None, None) => None,
477504
}
478505
.map(|event_kind| Event::new(event_kind).add_path(path.into()))
@@ -834,7 +861,7 @@ mod tests {
834861
std::fs::File::create_new(&path).expect("Unable to create");
835862

836863
rx.sleep_until_exists(&path);
837-
rx.wait_ordered_exact([expected(&path).create_any()]);
864+
rx.wait_ordered_exact([expected(&path).create_file()]);
838865
}
839866

840867
#[test]
@@ -850,7 +877,7 @@ mod tests {
850877
std::fs::File::create_new(&path).expect("create");
851878

852879
rx.sleep_until_exists(&path);
853-
rx.wait_ordered_exact([expected(&path).create_any()]);
880+
rx.wait_ordered_exact([expected(&path).create_file()]);
854881
}
855882

856883
#[test]
@@ -889,7 +916,7 @@ mod tests {
889916
std::fs::create_dir_all(path.parent().unwrap()).expect("create");
890917
std::fs::File::create_new(&path).expect("create");
891918

892-
rx.wait_ordered_exact([expected(&path).create_any()]);
919+
rx.wait_ordered_exact([expected(&path).create_file()]);
893920
}
894921

895922
#[test]
@@ -903,7 +930,7 @@ mod tests {
903930
std::fs::create_dir(&path).expect("Unable to create");
904931

905932
rx.sleep_until_exists(&path);
906-
rx.wait_ordered_exact([expected(&path).create_any()]);
933+
rx.wait_ordered_exact([expected(&path).create_folder()]);
907934
}
908935

909936
#[test]
@@ -940,8 +967,8 @@ mod tests {
940967
rx.sleep_until_exists(&new_path);
941968

942969
rx.wait_unordered_exact([
943-
expected(&path).remove_any(),
944-
expected(&new_path).create_any(),
970+
expected(&path).remove_file(),
971+
expected(&new_path).create_file(),
945972
]);
946973
}
947974

@@ -962,15 +989,15 @@ mod tests {
962989
rx.sleep_while_exists(&path);
963990
rx.sleep_until_exists(&new_path);
964991

965-
rx.wait_unordered_exact([expected(&path).remove_any()])
992+
rx.wait_unordered_exact([expected(&path).remove_file()])
966993
.ensure_no_tail();
967994

968995
std::fs::rename(&new_path, &path).expect("rename2");
969996

970997
rx.sleep_while_exists(&new_path);
971998
rx.sleep_until_exists(&path);
972999

973-
rx.wait_unordered_exact([expected(&path).create_any()])
1000+
rx.wait_unordered_exact([expected(&path).create_file()])
9741001
.ensure_no_tail();
9751002
}
9761003

@@ -998,7 +1025,7 @@ mod tests {
9981025
rx.sleep_while_exists(&path);
9991026
rx.sleep_until_exists(&new_path);
10001027

1001-
rx.wait_unordered_exact([expected(&path).remove_any()])
1028+
rx.wait_unordered_exact([expected(&path).remove_file()])
10021029
.ensure_no_tail();
10031030

10041031
let result = watcher.watcher.watch(
@@ -1031,7 +1058,7 @@ mod tests {
10311058
rx.sleep_while_exists(&path);
10321059
rx.wait_ordered_exact([
10331060
expected(&path).modify_data_any().optional(),
1034-
expected(&path).remove_any(),
1061+
expected(&path).remove_file(),
10351062
]);
10361063
}
10371064

@@ -1050,13 +1077,13 @@ mod tests {
10501077
rx.sleep_while_exists(&path);
10511078
rx.wait_ordered_exact([
10521079
expected(&path).modify_data_any().optional(),
1053-
expected(&path).remove_any(),
1080+
expected(&path).remove_file(),
10541081
]);
10551082

10561083
std::fs::write(&path, "").expect("write");
10571084

10581085
rx.sleep_until_exists(&path);
1059-
rx.wait_ordered_exact([expected(&path).create_any()]);
1086+
rx.wait_ordered_exact([expected(&path).create_file()]);
10601087
}
10611088

10621089
#[test]
@@ -1080,7 +1107,7 @@ mod tests {
10801107
rx.sleep_while_exists(&path);
10811108
rx.wait_ordered_exact([
10821109
expected(&path).modify_data_any().optional(),
1083-
expected(&path).remove_any(),
1110+
expected(&path).remove_file(),
10841111
]);
10851112

10861113
std::fs::write(&path, "").expect("write");

0 commit comments

Comments
 (0)