use std::ffi::OsString; use std::fs::read_dir; use std::io::ErrorKind; use std::path::PathBuf; use std::{env, io}; /// Adapted from https://github.com/neilwashere/rust-project-root pub fn get_project_root() -> io::Result { let path = env::current_dir()?; let mut path_ancestors = path.as_path().ancestors(); while let Some(p) = path_ancestors.next() { let has_cargo = read_dir(p)? .into_iter() .any(|p| p.unwrap().file_name() == OsString::from("Cargo.lock")); if has_cargo { return Ok(PathBuf::from(p).to_str().unwrap().to_owned()); } } Err(io::Error::new( ErrorKind::NotFound, "Ran out of places to find Cargo.toml", )) } fn main() -> Result<(), Box> { let root = get_project_root().unwrap(); tonic_build::compile_protos(format!("{}/../../protos/notary.proto", root))?; Ok(()) }