Skip to main content

file_storage/path/mutate/
append.rs

1use crate::StoragePath;
2
3impl StoragePath {
4    //! Append
5
6    /// Appends the `string`.
7    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    /// Appends the `string`.
15    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    /// Clones the path and appends the `string`.
24    ///
25    /// The result is the same as `path.clone().with_appended(s)` but with a single allocation.
26    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
35#[cfg(test)]
36mod tests {
37    use crate::StoragePath;
38
39    #[test]
40    fn append() {
41        let path: StoragePath = StoragePath::unix_root();
42        let result: StoragePath = path.with_appended("folder.txt");
43        assert_eq!(result.as_str(), "/folder.txt");
44
45        let file: StoragePath = StoragePath::unix_root().with_appended("folder");
46        let result: StoragePath = file.with_appended(".txt");
47        assert_eq!(result.as_str(), "/folder.txt");
48
49        let folder_path: StoragePath = StoragePath::unix_root().with_appended("folder/");
50        let result: StoragePath = folder_path.with_appended("file.txt");
51        assert_eq!(result.as_str(), "/folder/file.txt");
52    }
53}