file_storage/path/mutate/
append.rs1use crate::{FilePath, FolderPath, StoragePath};
2
3impl StoragePath {
4 pub fn append<S>(&mut self, string: S)
8 where
9 S: AsRef<str>,
10 {
11 unsafe { self.path_mut() }.push_str(string.as_ref());
12 }
13
14 pub fn with_appended<S>(mut self, string: S) -> Self
16 where
17 S: AsRef<str>,
18 {
19 self.append(string);
20 self
21 }
22
23 pub fn clone_append<S>(&self, string: S) -> Self
27 where
28 S: AsRef<str>,
29 {
30 let s: &str = string.as_ref();
31 self.clone_with_extra_capacity(s.len()).with_appended(s)
32 }
33}
34
35impl FilePath {
36 pub fn with_appended<S>(self, string: S) -> StoragePath
40 where
41 S: AsRef<str>,
42 {
43 self.to_path().with_appended(string)
44 }
45
46 pub fn clone_append<S>(&self, string: S) -> StoragePath
50 where
51 S: AsRef<str>,
52 {
53 self.path().clone_append(string)
54 }
55}
56
57impl FolderPath {
58 pub fn with_appended<S>(self, string: S) -> StoragePath
62 where
63 S: AsRef<str>,
64 {
65 self.to_path().with_appended(string)
66 }
67
68 pub fn clone_append<S>(&self, string: S) -> StoragePath
72 where
73 S: AsRef<str>,
74 {
75 self.path().clone_append(string)
76 }
77}
78
79#[cfg(test)]
80mod tests {
81 use crate::{FilePath, FolderPath, StoragePath};
82 use std::error::Error;
83
84 #[test]
85 fn append() -> Result<(), Box<dyn Error>> {
86 let path: StoragePath = StoragePath::unix_root();
87 let result: StoragePath = path.with_appended("folder.txt");
88 assert_eq!(result.as_str(), "/folder.txt");
89
90 let file: FilePath = FolderPath::unix_root().make_file("folder")?;
91 let result: StoragePath = file.with_appended(".txt");
92 assert_eq!(result.as_str(), "/folder.txt");
93
94 let folder: FolderPath = FolderPath::unix_root();
95 let result: StoragePath = folder.with_appended("folder.txt");
96 assert_eq!(result.as_str(), "/folder.txt");
97
98 Ok(())
99 }
100}