use std::path::Path;
use crate::error::{Error, Result};
pub struct FileUtils;
impl FileUtils {
pub fn get_extension(path: impl AsRef<Path>) -> Option<String> {
path.as_ref()
.extension()
.and_then(|ext| ext.to_str())
.map(|s| s.to_lowercase())
}
pub fn get_stem(path: impl AsRef<Path>) -> Option<String> {
path.as_ref()
.file_stem()
.and_then(|stem| stem.to_str())
.map(|s| s.to_string())
}
pub fn get_filename(path: impl AsRef<Path>) -> Option<String> {
path.as_ref()
.file_name()
.and_then(|name| name.to_str())
.map(|s| s.to_string())
}
pub fn exists(path: impl AsRef<Path>) -> bool {
path.as_ref().exists()
}
pub fn is_file(path: impl AsRef<Path>) -> bool {
path.as_ref().is_file()
}
pub fn is_dir(path: impl AsRef<Path>) -> bool {
path.as_ref().is_dir()
}
pub fn create_dir_all(path: impl AsRef<Path>) -> Result<()> {
std::fs::create_dir_all(path.as_ref()).map_err(Error::from)
}
pub fn remove_file(path: impl AsRef<Path>) -> Result<()> {
std::fs::remove_file(path.as_ref()).map_err(Error::from)
}
pub fn remove_dir_all(path: impl AsRef<Path>) -> Result<()> {
std::fs::remove_dir_all(path.as_ref()).map_err(Error::from)
}
pub fn get_size(path: impl AsRef<Path>) -> Result<u64> {
std::fs::metadata(path.as_ref())
.map(|meta| meta.len())
.map_err(Error::from)
}
pub fn get_modified_time(path: impl AsRef<Path>) -> Result<std::time::SystemTime> {
std::fs::metadata(path.as_ref())
.map(|meta| meta.modified().unwrap())
.map_err(Error::from)
}
}