prebindgen_project_root/
lib.rs

1use std::path::PathBuf;
2
3/// Returns the absolute path to the cargo workspace root that built this crate.
4///
5/// This works only if this crate is included in a Cargo workspace. In the opposite case,
6/// it will return an error, explaining how to correctly configure the crate.
7pub fn get_project_root() -> Result<PathBuf, &'static str> {
8    let project_root = env!("PROJECT_ROOT");
9    if project_root.is_empty() {
10        let error =
11            "The crate `prebindgen-project-root` is being used as a regular Cargo dependency.\n\
12            Because it is not located within your workspace, it cannot determine the path to the workspace root.\n\
13            Please add `prebindgen-project-root` as a member of your workspace and patch your dependencies to use the local path.\n\n\
14            You can do this with the helper tool:\n\n\
15            cargo install prebindgen-project-root\n\
16            cargo prebindgen-project-root install <path>\n\n\
17            where `<path>` is the path to your workspace root.\n\n\
18            If the patch is already applied and the error persists, verify the version of the patched crate.";
19        Err(error)
20    } else {
21        Ok(project_root.into())
22    }
23}