watchexec_events/
fs.rs

1use std::fmt;
2
3/// Re-export of the Notify file event types.
4#[cfg(feature = "notify")]
5pub mod filekind {
6	pub use notify_types::event::{
7		AccessKind, AccessMode, CreateKind, DataChange, EventKind as FileEventKind, MetadataKind,
8		ModifyKind, RemoveKind, RenameMode,
9	};
10}
11
12/// Pseudo file event types without dependency on Notify.
13#[cfg(not(feature = "notify"))]
14pub mod filekind {
15	pub use crate::sans_notify::{
16		AccessKind, AccessMode, CreateKind, DataChange, EventKind as FileEventKind, MetadataKind,
17		ModifyKind, RemoveKind, RenameMode,
18	};
19}
20
21/// The type of a file.
22///
23/// This is a simplification of the [`std::fs::FileType`] type, which is not constructable and may
24/// differ on different platforms.
25#[derive(Clone, Copy, Debug, Eq, PartialEq)]
26#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
27#[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))]
28pub enum FileType {
29	/// A regular file.
30	File,
31
32	/// A directory.
33	Dir,
34
35	/// A symbolic link.
36	Symlink,
37
38	/// Something else.
39	Other,
40}
41
42impl From<std::fs::FileType> for FileType {
43	fn from(ft: std::fs::FileType) -> Self {
44		if ft.is_file() {
45			Self::File
46		} else if ft.is_dir() {
47			Self::Dir
48		} else if ft.is_symlink() {
49			Self::Symlink
50		} else {
51			Self::Other
52		}
53	}
54}
55
56impl fmt::Display for FileType {
57	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
58		match self {
59			Self::File => write!(f, "file"),
60			Self::Dir => write!(f, "dir"),
61			Self::Symlink => write!(f, "symlink"),
62			Self::Other => write!(f, "other"),
63		}
64	}
65}