simd_r_drive/utils/
append_extension.rs

1use std::path::{Path, PathBuf};
2
3/// Appends an additional extension to the existing extension of a file path.
4///
5/// This function preserves the existing extension and appends the new extension
6/// to it, instead of replacing it. If the path does not have an existing
7/// extension, the new extension is simply added.
8///
9/// # Arguments
10///
11/// * `path` - A reference to a `PathBuf` representing the file path.
12/// * `ext` - The additional extension to append.
13///
14/// # Returns
15///
16/// A new `PathBuf` with the appended extension.
17///
18/// # Examples
19///
20/// ```
21/// use std::path::{Path, PathBuf};
22/// use simd_r_drive::utils::append_extension;
23///
24/// let path = Path::new("example.txt");
25/// let modified = append_extension(path, "bk");
26/// assert_eq!(modified, PathBuf::from("example.txt.bk"));
27///
28/// let path_no_ext = Path::new("example");
29/// let modified_no_ext = append_extension(path_no_ext, "bk");
30/// assert_eq!(modified_no_ext, PathBuf::from("example.bk"));
31///
32/// let path_multi_ext = Path::new("archive.tar.gz");
33/// let modified_multi_ext = append_extension(path_multi_ext, "bk");
34/// assert_eq!(modified_multi_ext, PathBuf::from("archive.tar.gz.bk"));
35/// ```
36pub fn append_extension(path: &Path, ext: &str) -> PathBuf {
37    let mut new_path = path.to_path_buf();
38    if let Some(original_ext) = new_path.extension() {
39        let new_ext = format!("{}.{}", original_ext.to_string_lossy(), ext);
40        new_path.set_extension(new_ext);
41    } else {
42        new_path.set_extension(ext);
43    }
44    new_path
45}