1use anyhow::Result;
2use std::{
3 env::current_dir,
4 ffi::OsStr,
5 fs::read_dir,
6 path::{Path, PathBuf},
7};
8
9pub fn files_with_extension(dir: impl AsRef<Path>, extension: &str) -> Result<Vec<PathBuf>> {
10 let mut pcs_paths = Vec::new();
11 for result in read_dir(dir)? {
12 let entry = result?;
13 let path = entry.path();
14 if path.is_file() && path.extension() == Some(OsStr::new(extension)) {
15 pcs_paths.push(path);
16 }
17 }
18 Ok(pcs_paths)
19}
20
21pub trait StripCurrentDir {
22 fn strip_current_dir(&self) -> &Self;
23}
24
25impl StripCurrentDir for Path {
26 fn strip_current_dir(&self) -> &Self {
27 let Ok(current_dir) = current_dir() else {
28 return self;
29 };
30 self.strip_prefix(current_dir).unwrap_or(self)
31 }
32}