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        let path: StoragePath = if self.is_folder() {
11            self.clone()
12        } else {
13            self.clone_with_extra_capacity(self.file_separator().len_utf8())
14                .with_appended_char(self.file_separator())
15        };
16        // Safety: path is already a folder or we appended the file separator.
17        unsafe { FolderPath::new(path) }
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        let path: StoragePath = if self.is_folder() {
25            self
26        } else {
27            let fs: char = self.file_separator();
28            self.with_appended_char(fs)
29        };
30        // Safety: path is already a folder or we appended the file separator.
31        unsafe { FolderPath::new(path) }
32    }
33}
34
35impl FilePath {
36    //! Make Folder
37
38    /// Makes the file a folder by appending a file-separator.
39    pub fn make_folder(self) -> FolderPath {
40        self.to_path().make_folder()
41    }
42}
43
44#[cfg(test)]
45mod tests {
46    use crate::StoragePath;
47
48    #[test]
49    fn make_folder() {
50        let path: StoragePath = StoragePath::unix_root();
51
52        let path: StoragePath = path.make_folder().to_path();
53        assert_eq!(path.as_str(), "/");
54
55        let path: StoragePath = path.with_appended("folder.txt").make_folder().to_path();
56        assert_eq!(path.as_str(), "/folder.txt/");
57    }
58}