urs 0.5.1

Rust utility library
Documentation
//! # misc
//!
//! Miscellaneous utilities

use std::env::var as getenv;
use std::path::{Path, PathBuf};

/// Search for file or directory `stub` in `search_locations`
pub fn search_for_path(search_locations: &[&str], stub: &str) -> Option<PathBuf> {
    for location in search_locations {
        let p = Path::new(location).join(stub);

        match p.try_exists() {
            Ok(true) => return Some(p),
            _ => continue,
        }
    }

    None
}

/// Search for an executable in the PATH environment variable
pub fn search_in_path(exec: &str) -> Option<PathBuf> {
    let path = match getenv("PATH") {
        Ok(p) => p,
        Err(_) => return None,
    };
    let path = path.split([':', ';']);
    search_for_path(&path.collect::<Vec<&str>>(), exec)
}

/// Check if an executable exists in PATH
pub fn is_executable(exec: &str) -> bool {
    search_in_path(exec).is_some()
}

/// Construct a hashmap
///
/// Example:
/// ```
/// use urs::map;
/// let m1 = map!(String, String);
///
/// let m2 = map!{
///     "abc".to_string() => "def"
/// };
/// ```
#[macro_export]
macro_rules! map {
    ($key:ty, $val:ty) => {
        {
            use ::std::collections::HashMap;
            let map: HashMap<$key, $val> = HashMap::new();
            map
        }
    };

    ($($key: expr => $val:expr),*) => {
        {
            use ::std::collections::HashMap;
            let mut map = HashMap::new();
            $( map.insert($key, $val); )*
            map
        }
    };
}

pub mod config;