#![allow(dead_code)]
use std::env;
use std::ffi::OsStr;
use std::path::{Path, PathBuf};
pub fn pop_newline<T>(input: T) -> String
where
T: std::string::ToString,
{
let mut output = input.to_string();
if output.ends_with('\n') {
output.pop();
}
output
}
pub fn ucfirst<S: AsRef<str>>(s: S) -> String {
let mut c = s.as_ref().chars();
match c.next() {
None => String::new(),
Some(f) => f.to_uppercase().collect::<String>() + c.as_str(),
}
}
pub fn which<P>(input: P) -> bool
where
P: AsRef<Path>,
{
env::var_os("PATH")
.and_then(|paths| env::split_paths(&paths).find(|dir| dir.join(&input).is_file()))
.is_some()
}
pub fn count_lines<T>(buffer: T) -> Option<usize>
where
T: std::string::ToString,
{
let buf = buffer.to_string().trim().to_owned();
if !buf.is_empty() {
return Some(buf.as_bytes().iter().filter(|&&c| c == b'\n').count() + 1);
}
None
}
pub fn get_entries(path: &Path) -> Option<Vec<PathBuf>> {
if let Ok(dir) = std::fs::read_dir(path) {
let mut entries: Vec<PathBuf> = Vec::new();
dir.flatten().for_each(|x| entries.push(x.path()));
return Some(entries);
}
None
}
pub fn path_extension(path: &Path) -> Option<&str> {
path.extension().and_then(OsStr::to_str)
}
pub fn common_shells() -> [&'static str; 10] {
[
"sh", "su", "nu", "bash", "fish", "dash", "tcsh", "zsh", "ksh", "csh",
]
}
#[cfg(test)]
#[cfg(not(target_os = "netbsd"))]
mod tests {
use super::*;
#[test]
fn test_ucfirst() {
assert_eq!(ucfirst("lorem"), "Lorem");
assert_eq!(ucfirst("Ipsum"), "Ipsum");
}
#[test]
fn test_pop_newline() {
assert_eq!(pop_newline(String::from("Lorem ipsum\n")), "Lorem ipsum");
}
#[test]
fn test_path_extension() {
assert_eq!(path_extension(Path::new("foo.rs")).unwrap(), "rs");
assert!(path_extension(Path::new("bar"))
.unwrap_or_default()
.is_empty());
}
#[test]
#[cfg(not(feature = "openwrt"))]
fn test_which() {
assert!(which("python"));
assert!(!which("not_a_real_command"));
}
}