pub mod as_path;
pub mod from_path;
#[cfg(feature = "temp-path")]
mod temp_path;
#[cfg(test)]
#[path = "./mod_test.rs"]
mod mod_test;
use crate::error::FsIOError;
use crate::types::FsIOResult;
use as_path::AsPath;
use from_path::FromPath;
use std::fs;
use std::time::SystemTime;
pub fn canonicalize_as_string<T: AsPath + ?Sized>(path: &T) -> FsIOResult<String> {
let path_obj = path.as_path();
match path_obj.canonicalize() {
Ok(path_buf) => {
let path_string: String = FromPath::from_path(&path_buf);
#[cfg(not(windows))]
{
Ok(path_string)
}
#[cfg(windows)]
{
let win_path_string = match dunce::canonicalize(&path_string) {
Ok(value) => FromPath::from_path(&value),
Err(_) => path_string,
};
Ok(win_path_string)
}
}
Err(error) => Err(FsIOError::IOError(
"Unable to canonicalize path.".to_string(),
Some(error),
)),
}
}
pub fn canonicalize_or<T: AsPath + ?Sized>(path: &T, or_value: &str) -> String {
match canonicalize_as_string(path) {
Ok(value) => value,
Err(_) => or_value.to_string(),
}
}
pub fn get_basename<T: AsPath + ?Sized>(path: &T) -> Option<String> {
let path_obj = path.as_path();
match path_obj.file_name() {
Some(name) => Some(name.to_string_lossy().into_owned()),
None => None,
}
}
pub fn get_parent_directory<T: AsPath + ?Sized>(path: &T) -> Option<String> {
let path_obj = path.as_path();
let directory = path_obj.parent();
match directory {
Some(directory_path) => {
let directory_path_string: String = FromPath::from_path(directory_path);
if directory_path_string.is_empty() {
None
} else {
Some(directory_path_string)
}
}
None => None,
}
}
pub fn get_last_modified_time(path: &str) -> FsIOResult<u128> {
match fs::metadata(path) {
Ok(metadata) => match metadata.modified() {
Ok(time) => match time.duration_since(SystemTime::UNIX_EPOCH) {
Ok(duration) => Ok(duration.as_millis()),
Err(error) => Err(FsIOError::SystemTimeError(
"Unable to get last modified duration for path.".to_string(),
Some(error),
)),
},
Err(error) => Err(FsIOError::IOError(
"Unable to extract modified time for path.".to_string(),
Some(error),
)),
},
Err(error) => Err(FsIOError::IOError(
"Unable to extract metadata for path.".to_string(),
Some(error),
)),
}
}
#[cfg(feature = "temp-path")]
pub fn get_temporary_file_path(extension: &str) -> String {
temp_path::get(extension)
}