2024-11-01 19:08:56 +00:00
|
|
|
//
|
2024-11-02 07:34:08 +00:00
|
|
|
use std::{
|
|
|
|
marker::PhantomData,
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
};
|
2024-11-01 19:08:56 +00:00
|
|
|
|
|
|
|
use crate::fs::{Error, Result};
|
|
|
|
|
2024-11-02 07:34:08 +00:00
|
|
|
pub trait PathType {}
|
|
|
|
|
|
|
|
pub struct PathG;
|
|
|
|
impl PathType for PathG {}
|
|
|
|
|
|
|
|
pub struct FileG;
|
|
|
|
impl PathType for FileG {}
|
|
|
|
|
|
|
|
pub struct DirG;
|
|
|
|
impl PathType for DirG {}
|
2024-11-01 19:20:41 +00:00
|
|
|
|
2024-11-01 19:08:56 +00:00
|
|
|
#[derive(Debug)]
|
2024-11-02 07:34:08 +00:00
|
|
|
pub struct PathReal<'base, 'path, T: PathType> {
|
2024-11-01 19:08:56 +00:00
|
|
|
base: &'base Path,
|
|
|
|
path: &'path Path,
|
2024-11-02 07:34:08 +00:00
|
|
|
_phanton: PhantomData<T>,
|
2024-11-01 19:08:56 +00:00
|
|
|
pub(super) error: Option<Error>,
|
|
|
|
}
|
2024-11-02 07:34:08 +00:00
|
|
|
impl<'base, 'path, T: PathType> PathReal<'base, 'path, T> {
|
|
|
|
pub(super) fn new(base: &'base Path, path: &'path Path) -> Self {
|
|
|
|
Self {
|
|
|
|
base,
|
|
|
|
path,
|
|
|
|
_phanton: PhantomData::<T>,
|
|
|
|
error: PathReal::<T>::validate(base, path),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_pathbuf(&self) -> PathBuf {
|
2024-11-01 19:08:56 +00:00
|
|
|
self.base.join(self.path)
|
|
|
|
}
|
2024-11-02 07:34:08 +00:00
|
|
|
|
2024-11-01 19:08:56 +00:00
|
|
|
pub(super) fn put(&mut self, error: Error) {
|
|
|
|
if self.error.is_none() {
|
|
|
|
self.error.replace(error);
|
|
|
|
}
|
|
|
|
}
|
2024-11-02 07:34:08 +00:00
|
|
|
|
2024-11-01 19:08:56 +00:00
|
|
|
fn validate(base: &Path, path: &Path) -> Option<Error> {
|
2024-11-02 07:34:08 +00:00
|
|
|
match PathReal::<PathG>::clean_path(path) {
|
2024-11-01 19:08:56 +00:00
|
|
|
Err(error) => Some(error),
|
|
|
|
Ok(path) => {
|
|
|
|
if !path.starts_with(base) {
|
|
|
|
return Some(Error::PathTraversal {
|
|
|
|
base: base.to_path_buf(),
|
|
|
|
path,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn clean_path(path: &Path) -> Result<PathBuf> {
|
|
|
|
// let path = path.as_ref();
|
|
|
|
use path_clean::PathClean;
|
|
|
|
let abs_path = if path.is_absolute() {
|
|
|
|
path.to_path_buf()
|
|
|
|
} else {
|
|
|
|
std::env::current_dir().expect("current_dir").join(path)
|
|
|
|
}
|
|
|
|
.clean();
|
|
|
|
Ok(abs_path)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn check_error(&mut self) -> Result<()> {
|
|
|
|
if let Some(error) = self.error.take() {
|
|
|
|
return Err(error);
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-11-02 07:34:08 +00:00
|
|
|
pub fn exists(&mut self) -> Result<bool> {
|
|
|
|
self.check_error()?;
|
|
|
|
Ok(self.as_pathbuf().exists())
|
|
|
|
}
|
|
|
|
|
2024-11-01 19:08:56 +00:00
|
|
|
pub fn is_dir(&mut self) -> Result<bool> {
|
|
|
|
self.check_error()?;
|
2024-11-02 07:34:08 +00:00
|
|
|
Ok(self.as_pathbuf().is_dir())
|
2024-11-01 19:08:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_file(&mut self) -> Result<bool> {
|
|
|
|
self.check_error()?;
|
2024-11-02 07:34:08 +00:00
|
|
|
Ok(self.as_pathbuf().is_file())
|
2024-11-01 19:08:56 +00:00
|
|
|
}
|
2024-11-01 19:20:41 +00:00
|
|
|
|
2024-11-02 07:34:08 +00:00
|
|
|
pub fn as_dir(&mut self) -> Result<Option<PathReal<'base, 'path, DirG>>> {
|
2024-11-01 19:20:41 +00:00
|
|
|
self.check_error()?;
|
2024-11-02 07:34:08 +00:00
|
|
|
if self.as_pathbuf().is_dir() {
|
|
|
|
Ok(Some(PathReal::new(self.base, self.path)))
|
2024-11-01 19:20:41 +00:00
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-02 07:34:08 +00:00
|
|
|
pub fn as_file(&mut self) -> Result<Option<PathReal<'base, 'path, FileG>>> {
|
2024-11-01 19:20:41 +00:00
|
|
|
self.check_error()?;
|
2024-11-02 07:34:08 +00:00
|
|
|
if self.as_pathbuf().is_file() {
|
|
|
|
Ok(Some(PathReal::new(self.base, self.path)))
|
2024-11-01 19:20:41 +00:00
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
2024-11-01 19:08:56 +00:00
|
|
|
}
|
2024-11-02 07:34:08 +00:00
|
|
|
impl From<PathReal<'_, '_, PathG>> for PathBuf {
|
|
|
|
fn from(path: PathReal<PathG>) -> Self {
|
2024-11-01 19:08:56 +00:00
|
|
|
path.base.join(path.path)
|
|
|
|
}
|
|
|
|
}
|