Skip to main content

file_storage/path/mutate/
append.rs

1use crate::{FilePath, FolderPath, 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
35impl FilePath {
36    //! Append
37
38    /// Appends the `string`.
39    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    /// Clones the path and appends the `string`.
47    ///
48    /// The result is the same as `path.clone().with_appended(s)` but with a single allocation.
49    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    //! Append
59
60    /// Appends the `string`.
61    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    /// Clones the path and appends the `string`.
69    ///
70    /// The result is the same as `path.clone().with_appended(s)` but with a single allocation.
71    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}