diff --git a/Cargo.toml b/Cargo.toml index 93bff75..08aaa75 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,9 @@ license = "MIT OR Apache-2.0" repository = "https://github.com/dirs-dev/dirs-sys-rs" maintenance = { status = "as-is" } +[dependencies] +option-ext = "0.2.0" + [target.'cfg(unix)'.dependencies] libc = "0.2" diff --git a/src/lib.rs b/src/lib.rs index ebf2e1e..c9e43d9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,5 @@ +extern crate option_ext; + use std::ffi::OsString; use std::path::PathBuf; diff --git a/src/xdg_user_dirs.rs b/src/xdg_user_dirs.rs index de7e9e9..82e9018 100644 --- a/src/xdg_user_dirs.rs +++ b/src/xdg_user_dirs.rs @@ -6,6 +6,8 @@ use std::os::unix::ffi::OsStringExt; use std::path::{Path, PathBuf}; use std::str; +use option_ext::OptionExt; + /// Returns all XDG user directories obtained from $(XDG_CONFIG_HOME)/user-dirs.dirs. pub fn all(home_dir_path: &Path, user_dir_file_path: &Path) -> HashMap { let bytes = read_all(user_dir_file_path).unwrap_or(Vec::new()); @@ -32,7 +34,7 @@ fn parse_user_dirs(home_dir: &Path, user_dir: Option<&str>, bytes: &[u8]) -> Has let key = if key.starts_with(b"XDG_") && key.ends_with(b"_DIR") { match str::from_utf8(&key[4..key.len()-4]) { Ok(key) => - if user_dir.is_some() && option_contains(user_dir, key) { + if user_dir.contains(&key) { single_dir_found = true; key } else if user_dir.is_none() { @@ -137,18 +139,12 @@ fn shell_unescape(escaped: &[u8]) -> Vec { unescaped } -fn option_contains(option: Option, value: T) -> bool { - match option { - Some(val) => val == value, - None => false - } -} - #[cfg(test)] mod tests { use std::collections::HashMap; use std::path::{Path, PathBuf}; - use super::{trim_blank, shell_unescape, split_once, parse_user_dirs}; + + use super::{parse_user_dirs, shell_unescape, split_once, trim_blank}; #[test] fn test_trim_blank() {