From 15be9357da63924efa53017d6d609659b1413656 Mon Sep 17 00:00:00 2001 From: krolxon Date: Sat, 1 Jun 2024 20:25:35 +0530 Subject: move FileExtension to utils.rs --- src/utils.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/utils.rs (limited to 'src/utils.rs') 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>(&self, extensions: &[S]) -> bool; +} + +impl> FileExtension for P { + fn has_extension>(&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 + } +} + -- cgit v1.2.3