Skip to main content

wordchipper_disk_cache/
path_utils.rs

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