1use std::path::Path;
2
3pub fn has_executable_extension<T: AsRef<Path>, S: AsRef<str>>(path: T, pathext: &[S]) -> bool {
5 let ext = path.as_ref().extension().and_then(|e| e.to_str());
6 match ext {
7 Some(ext) => pathext
8 .iter()
9 .any(|e| !e.as_ref().is_empty() && ext.eq_ignore_ascii_case(&e.as_ref()[1..])),
10 _ => false,
11 }
12}
13
14#[cfg(test)]
15mod test {
16 use super::*;
17 use std::path::PathBuf;
18
19 #[test]
20 fn test_extension_in_extension_vector() {
21 assert!(has_executable_extension(
23 PathBuf::from("foo.exe"),
24 &[".COM", ".EXE", ".CMD"]
25 ));
26
27 assert!(has_executable_extension(
28 PathBuf::from("foo.CMD"),
29 &[".COM", ".EXE", ".CMD"]
30 ));
31 }
32
33 #[test]
34 fn test_extension_not_in_extension_vector() {
35 assert!(!has_executable_extension(
36 PathBuf::from("foo.bar"),
37 &[".COM", ".EXE", ".CMD"]
38 ));
39 }
40
41 #[test]
42 fn test_invalid_exts() {
43 assert!(!has_executable_extension(
44 PathBuf::from("foo.bar"),
45 &["", "."]
46 ));
47 }
48}