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
71
72
73
74
75
76
77
78
//! # 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;
use std::fs::read_dir;
use std::path::PathBuf;

/// Get the project root (relative to closest Cargo.lock file)
/// ```rust
/// let project_path = match project_root::get_project_root() {
///     Ok(p) => p.to_str().expect("Could not retrieve project path").to_string(),
///     Err(e) => panic!(e),
/// };
/// ```
pub fn get_project_root() -> Result<PathBuf, anyhow::Error> {
    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) => {
                // Get all paths (files or directories) at this location
                let paths = read_dir(p).unwrap();
                // return true if one of these paths is Cargo.lock. This means we are at the root!
                paths
                    .into_iter()
                    .any(|p| p.unwrap().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 serde::Deserialize;
    use std::fs::read_to_string;

    #[derive(Deserialize)]
    struct Config {
        package: Package,
    }

    #[derive(Deserialize)]
    struct Package {
        name: String,
    }

    #[test]
    fn it_should_find_our_project_root() {
        let crate_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();
        let toml: Config = toml::from_str(toml_string.as_str()).unwrap();

        assert_eq!(toml.package.name, crate_name)
    }
}