Skip to main content

file_storage/path/mutate/
make_folder.rs

1use crate::{FilePath, FolderPath, StoragePath};
2
3impl StoragePath {
4    //! Make Folder
5
6    /// Clones the path and makes the path a folder.
7    ///
8    /// Returns the path as is if it is already a folder, otherwise appends a file-separator.
9    pub fn clone_make_folder(&self) -> FolderPath {
10        if self.is_folder() {
11            self.clone().to_folder().unwrap()
12        } else {
13            self.clone_with_extra_capacity(self.file_separator().len_utf8())
14                .with_appended_char(self.file_separator())
15                .to_folder()
16                .unwrap()
17        }
18    }
19
20    /// Makes the storage path a folder.
21    ///
22    /// Returns the path as is if it is already a folder, otherwise appends a file-separator.
23    pub fn make_folder(self) -> FolderPath {
24        if self.is_folder() {
25            self.to_folder().unwrap()
26        } else {
27            let fs: char = self.file_separator();
28            self.with_appended_char(fs).to_folder().unwrap()
29        }
30    }
31}
32
33impl FilePath {
34    //! Make Folder
35
36    /// Makes the file a folder by appending a file-separator.
37    pub fn make_folder(self) -> FolderPath {
38        let mut path: StoragePath = self.to_path();
39        let file_separator: char = path.file_separator();
40        unsafe { path.path_mut().push(file_separator) };
41        path.to_folder().unwrap()
42    }
43}
44
45#[cfg(test)]
46mod tests {
47    use crate::StoragePath;
48
49    #[test]
50    fn make_folder() {
51        let path: StoragePath = StoragePath::unix_root();
52
53        let path: StoragePath = path.make_folder().to_path();
54        assert_eq!(path.as_str(), "/");
55
56        let path: StoragePath = path.with_appended("folder.txt").make_folder().to_path();
57        assert_eq!(path.as_str(), "/folder.txt/");
58    }
59}