Skip to main content

file_storage/path/convert/
to_folder.rs

1use crate::{Error, FolderPath, Operation, Reason, StoragePath};
2
3impl StoragePath {
4    //! To Folder
5
6    /// Converts the path to a folder path.
7    ///
8    /// Returns `Err` if the path is not a folder path.
9    pub fn to_folder(self) -> Result<FolderPath, Error> {
10        if self.is_folder() {
11            Ok(unsafe { FolderPath::new(self) })
12        } else {
13            Err(Error::new(self, Operation::ModifyPath, Reason::InvalidPath))
14        }
15    }
16
17    /// Clones the path and converts it to a folder path.
18    ///
19    /// Returns `Err` if the path is not a folder path.
20    pub fn clone_to_folder(&self) -> Result<FolderPath, Error> {
21        self.clone().to_folder()
22    }
23}
24
25#[cfg(test)]
26mod tests {
27    use crate::{FolderPath, StoragePath};
28
29    #[test]
30    fn to_folder() {
31        let path: StoragePath = StoragePath::parse("/folder/").unwrap();
32        assert!(path.to_folder().is_ok());
33    }
34
35    #[test]
36    fn to_folder_root() {
37        let path: StoragePath = StoragePath::parse("/").unwrap();
38        assert!(path.to_folder().is_ok());
39    }
40
41    #[test]
42    fn to_folder_file_is_err() {
43        let path: StoragePath = StoragePath::parse("/file.txt").unwrap();
44        assert!(path.to_folder().is_err());
45    }
46
47    #[test]
48    fn clone_to_folder() {
49        let path: StoragePath = StoragePath::parse("/folder/").unwrap();
50        let folder: FolderPath = path.clone_to_folder().unwrap();
51        assert_eq!(folder.as_str(), "/folder/");
52        assert_eq!(path.as_str(), "/folder/");
53    }
54}