summaryrefslogtreecommitdiffstats
path: root/src/path/closest.rs
blob: 40e5e1efeb5f33ef3c0eda0336501ba8c05a9be2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
use std::path::{Path, PathBuf};

/// return the closest enclosing directory
pub fn closest_dir(mut path: &Path) -> PathBuf {
    loop {
        if path.exists() && path.is_dir() {
            return path.to_path_buf();
        }
        match path.parent() {
            Some(parent) => path = parent,
            None => {
                debug!("no existing parent"); // unexpected
                return path.to_path_buf();
            }
        }
    }
}