file_storage/path/convert/
to_file.rs1use crate::{Error, FilePath, Operation, Reason, StoragePath};
2
3impl StoragePath {
4 pub fn to_file(self) -> Result<FilePath, Error> {
10 if self.is_file() {
11 Ok(unsafe { FilePath::new(self) })
12 } else {
13 Err(Error::new(self, Operation::ModifyPath, Reason::InvalidPath))
14 }
15 }
16
17 pub fn clone_to_file(&self) -> Result<FilePath, Error> {
21 self.clone().to_file()
22 }
23}
24
25#[cfg(test)]
26mod tests {
27 use crate::{FilePath, StoragePath};
28
29 #[test]
30 fn to_file() {
31 let path: StoragePath = StoragePath::parse("/file.txt").unwrap();
32 assert!(path.to_file().is_ok());
33 }
34
35 #[test]
36 fn to_file_folder_is_err() {
37 let path: StoragePath = StoragePath::parse("/folder/").unwrap();
38 assert!(path.to_file().is_err());
39 }
40
41 #[test]
42 fn to_file_root_is_err() {
43 let path: StoragePath = StoragePath::parse("/").unwrap();
44 assert!(path.to_file().is_err());
45 }
46
47 #[test]
48 fn clone_to_file() {
49 let path: StoragePath = StoragePath::parse("/file.txt").unwrap();
50 let file: FilePath = path.clone_to_file().unwrap();
51 assert_eq!(file.as_str(), "/file.txt");
52 assert_eq!(path.as_str(), "/file.txt");
53 }
54}