| 1 | use std::process::Command; |
| 2 | use std::ffi::OsStr; |
| 3 | use std::path::Path; |
| 4 | |
| 5 | /// Checks if given program is installed in your system |
| 6 | pub fn is_installed(ss: &str) -> bool { |
| 7 | let output = Command::new("which") |
| 8 | .arg(ss) |
| 9 | .output() |
| 10 | .expect("Failed to execute command"); |
| 11 | |
| 12 | output.status.success() |
| 13 | } |
| 14 | |
| 15 | /// Checks if a file has a given extension |
| 16 | // https://stackoverflow.com/questions/72392835/check-if-a-file-is-of-a-given-type |
| 17 | pub trait FileExtension { |
| 18 | fn has_extension<S: AsRef<str>>(&self, extensions: &[S]) -> bool; |
| 19 | } |
| 20 | |
| 21 | impl<P: AsRef<Path>> FileExtension for P { |
| 22 | fn has_extension<S: AsRef<str>>(&self, extensions: &[S]) -> bool { |
| 23 | if let Some(extension) = self.as_ref().extension().and_then(OsStr::to_str) { |
| 24 | return extensions |
| 25 | .iter() |
| 26 | .any(|x| x.as_ref().eq_ignore_ascii_case(extension)); |
| 27 | } |
| 28 | |
| 29 | false |
| 30 | } |
| 31 | } |
| 32 | |