use std::path::{Path, PathBuf};
pub fn get_home() -> String {
std::env::var("HOME").expect("HOME environment variable is not set.")
}
pub fn get_cwd() -> PathBuf {
std::env::current_dir().expect("Failed to get the current working directory.")
}
pub fn get_last_path_component<P: AsRef<Path>>(path: P) -> String {
path.as_ref()
.components()
.next_back()
.map(|comp| comp.as_os_str().to_string_lossy().into_owned())
.unwrap()
}
pub fn get_file_name<P: AsRef<Path>>(path: P) -> String {
path.as_ref()
.file_name()
.and_then(|s| s.to_str())
.map(String::from)
.expect("Failed to get the file name.")
}
pub fn get_file_stem<P: AsRef<Path>>(path: P) -> String {
path.as_ref()
.file_stem()
.and_then(|s| s.to_str())
.map(String::from)
.expect("Failed to get the file stem.")
}
pub fn get_file_extension<P: AsRef<Path>>(path: P) -> String {
path.as_ref()
.extension()
.and_then(|s| s.to_str())
.map(String::from)
.unwrap_or(String::from(""))
}
pub fn to_path_buf<P: AsRef<Path>>(path: P) -> PathBuf {
path.as_ref().to_path_buf()
}
#[cfg(test)]
mod tests {
use super::*;
use crate::get_cwd;
use serial_test::serial;
use temp_env::with_var;
#[test]
fn test_get_home() {
with_var("HOME", Some("/tmp/test_home"), || {
let home = get_home();
assert_eq!(home, "/tmp/test_home");
});
}
#[test]
#[serial]
fn test_get_cwd() {
assert_eq!(get_last_path_component(get_cwd()), "file_io");
}
#[test]
fn test_get_last_path_component_str() {
assert_eq!(
get_last_path_component("/some/path/to/file.txt"),
"file.txt"
);
assert_eq!(get_last_path_component("some/path/to/file.txt"), "file.txt");
assert_eq!(get_last_path_component("/some/path/to/folder/"), "folder");
assert_eq!(get_last_path_component("/some/path/to/folder"), "folder");
assert_eq!(get_last_path_component("some/path/to/folder/"), "folder");
assert_eq!(get_last_path_component("some/path/to/folder"), "folder");
assert_eq!(get_last_path_component("/file.txt"), "file.txt");
assert_eq!(get_last_path_component("file.txt"), "file.txt");
assert_eq!(get_last_path_component("/folder/"), "folder");
assert_eq!(get_last_path_component("/folder"), "folder");
assert_eq!(get_last_path_component("folder/"), "folder");
assert_eq!(get_last_path_component("folder"), "folder");
}
#[test]
fn test_get_last_path_component_other_type_spot_checks() {
assert_eq!(
get_last_path_component(String::from("/some/path/to/file.txt")),
"file.txt"
);
assert_eq!(
get_last_path_component(Path::new("/some/path/to/file.txt")),
"file.txt"
);
assert_eq!(
get_last_path_component(PathBuf::from("/some/path/to/file.txt")),
"file.txt"
);
}
#[test]
fn test_get_file_name_str() {
assert_eq!(get_file_name("/some/path/to/file.txt"), "file.txt");
assert_eq!(get_file_name("some/path/to/file.txt"), "file.txt");
assert_eq!(get_file_name("/file.txt"), "file.txt");
assert_eq!(get_file_name("file.txt"), "file.txt");
assert_eq!(get_file_name("/some/path/to/file"), "file");
assert_eq!(get_file_name("some/path/to/file"), "file");
assert_eq!(get_file_name("/file"), "file");
assert_eq!(get_file_name("file"), "file");
}
#[test]
fn test_get_file_name_other_type_spot_checks() {
assert_eq!(
get_file_name(String::from("/some/path/to/file.txt")),
"file.txt"
);
assert_eq!(
get_file_name(Path::new("/some/path/to/file.txt")),
"file.txt"
);
assert_eq!(
get_file_name(PathBuf::from("/some/path/to/file.txt")),
"file.txt"
);
}
#[test]
fn test_get_file_stem_str() {
assert_eq!(get_file_stem("/some/path/to/file.txt"), "file");
assert_eq!(get_file_stem("some/path/to/file.txt"), "file");
assert_eq!(get_file_stem("/file.txt"), "file");
assert_eq!(get_file_stem("file.txt"), "file");
assert_eq!(get_file_stem("/some/path/to/file"), "file");
assert_eq!(get_file_stem("some/path/to/file"), "file");
assert_eq!(get_file_stem("/file"), "file");
assert_eq!(get_file_stem("file"), "file");
}
#[test]
fn test_get_file_stem_other_type_spot_checks() {
assert_eq!(
get_file_stem(String::from("/some/path/to/file.txt")),
"file"
);
assert_eq!(get_file_stem(Path::new("/some/path/to/file.txt")), "file");
assert_eq!(
get_file_stem(PathBuf::from("/some/path/to/file.txt")),
"file"
);
}
#[test]
fn test_get_file_extension_str() {
assert_eq!(get_file_extension("/some/path/to/file.txt"), "txt");
assert_eq!(get_file_extension("some/path/to/file.txt"), "txt");
assert_eq!(get_file_extension("/file.txt"), "txt");
assert_eq!(get_file_extension("file.txt"), "txt");
assert_eq!(get_file_extension("/some/path/to/file"), "");
assert_eq!(get_file_extension("some/path/to/file"), "");
assert_eq!(get_file_extension("/file"), "");
assert_eq!(get_file_extension("file"), "");
}
#[test]
fn test_get_file_extension_other_type_spot_checks() {
assert_eq!(
get_file_extension(String::from("/some/path/to/file.txt")),
"txt"
);
assert_eq!(
get_file_extension(Path::new("/some/path/to/file.txt")),
"txt"
);
assert_eq!(
get_file_extension(PathBuf::from("/some/path/to/file.txt")),
"txt"
);
}
#[test]
fn test_to_path_buf() {
let path_str: &str = "folder/subfolder/file.txt";
let path_buf: PathBuf = to_path_buf(path_str);
assert_eq!(path_buf.to_str().unwrap(), path_str);
let path_string: String = String::from("folder/subfolder/file.txt");
let path_buf: PathBuf = to_path_buf(path_string);
assert_eq!(path_buf.to_str().unwrap(), "folder/subfolder/file.txt");
let path: &Path = Path::new("folder/subfolder/file.txt");
let path_buf: PathBuf = to_path_buf(path);
assert_eq!(path_buf.to_str().unwrap(), "folder/subfolder/file.txt");
let path_buf_input: PathBuf = PathBuf::from("folder/subfolder/file.txt");
let path_buf_output: PathBuf = to_path_buf(path_buf_input);
assert_eq!(
path_buf_output.to_str().unwrap(),
"folder/subfolder/file.txt"
);
}
}