Skip to main content

wordchipper_disk_cache/
path_utils.rs

1//! # Path Utilities
2
3use std::path::{Path, PathBuf};
4
5/// Extend a path with a context and filename.
6///
7/// * Does not check that the path exists.
8/// * Does not initialize the containing directories.
9///
10/// # Arguments
11/// * `context` - prefix dirs, inserted between `self.cache_dir` and `file`.
12/// * `file` - the final file name.
13pub fn extend_path<P, S, F>(
14    path: P,
15    context: &[S],
16    filename: F,
17) -> PathBuf
18where
19    P: AsRef<Path>,
20    S: AsRef<Path>,
21    F: AsRef<Path>,
22{
23    let mut path = path.as_ref().to_path_buf();
24    path.extend(context.iter().map(|s| s.as_ref()));
25    path.push(filename.as_ref());
26    path
27}
28
29#[cfg(test)]
30mod tests {
31    use super::*;
32
33    #[test]
34    fn test_extend_path() {
35        let path = extend_path("/tmp/wordchipper", &["cache", "data"], "file.txt");
36        assert_eq!(path, PathBuf::from("/tmp/wordchipper/cache/data/file.txt"));
37    }
38}