1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use crate::*;
use extend::ext;
use std::path::Path;
use std::path::PathBuf;

#[ext]
pub impl PathBuf {
	fn push_with<P: AsRef<Path>>(parent: &PathBuf, path: P) -> Self {
		let mut child = parent.clone();
		child.push(path);
		child
	}
	fn filename_starts_with_uppercase(&self) -> bool {
		self.file_name().str().first().is_ascii_uppercase()
	}

	fn filename_included(&self, arr: &[&str]) -> bool {
		arr.iter().any(|f| self.file_stem().unwrap() == *f)
	}
	fn filestem_starts_with_underscore(&self) -> bool {
		self.file_stem().str().first() == '_'
	}
	fn filestem_ends_with_underscore(&self) -> bool {
		self.file_stem().str().last() == '_'
	}
	fn filestem_ends_with_double_underscore(&self) -> bool {
		self.file_stem().str().ends_with("__")
	}
	fn filestem_contains_double_underscore(&self) -> bool {
		self.file_stem().str().contains("__")
	}

	fn filestem_ends_with_triple_underscore(&self) -> bool {
		self.file_stem().str().ends_with("___")
	}

	fn parent_ends_with_underscore(&self) -> bool {
		match self.parent() {
			Some(parent) => {
				parent.to_path_buf().file_name().str().last() == '_'
			}
			None => false,
		}
	}
	fn parent_ends_with_double_underscore(&self) -> bool {
		match self.parent() {
			Some(parent) => {
				parent.to_path_buf().file_name().str().ends_with("__")
			}
			None => false,
		}
	}


	fn is_dir_or_extension(&self, ext: &str) -> bool {
		match self.extension() {
			Some(value) => value.to_str().unwrap() == ext,
			None => self.is_dir(),
		}
	}

	fn parents(&self) -> Vec<PathBuf> {
		let mut acc = Vec::new();
		let mut current = self.clone();
		if self.is_dir() {
			acc.push(self.clone());
		}
		while let Some(parent) = current.parent() {
			acc.push(parent.to_path_buf());
			current = parent.to_path_buf();
		}
		acc
	}

	fn pop_first_two_path_components(path: &str) -> PathBuf {
		let mut components = Path::new(path).components();
		components.next();
		components.next();
		components.as_path().to_path_buf()
	}
}