use std::fs; use std::os::unix::fs::MetadataExt; use std::path::Path; /// Reads the file and strips whitespace (including newlines) /// Useful for file-based key-value store pub fn read_or_none(path: &Path) -> Option { if !path.is_file() { return None; } match fs::read_to_string(path) { Ok(content) => { // Remove trailing space/newlines Some(content.trim().to_string()) } Err(e) => { eprintln!("IO ERROR: {}", e); None } } } pub fn read_extension(path: &Path, setting: &str) -> Option { let mut path = path.to_path_buf(); path.set_extension(setting); read_or_none(&path) } /// Returns true when the file exists and has user exec /// permission. Returns false otherwise. pub fn is_executable(path: &Path) -> bool { // Do not match directories if !path.is_file() { return false; } match fs::metadata(path) { Ok(stat) => { let mode = stat.mode(); // Check user exec permission) if (mode & 0o100) == 0o100 { true } else { false } } Err(e) => { eprintln!("IO Error: {}", e); false } } }