Fs watcher (#113)

fs watcher
This commit is contained in:
Christian Visintin
2022-06-09 13:03:02 +02:00
committed by GitHub
parent 2caa0432df
commit 816270d545
25 changed files with 1665 additions and 47 deletions

View File

@@ -37,10 +37,9 @@ pub fn absolutize(wrkdir: &Path, target: &Path) -> PathBuf {
///
/// This function has been written by <https://github.com/Manishearth>
/// and is licensed under the APACHE-2/MIT license <https://github.com/Manishearth/pathdiff>
pub fn diff_paths<P, B>(path: P, base: B) -> Option<PathBuf>
pub fn diff_paths<P>(path: P, base: P) -> Option<PathBuf>
where
P: AsRef<Path>,
B: AsRef<Path>,
{
let path = path.as_ref();
let base = base.as_ref();
@@ -82,6 +81,11 @@ where
}
}
/// Returns whether `p` is child (direct/indirect) of ancestor `ancestor`
pub fn is_child_of<P: AsRef<Path>>(p: P, ancestor: P) -> bool {
p.as_ref().ancestors().any(|x| x == ancestor.as_ref())
}
#[cfg(test)]
mod test {
@@ -120,4 +124,27 @@ mod test {
Path::new("foo/bar/chiedo.gif")
);
}
#[test]
fn should_tell_whether_path_is_child_of() {
assert_eq!(
is_child_of(Path::new("/home/foo/foo.txt"), Path::new("/home"),),
true
);
assert_eq!(
is_child_of(Path::new("/home/foo/foo.txt"), Path::new("/home/foo/"),),
true
);
assert_eq!(
is_child_of(
Path::new("/home/foo/foo.txt"),
Path::new("/home/foo/foo.txt"),
),
true
);
assert_eq!(
is_child_of(Path::new("/home/foo/foo.txt"), Path::new("/tmp"),),
false
);
}
}

View File

@@ -58,7 +58,7 @@ pub fn create_sample_file() -> NamedTempFile {
/// ### make_file_at
///
/// Make a file with `name` at specified path
pub fn make_file_at(dir: &Path, filename: &str) -> std::io::Result<()> {
pub fn make_file_at(dir: &Path, filename: &str) -> std::io::Result<PathBuf> {
let mut p: PathBuf = PathBuf::from(dir);
p.push(filename);
let mut file = StdFile::create(p.as_path())?;
@@ -66,7 +66,7 @@ pub fn make_file_at(dir: &Path, filename: &str) -> std::io::Result<()> {
file,
"Lorem ipsum dolor sit amet, consectetur adipiscing elit.Mauris ultricies consequat eros,nec scelerisque magna imperdiet metus."
)?;
Ok(())
Ok(p)
}
/// ### make_dir_at