use crate::{Error, FolderPath, Operation, Reason, StoragePath};
impl StoragePath {
pub fn to_folder(self) -> Result<FolderPath, Error> {
if self.is_folder() {
Ok(unsafe { FolderPath::new(self) })
} else {
Err(Error::new(self, Operation::ModifyPath, Reason::InvalidPath))
}
}
pub fn clone_to_folder(&self) -> Result<FolderPath, Error> {
self.clone().to_folder()
}
}
#[cfg(test)]
mod tests {
use crate::{FolderPath, StoragePath};
#[test]
fn to_folder() {
let path: StoragePath = StoragePath::parse("/folder/").unwrap();
assert!(path.to_folder().is_ok());
}
#[test]
fn to_folder_root() {
let path: StoragePath = StoragePath::parse("/").unwrap();
assert!(path.to_folder().is_ok());
}
#[test]
fn to_folder_file_is_err() {
let path: StoragePath = StoragePath::parse("/file.txt").unwrap();
assert!(path.to_folder().is_err());
}
#[test]
fn clone_to_folder() {
let path: StoragePath = StoragePath::parse("/folder/").unwrap();
let folder: FolderPath = path.clone_to_folder().unwrap();
assert_eq!(folder.as_str(), "/folder/");
assert_eq!(path.as_str(), "/folder/");
}
}