autopulse_utils/
what_is.rs

1use std::path::{Path, PathBuf};
2
3#[derive(Debug, PartialEq, Eq)]
4pub enum PathType {
5    File,
6    Directory,
7}
8
9pub fn what_is<P: AsRef<Path>>(path: P) -> PathType {
10    let path_ref = path.as_ref();
11
12    if path_ref.extension().is_some() {
13        PathType::File
14    } else {
15        PathType::Directory
16    }
17}
18
19pub fn is_file<P: AsRef<Path>>(path: P) -> bool {
20    matches!(what_is(path), PathType::File)
21}
22
23pub fn is_directory<P: AsRef<Path>>(path: P) -> bool {
24    matches!(what_is(path), PathType::Directory)
25}
26
27pub fn squash_directory<P: AsRef<Path>>(path: P) -> PathBuf {
28    let path_ref = path.as_ref();
29
30    match what_is(path_ref) {
31        PathType::File => path_ref.parent().unwrap_or(path_ref).to_path_buf(),
32        PathType::Directory => path_ref.to_path_buf(),
33    }
34}