Krolyxon
Commit 7f77734

rmptuisrcutils.rs

27 lines · 831 B · Rust

1use std::ffi::OsStr;
2use std::path::Path;
3
4/// Checks if a file has a given extension
5// https://stackoverflow.com/questions/72392835/check-if-a-file-is-of-a-given-type
6pub trait FileExtension {
7 fn has_extension<S: AsRef<str>>(&self, extensions: &[S]) -> bool;
8}
9
10impl<P: AsRef<Path>> FileExtension for P {
11 fn has_extension<S: AsRef<str>>(&self, extensions: &[S]) -> bool {
12 if let Some(extension) = self.as_ref().extension().and_then(OsStr::to_str) {
13 return extensions
14 .iter()
15 .any(|x| x.as_ref().eq_ignore_ascii_case(extension));
16 }
17
18 false
19 }
20}
21
22pub fn is_audio_file(path: &str) -> bool {
23 Path::new(path).has_extension(&[
24 "mp3", "ogg", "flac", "m4a", "wav", "aac", "opus", "ape", "wma", "mpc", "aiff", "dff",
25 "mp2", "mka",
26 ])
27}