use std::{ffi::OsStr, fs::File, path::Path, path::PathBuf};
use crate::errors::Error;
pub fn open<S>(path: &S) -> Result<File, Error>
where
S: AsRef<Path> + ?Sized,
{
File::open(path).map_err(|e| {
let p: &Path = path.as_ref();
Error::FileOpen(e, p.to_string_lossy().to_string())
})
}
pub fn create<S>(path: &S) -> Result<File, Error>
where
S: AsRef<Path> + ?Sized,
{
File::create(path).map_err(|e| {
let p: &Path = path.as_ref();
Error::FileCreate(e, p.to_string_lossy().to_string())
})
}
pub fn canonicalize<S>(path: &S) -> Result<PathBuf, Error>
where
S: AsRef<Path> + ?Sized,
{
std::fs::canonicalize(path).map_err(|e| {
let p: &Path = path.as_ref();
Error::FileCanonicalize(e, p.to_string_lossy().to_string())
})
}
pub fn set_current_dir<S>(path: &S) -> Result<(), Error>
where
S: AsRef<Path>,
{
std::env::set_current_dir(path).map_err(|e| {
let p: &Path = path.as_ref();
Error::Cwd(e, p.to_string_lossy().to_string())
})
}
pub fn extension(path: &str) -> Option<&str> {
Path::new(path).extension().and_then(OsStr::to_str)
}