1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//! # project root
//!
//! Helper to find the absolute root directory path of a project as it stands relative
//! to the location of the nearest Cargo.lock file.

use std::{env, io};
use std::fs::read_dir;
use std::path::PathBuf;

/// Get the project root (relative to closest Cargo.lock file)
/// ```rust
/// match project_root::get_project_root() {
///     Ok(p) => println!("Current project root is {:?}", p),
///     Err(e) => println!("Error obtaining project root {:?}", e)
/// };
/// ```
pub fn get_project_root() -> io::Result<PathBuf> {
    let path = env::current_dir()?;
    let mut path_ancestors = path.as_path().ancestors();
    let mut path_component = path_ancestors.next();

    loop {
        let have_project_root = match path_component {
            None => panic!("no directories left to check :/"),
            Some(p) => {
                // do any entries in this directory look like Cargo.toml?
                read_dir(p)?
                    .into_iter()
                    .any(|p| {
                        p.expect("Unable to get directory entry")
                            .file_name()
                            .to_str()
                            .unwrap()
                            == "Cargo.lock"
                    })
            }
        };

        if have_project_root {
            break;
        }

        path_component = path_ancestors.next();
    }

    let project_path = path_component
        .unwrap()
        .to_str()
        .expect("Could not locate project root");

    Ok(PathBuf::from(project_path))
}

#[cfg(test)]
mod tests {
    use crate::get_project_root;
    use std::fs::read_to_string;

    #[test]
    fn it_should_find_our_project_root() {
        let crate_name = "name = \"project-root\"";

        let project_root = get_project_root().expect("There is no project root");

        let toml_path = project_root.to_str().unwrap().to_owned() + "/Cargo.toml";
        let toml_string = read_to_string(toml_path).unwrap();

        assert!(toml_string.contains(crate_name));
    }
}