watchexec/
watched_path.rs

1use std::path::{Path, PathBuf};
2
3/// A path to watch.
4///
5/// Can be a recursive or non-recursive watch.
6#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
7pub struct WatchedPath {
8	pub(crate) path: PathBuf,
9	pub(crate) recursive: bool,
10}
11
12impl From<PathBuf> for WatchedPath {
13	fn from(path: PathBuf) -> Self {
14		Self {
15			path,
16			recursive: true,
17		}
18	}
19}
20
21impl From<&str> for WatchedPath {
22	fn from(path: &str) -> Self {
23		Self {
24			path: path.into(),
25			recursive: true,
26		}
27	}
28}
29
30impl From<String> for WatchedPath {
31	fn from(path: String) -> Self {
32		Self {
33			path: path.into(),
34			recursive: true,
35		}
36	}
37}
38
39impl From<&Path> for WatchedPath {
40	fn from(path: &Path) -> Self {
41		Self {
42			path: path.into(),
43			recursive: true,
44		}
45	}
46}
47
48impl From<WatchedPath> for PathBuf {
49	fn from(path: WatchedPath) -> Self {
50		path.path
51	}
52}
53
54impl From<&WatchedPath> for PathBuf {
55	fn from(path: &WatchedPath) -> Self {
56		path.path.clone()
57	}
58}
59
60impl AsRef<Path> for WatchedPath {
61	fn as_ref(&self) -> &Path {
62		self.path.as_ref()
63	}
64}
65
66impl WatchedPath {
67	/// Create a new watched path, recursively descending into subdirectories.
68	pub fn recursive(path: impl Into<PathBuf>) -> Self {
69		Self {
70			path: path.into(),
71			recursive: true,
72		}
73	}
74
75	/// Create a new watched path, not descending into subdirectories.
76	pub fn non_recursive(path: impl Into<PathBuf>) -> Self {
77		Self {
78			path: path.into(),
79			recursive: false,
80		}
81	}
82}