diff options
| author | krolxon <krolyxon@tutanota.com> | 2024-06-01 20:25:35 +0530 |
|---|---|---|
| committer | krolxon <krolyxon@tutanota.com> | 2024-06-01 20:25:35 +0530 |
| commit | 15be9357da63924efa53017d6d609659b1413656 (patch) | |
| tree | afcac43636a7319d9fae001c6fff65145802a1d8 /src/utils.rs | |
| parent | 7ae0a2cc19ba9565b77cc9a459bcaace277677ea (diff) | |
move FileExtension to utils.rs
Diffstat (limited to 'src/utils.rs')
| -rw-r--r-- | src/utils.rs | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..b8c1bea --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,32 @@ +use std::process::Command; +use std::ffi::OsStr; +use std::path::Path; + +/// Checks if given program is installed in your system +pub fn is_installed(ss: &str) -> bool { + let output = Command::new("which") + .arg(ss) + .output() + .expect("Failed to execute command"); + + output.status.success() +} + +/// Checks if a file has a given extension +// https://stackoverflow.com/questions/72392835/check-if-a-file-is-of-a-given-type +pub trait FileExtension { + fn has_extension<S: AsRef<str>>(&self, extensions: &[S]) -> bool; +} + +impl<P: AsRef<Path>> FileExtension for P { + fn has_extension<S: AsRef<str>>(&self, extensions: &[S]) -> bool { + if let Some(extension) = self.as_ref().extension().and_then(OsStr::to_str) { + return extensions + .iter() + .any(|x| x.as_ref().eq_ignore_ascii_case(extension)); + } + + false + } +} + |
